Skip to content

Commit 5cb94a7

Browse files
fix:
* added logic if role is deleted then also be deleted from user side if role is assigned to that user. * default role should be subset of roles
1 parent a203b85 commit 5cb94a7

File tree

2 files changed

+94
-7
lines changed

2 files changed

+94
-7
lines changed

server/resolvers/update_env.go

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"reflect"
99
"strings"
10+
"time"
1011

1112
log "github.com/sirupsen/logrus"
1213

@@ -93,6 +94,42 @@ func clearSessionIfRequired(currentData, updatedData map[string]interface{}) {
9394
}
9495
}
9596

97+
func updateRoles(ctx context.Context, deletedRoles []string) error {
98+
data, err := db.Provider.ListUsers(ctx, &model.Pagination{
99+
Limit: 1,
100+
Offset: 1,
101+
})
102+
if err != nil {
103+
return err
104+
}
105+
106+
allData, err := db.Provider.ListUsers(ctx, &model.Pagination{
107+
Limit: data.Pagination.Total,
108+
})
109+
if err != nil {
110+
return err
111+
}
112+
113+
for i := range allData.Users {
114+
now := time.Now().Unix()
115+
allData.Users[i].Roles = utils.DeleteFromArray(allData.Users[i].Roles, deletedRoles)
116+
allData.Users[i].UpdatedAt = &now
117+
}
118+
119+
for i := range allData.Users {
120+
updatedValues := map[string]interface{}{
121+
"roles": strings.Join(allData.Users[i].Roles, ","),
122+
"updated_at": time.Now().Unix(),
123+
}
124+
id := []string{allData.Users[i].ID}
125+
err = db.Provider.UpdateUsers(ctx, updatedValues, id)
126+
if err != nil {
127+
return err
128+
}
129+
}
130+
return nil
131+
}
132+
96133
// UpdateEnvResolver is a resolver for update config mutation
97134
// This is admin only mutation
98135
func UpdateEnvResolver(ctx context.Context, params model.UpdateEnvInput) (*model.Response, error) {
@@ -291,28 +328,38 @@ func UpdateEnvResolver(ctx context.Context, params model.UpdateEnvInput) (*model
291328
}, nil)
292329
}
293330

331+
previousRoles := strings.Split(currentData[constants.EnvKeyRoles].(string), ",")
332+
updatedRoles := strings.Split(updatedData[constants.EnvKeyRoles].(string), ",")
333+
updatedDefaultRoles := strings.Split(updatedData[constants.EnvKeyDefaultRoles].(string), ",")
334+
updatedProtectedRoles := strings.Split(updatedData[constants.EnvKeyProtectedRoles].(string), ",")
335+
294336
// check the roles change
295-
if len(params.Roles) > 0 {
296-
if len(params.DefaultRoles) > 0 {
337+
if len(updatedRoles) > 0 {
338+
if len(updatedDefaultRoles) > 0 {
297339
// should be subset of roles
298-
for _, role := range params.DefaultRoles {
299-
if !utils.StringSliceContains(params.Roles, role) {
340+
for _, role := range updatedDefaultRoles {
341+
if !utils.StringSliceContains(updatedRoles, role) {
300342
log.Debug("Default roles should be subset of roles")
301343
return res, fmt.Errorf("default role %s is not in roles", role)
302344
}
303345
}
304346
}
305347
}
306348

307-
if len(params.ProtectedRoles) > 0 {
308-
for _, role := range params.ProtectedRoles {
309-
if utils.StringSliceContains(params.Roles, role) || utils.StringSliceContains(params.DefaultRoles, role) {
349+
if len(updatedProtectedRoles) > 0 {
350+
for _, role := range updatedProtectedRoles {
351+
if utils.StringSliceContains(updatedRoles, role) || utils.StringSliceContains(updatedDefaultRoles, role) {
310352
log.Debug("Protected roles should not be in roles or default roles")
311353
return res, fmt.Errorf("protected role %s found roles or default roles", role)
312354
}
313355
}
314356
}
315357

358+
deletedRoles := utils.FindDeletedValues(previousRoles, updatedRoles)
359+
if len(deletedRoles) > 0 {
360+
go updateRoles(ctx, deletedRoles)
361+
}
362+
316363
// Update local store
317364
memorystore.Provider.UpdateEnvStore(updatedData)
318365
jwk, err := crypto.GenerateJWKBasedOnEnv()

server/utils/common.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,43 @@ func GetInviteVerificationURL(verificationURL, token, redirectURI string) string
9595
func GetEmailVerificationURL(token, hostname, redirectURI string) string {
9696
return hostname + "/verify_email?token=" + token + "&redirect_uri=" + redirectURI
9797
}
98+
99+
// FindDeletedValues find deleted values between original and updated one
100+
func FindDeletedValues(original, updated []string) []string {
101+
deletedValues := make([]string, 0)
102+
103+
// Create a map to store elements of the updated array for faster lookups
104+
updatedMap := make(map[string]bool)
105+
for _, value := range updated {
106+
updatedMap[value] = true
107+
}
108+
109+
// Check for deleted values in the original array
110+
for _, value := range original {
111+
if _, found := updatedMap[value]; !found {
112+
deletedValues = append(deletedValues, value)
113+
}
114+
}
115+
116+
return deletedValues
117+
}
118+
119+
// DeleteFromArray will delete array from an array
120+
func DeleteFromArray(original, valuesToDelete []string) []string {
121+
result := make([]string, 0)
122+
123+
// Create a map to store values to delete for faster lookups
124+
valuesToDeleteMap := make(map[string]bool)
125+
for _, value := range valuesToDelete {
126+
valuesToDeleteMap[value] = true
127+
}
128+
129+
// Check if each element in the original array should be deleted
130+
for _, value := range original {
131+
if _, found := valuesToDeleteMap[value]; !found {
132+
result = append(result, value)
133+
}
134+
}
135+
136+
return result
137+
}

0 commit comments

Comments
 (0)