feat: add roles to update profile

This commit is contained in:
Lakhan Samani
2021-09-19 00:04:54 +05:30
parent 2c903d0332
commit aec5d5a0c7
4 changed files with 30 additions and 2 deletions

View File

@@ -8,6 +8,7 @@ For the first version we will only support setting roles master list via env
- [x] `ROLES` -> comma separated list of role names
- [x] `DEFAULT_ROLE` -> default role to assign to users
- [x] Add roles input for signup
- [x] Add roles to update profile mutation
- [ ] Add roles input for login
- [ ] Return roles to user
- [ ] Return roles in users list for super admin

View File

@@ -35,7 +35,7 @@ func Signup(ctx context.Context, params model.SignUpInput) (*model.AuthResponse,
return res, fmt.Errorf(`invalid email address`)
}
if len(params.Roles) > 0 {
if params.Roles != nil && len(params.Roles) > 0 {
// check if roles exists
if !utils.IsValidRolesArray(params.Roles) {
return res, fmt.Errorf(`invalid roles`)

View File

@@ -39,7 +39,7 @@ func UpdateProfile(ctx context.Context, params model.UpdateProfileInput) (*model
}
// validate if all params are not empty
if params.FirstName == nil && params.LastName == nil && params.Image == nil && params.OldPassword == nil && params.Email == nil {
if params.FirstName == nil && params.LastName == nil && params.Image == nil && params.OldPassword == nil && params.Email == nil && params.Roles != nil {
return res, fmt.Errorf("please enter atleast one param to update")
}
@@ -120,7 +120,22 @@ func UpdateProfile(ctx context.Context, params model.UpdateProfileInput) (*model
go func() {
utils.SendVerificationMail(newEmail, token)
}()
}
rolesToSave := ""
if params.Roles != nil && len(params.Roles) > 0 {
currentRoles := strings.Split(user.Roles, ",")
inputRoles := []string{}
for _, item := range params.Roles {
inputRoles = append(inputRoles, *item)
}
if !utils.IsStringArrayEqual(inputRoles, currentRoles) && utils.IsValidRolesArray(params.Roles) {
rolesToSave = strings.Join(inputRoles, ",")
}
}
if rolesToSave != "" {
user.Roles = rolesToSave
}
_, err = db.Mgr.UpdateUser(user)

View File

@@ -55,3 +55,15 @@ func IsValidRolesArray(roles []*string) bool {
}
return valid
}
func IsStringArrayEqual(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}