diff --git a/db/config.go b/db/config.go index ee6eb5d29..b756f48c5 100644 --- a/db/config.go +++ b/db/config.go @@ -13,7 +13,7 @@ import ( type database struct { db *gorm.DB getWorkspaceByUuid func(uuid string) Workspace - getUserRoles func(uuid string, pubkey string) []UserRoles + getUserRoles func(uuid string, pubkey string) []WorkspaceUserRoles } func NewDatabaseConfig(db *gorm.DB) *database { @@ -197,10 +197,12 @@ func (db database) MigrateOrganizationToWorkspace() { db.db.Migrator().RenameTable(&OrganizationUsers{}, "workspace_users") } - if (db.db.Migrator().HasTable(&UserRoles{})) { + if (db.db.Migrator().HasTable(&UserRoles{}) && !db.db.Migrator().HasTable("workspace_user_roles")) { if db.db.Migrator().HasColumn(&UserRoles{}, "org_uuid") { db.db.Migrator().RenameColumn(&UserRoles{}, "org_uuid", "workspace_uuid") } + + db.db.Migrator().RenameTable(&UserRoles{}, "workspace_user_roles") } if (db.db.Migrator().HasTable(&Bounty{})) { @@ -261,7 +263,7 @@ func GetRolesMap() map[string]string { return roles } -func GetUserRolesMap(userRoles []UserRoles) map[string]string { +func GetUserRolesMap(userRoles []WorkspaceUserRoles) map[string]string { roles := map[string]string{} for _, v := range userRoles { roles[v.Role] = v.Role @@ -291,7 +293,7 @@ func (db database) ConvertMetricsBountiesToMap(metricsCsv []MetricsBountyCsv) [] return metricsMap } -func RolesCheck(userRoles []UserRoles, check string) bool { +func RolesCheck(userRoles []WorkspaceUserRoles, check string) bool { rolesMap := GetRolesMap() userRolesMap := GetUserRolesMap(userRoles) @@ -309,7 +311,7 @@ func RolesCheck(userRoles []UserRoles, check string) bool { return true } -func CheckUser(userRoles []UserRoles, pubkey string) bool { +func CheckUser(userRoles []WorkspaceUserRoles, pubkey string) bool { for _, role := range userRoles { if role.OwnerPubKey == pubkey { return true diff --git a/db/config_test.go b/db/config_test.go index 386c8dfd5..2c92099e0 100644 --- a/db/config_test.go +++ b/db/config_test.go @@ -10,8 +10,8 @@ import ( func TestRolesCheck_UserHasRole(t *testing.T) { // Mock user roles - userRoles := []UserRoles{ - {Role: "ADD BOUNTY", OwnerPubKey: "user1", OrgUuid: "org1", Created: &time.Time{}}, + userRoles := []WorkspaceUserRoles{ + {Role: "ADD BOUNTY", OwnerPubKey: "user1", WorkspaceUuid: "org1", Created: &time.Time{}}, } // Role to check @@ -28,8 +28,8 @@ func TestRolesCheck_UserHasRole(t *testing.T) { func TestRolesCheck_UserDoesNotHaveRole(t *testing.T) { // Mock user roles - userRoles := []UserRoles{ - {Role: "DELETE BOUNTY", OwnerPubKey: "user2", OrgUuid: "org1", Created: &time.Time{}}, + userRoles := []WorkspaceUserRoles{ + {Role: "DELETE BOUNTY", OwnerPubKey: "user2", WorkspaceUuid: "org1", Created: &time.Time{}}, } // Role to check @@ -45,7 +45,7 @@ func TestRolesCheck_UserDoesNotHaveRole(t *testing.T) { } func TestCheckUser(t *testing.T) { - userRoles := []UserRoles{ + userRoles := []WorkspaceUserRoles{ {OwnerPubKey: "userPublicKey"}, } @@ -62,9 +62,9 @@ func TestUserHasAccess(t *testing.T) { } } - mockGetUserRoles := func(uuid string, pubkey string) []UserRoles { - return []UserRoles{ - {Role: "ADD BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}}, + mockGetUserRoles := func(uuid string, pubkey string) []WorkspaceUserRoles { + return []WorkspaceUserRoles{ + {Role: "ADD BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}}, } } @@ -110,17 +110,17 @@ func TestUserHasManageBountyRoles(t *testing.T) { } } - mockGetUserRoles := func(uuid string, pubkey string) []UserRoles { + mockGetUserRoles := func(uuid string, pubkey string) []WorkspaceUserRoles { if uuid == "workspace_uuid" { - return []UserRoles{ - {Role: "ADD BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}}, + return []WorkspaceUserRoles{ + {Role: "ADD BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}}, } } else { - return []UserRoles{ - {Role: "ADD BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}}, - {Role: "UPDATE BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}}, - {Role: "DELETE BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}}, - {Role: "PAY BOUNTY", OwnerPubKey: pubkey, OrgUuid: uuid, Created: &time.Time{}}, + return []WorkspaceUserRoles{ + {Role: "ADD BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}}, + {Role: "UPDATE BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}}, + {Role: "DELETE BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}}, + {Role: "PAY BOUNTY", OwnerPubKey: pubkey, WorkspaceUuid: uuid, Created: &time.Time{}}, } } } diff --git a/db/interface.go b/db/interface.go index 2dc58fa04..c0cffb051 100644 --- a/db/interface.go +++ b/db/interface.go @@ -95,7 +95,7 @@ type Database interface { CreateWorkspaceUser(orgUser WorkspaceUsers) WorkspaceUsers DeleteWorkspaceUser(orgUser WorkspaceUsersData, org string) WorkspaceUsersData GetBountyRoles() []BountyRoles - CreateUserRoles(roles []UserRoles, uuid string, pubkey string) []UserRoles + CreateUserRoles(roles []WorkspaceUserRoles, uuid string, pubkey string) []WorkspaceUserRoles GetUserCreatedWorkspaces(pubkey string) []Workspace GetUserAssignedWorkspaces(pubkey string) []WorkspaceUsers AddBudgetHistory(budget BudgetHistory) BudgetHistory diff --git a/db/structs.go b/db/structs.go index d8018ea51..679266739 100644 --- a/db/structs.go +++ b/db/structs.go @@ -515,6 +515,14 @@ type UserRoles struct { Created *time.Time `json:"created"` } +// change back to UserRoles after migration +type WorkspaceUserRoles struct { + Role string `json:"role"` + OwnerPubKey string `json:"owner_pubkey"` + WorkspaceUuid string `json:"workspace_uuid"` + Created *time.Time `json:"created"` +} + type BountyBudget struct { ID uint `json:"id"` OrgUuid string `json:"org_uuid"` diff --git a/db/workspaces.go b/db/workspaces.go index 0e46bd54c..0619b38cc 100644 --- a/db/workspaces.go +++ b/db/workspaces.go @@ -96,16 +96,16 @@ func (db database) GetBountyRoles() []BountyRoles { return ms } -func (db database) CreateUserRoles(roles []UserRoles, uuid string, pubkey string) []UserRoles { +func (db database) CreateUserRoles(roles []WorkspaceUserRoles, uuid string, pubkey string) []WorkspaceUserRoles { // delete roles and create new ones - db.db.Where("workspace_uuid = ?", uuid).Where("owner_pub_key = ?", pubkey).Delete(&UserRoles{}) + db.db.Where("workspace_uuid = ?", uuid).Where("owner_pub_key = ?", pubkey).Delete(&WorkspaceUserRoles{}) db.db.Create(&roles) return roles } -func (db database) GetUserRoles(uuid string, pubkey string) []UserRoles { - ms := []UserRoles{} +func (db database) GetUserRoles(uuid string, pubkey string) []WorkspaceUserRoles { + ms := []WorkspaceUserRoles{} db.db.Where("workspace_uuid = ?", uuid).Where("owner_pub_key = ?", pubkey).Find(&ms) return ms } diff --git a/handlers/workspaces.go b/handlers/workspaces.go index 0407c8724..5dc7f54e2 100644 --- a/handlers/workspaces.go +++ b/handlers/workspaces.go @@ -339,7 +339,7 @@ func AddUserRoles(w http.ResponseWriter, r *http.Request) { return } - roles := []db.UserRoles{} + roles := []db.WorkspaceUserRoles{} body, err := io.ReadAll(r.Body) r.Body.Close() err = json.Unmarshal(body, &roles) @@ -380,7 +380,7 @@ func AddUserRoles(w http.ResponseWriter, r *http.Request) { } rolesMap := db.GetRolesMap() - insertRoles := []db.UserRoles{} + insertRoles := []db.WorkspaceUserRoles{} for _, role := range roles { _, ok := rolesMap[role.Role] // if any of the roles does not exists return an error diff --git a/mocks/Database.go b/mocks/Database.go index 716cff8b3..abf970283 100644 --- a/mocks/Database.go +++ b/mocks/Database.go @@ -1096,19 +1096,19 @@ func (_c *Database_CreateOrEditWorkspace_Call) RunAndReturn(run func(db.Workspac } // CreateUserRoles provides a mock function with given fields: roles, uuid, pubkey -func (_m *Database) CreateUserRoles(roles []db.UserRoles, uuid string, pubkey string) []db.UserRoles { +func (_m *Database) CreateUserRoles(roles []db.WorkspaceUserRoles, uuid string, pubkey string) []db.WorkspaceUserRoles { ret := _m.Called(roles, uuid, pubkey) if len(ret) == 0 { panic("no return value specified for CreateUserRoles") } - var r0 []db.UserRoles - if rf, ok := ret.Get(0).(func([]db.UserRoles, string, string) []db.UserRoles); ok { + var r0 []db.WorkspaceUserRoles + if rf, ok := ret.Get(0).(func([]db.WorkspaceUserRoles, string, string) []db.WorkspaceUserRoles); ok { r0 = rf(roles, uuid, pubkey) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).([]db.UserRoles) + r0 = ret.Get(0).([]db.WorkspaceUserRoles) } } @@ -1121,26 +1121,26 @@ type Database_CreateUserRoles_Call struct { } // CreateUserRoles is a helper method to define mock.On call -// - roles []db.UserRoles +// - roles []db.WorkspaceUserRoles // - uuid string // - pubkey string func (_e *Database_Expecter) CreateUserRoles(roles interface{}, uuid interface{}, pubkey interface{}) *Database_CreateUserRoles_Call { return &Database_CreateUserRoles_Call{Call: _e.mock.On("CreateUserRoles", roles, uuid, pubkey)} } -func (_c *Database_CreateUserRoles_Call) Run(run func(roles []db.UserRoles, uuid string, pubkey string)) *Database_CreateUserRoles_Call { +func (_c *Database_CreateUserRoles_Call) Run(run func(roles []db.WorkspaceUserRoles, uuid string, pubkey string)) *Database_CreateUserRoles_Call { _c.Call.Run(func(args mock.Arguments) { - run(args[0].([]db.UserRoles), args[1].(string), args[2].(string)) + run(args[0].([]db.WorkspaceUserRoles), args[1].(string), args[2].(string)) }) return _c } -func (_c *Database_CreateUserRoles_Call) Return(_a0 []db.UserRoles) *Database_CreateUserRoles_Call { +func (_c *Database_CreateUserRoles_Call) Return(_a0 []db.WorkspaceUserRoles) *Database_CreateUserRoles_Call { _c.Call.Return(_a0) return _c } -func (_c *Database_CreateUserRoles_Call) RunAndReturn(run func([]db.UserRoles, string, string) []db.UserRoles) *Database_CreateUserRoles_Call { +func (_c *Database_CreateUserRoles_Call) RunAndReturn(run func([]db.WorkspaceUserRoles, string, string) []db.WorkspaceUserRoles) *Database_CreateUserRoles_Call { _c.Call.Return(run) return _c }