Skip to content

Commit

Permalink
user roles and groups migration
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishek9686 committed Oct 31, 2024
1 parent 1b035d9 commit 4b99435
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 11 deletions.
3 changes: 3 additions & 0 deletions controllers/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ func userMiddleWare(handler http.Handler) http.Handler {
if strings.Contains(route, "acls") {
r.Header.Set("TARGET_RSRC", models.AclRsrc.String())
}
if strings.Contains(route, "tags") {
r.Header.Set("TARGET_RSRC", models.TagRsrc.String())
}
if strings.Contains(route, "extclients") {
r.Header.Set("TARGET_RSRC", models.ExtClientsRsrc.String())
}
Expand Down
2 changes: 1 addition & 1 deletion logic/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func CreateUser(user *models.User) error {
logger.Log(0, "failed to insert user", err.Error())
return err
}

AddGlobalNetRolesToAdmins(*user)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion logic/user_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var DeleteNetworkRoles = func(netID string) {}
var CreateDefaultNetworkRolesAndGroups = func(netID models.NetworkID) {}
var CreateDefaultUserPolicies = func(netID models.NetworkID) {}
var GetUserGroupsInNetwork = func(netID models.NetworkID) (networkGrps map[models.UserGroupID]models.UserGroup) { return }
var AddGlobalNetRolesToAdmins = func(u *models.User) {}
var AddGlobalNetRolesToAdmins = func(u models.User) {}

// GetRole - fetches role template by id
func GetRole(roleID models.UserRoleID) (models.UserRolePermissionTemplate, error) {
Expand Down
1 change: 0 additions & 1 deletion logic/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func SetUserDefaults(user *models.User) {
if len(user.UserGroups) == 0 {
user.UserGroups = make(map[models.UserGroupID]struct{})
}
AddGlobalNetRolesToAdmins(user)
}

// SortUsers - Sorts slice of Users by username
Expand Down
5 changes: 2 additions & 3 deletions migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ func syncUsers() {
if err == nil {
for _, user := range users {
user := user
logic.AddGlobalNetRolesToAdmins(&user)
logic.UpsertUser(user)
logic.AddGlobalNetRolesToAdmins(user)
if user.PlatformRoleID == models.AdminRole && !user.IsAdmin {
user.IsAdmin = true
logic.UpsertUser(user)
Expand All @@ -409,6 +408,7 @@ func syncUsers() {
logic.UpsertUser(user)
}
if user.PlatformRoleID.String() != "" {
logic.MigrateUserRoleAndGroups(user)
continue
}
user.AuthType = models.BasicAuth
Expand All @@ -431,7 +431,6 @@ func syncUsers() {
}
logic.UpsertUser(user)
logic.MigrateUserRoleAndGroups(user)

}
}

Expand Down
1 change: 1 addition & 0 deletions models/user_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const (
EnrollmentKeysRsrc RsrcType = "enrollment_key"
UserRsrc RsrcType = "users"
AclRsrc RsrcType = "acl"
TagRsrc RsrcType = "tag"
DnsRsrc RsrcType = "dns"
FailOverRsrc RsrcType = "fail_over"
MetricRsrc RsrcType = "metrics"
Expand Down
5 changes: 1 addition & 4 deletions pro/logic/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func MigrateUserRoleAndGroups(user models.User) {
continue
}
user.UserGroups[g.ID] = struct{}{}

}
}
if len(user.NetworkRoles) > 0 {
Expand All @@ -44,9 +43,7 @@ func MigrateUserRoleAndGroups(user models.User) {
continue
}
user.UserGroups[g.ID] = struct{}{}
if err != nil {
continue
}
user.NetworkRoles = make(map[models.NetworkID]map[models.UserRoleID]struct{})
}

}
Expand Down
3 changes: 2 additions & 1 deletion pro/logic/user_mgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1221,12 +1221,13 @@ func GetUserGroupsInNetwork(netID models.NetworkID) (networkGrps map[models.User
return
}

func AddGlobalNetRolesToAdmins(u *models.User) {
func AddGlobalNetRolesToAdmins(u models.User) {
if u.PlatformRoleID != models.SuperAdminRole && u.PlatformRoleID != models.AdminRole {
return
}
if u.UserGroups == nil {
u.UserGroups = make(map[models.UserGroupID]struct{})
}
u.UserGroups[models.UserGroupID(fmt.Sprintf("global-%s-grp", models.NetworkAdmin))] = struct{}{}
logic.UpsertUser(u)
}

0 comments on commit 4b99435

Please sign in to comment.