Skip to content

Commit

Permalink
fixed user roles migration
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Apr 22, 2024
1 parent 9e0e8ff commit 326d13e
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 37 deletions.
12 changes: 7 additions & 5 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{})) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
32 changes: 16 additions & 16 deletions db/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -45,7 +45,7 @@ func TestRolesCheck_UserDoesNotHaveRole(t *testing.T) {
}

func TestCheckUser(t *testing.T) {
userRoles := []UserRoles{
userRoles := []WorkspaceUserRoles{
{OwnerPubKey: "userPublicKey"},
}

Expand All @@ -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{}},
}
}

Expand Down Expand Up @@ -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{}},
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion db/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
8 changes: 4 additions & 4 deletions db/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions handlers/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions mocks/Database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 326d13e

Please sign in to comment.