|
7 | 7 | "fmt" |
8 | 8 | "reflect" |
9 | 9 | "strings" |
| 10 | + "time" |
10 | 11 |
|
11 | 12 | log "github.com/sirupsen/logrus" |
12 | 13 |
|
@@ -93,6 +94,53 @@ func clearSessionIfRequired(currentData, updatedData map[string]interface{}) { |
93 | 94 | } |
94 | 95 | } |
95 | 96 |
|
| 97 | +// updateRoles will update DB for user roles, if a role is deleted by admin |
| 98 | +// then this function will those roles from user roles if exists |
| 99 | +func updateRoles(ctx context.Context, deletedRoles []string) error { |
| 100 | + data, err := db.Provider.ListUsers(ctx, &model.Pagination{ |
| 101 | + Limit: 1, |
| 102 | + Offset: 1, |
| 103 | + }) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + allData, err := db.Provider.ListUsers(ctx, &model.Pagination{ |
| 109 | + Limit: data.Pagination.Total, |
| 110 | + }) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + chunkSize := 1000 |
| 116 | + totalUsers := len(allData.Users) |
| 117 | + |
| 118 | + for start := 0; start < totalUsers; start += chunkSize { |
| 119 | + end := start + chunkSize |
| 120 | + if end > totalUsers { |
| 121 | + end = totalUsers |
| 122 | + } |
| 123 | + |
| 124 | + chunkUsers := allData.Users[start:end] |
| 125 | + |
| 126 | + for i := range chunkUsers { |
| 127 | + roles := utils.DeleteFromArray(chunkUsers[i].Roles, deletedRoles) |
| 128 | + if len(chunkUsers[i].Roles) != len(roles) { |
| 129 | + updatedValues := map[string]interface{}{ |
| 130 | + "roles": strings.Join(roles, ","), |
| 131 | + "updated_at": time.Now().Unix(), |
| 132 | + } |
| 133 | + id := []string{chunkUsers[i].ID} |
| 134 | + err = db.Provider.UpdateUsers(ctx, updatedValues, id) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
96 | 144 | // UpdateEnvResolver is a resolver for update config mutation |
97 | 145 | // This is admin only mutation |
98 | 146 | func UpdateEnvResolver(ctx context.Context, params model.UpdateEnvInput) (*model.Response, error) { |
@@ -291,28 +339,41 @@ func UpdateEnvResolver(ctx context.Context, params model.UpdateEnvInput) (*model |
291 | 339 | }, nil) |
292 | 340 | } |
293 | 341 |
|
| 342 | + previousRoles := strings.Split(currentData[constants.EnvKeyRoles].(string), ",") |
| 343 | + previousProtectedRoles := strings.Split(currentData[constants.EnvKeyProtectedRoles].(string), ",") |
| 344 | + updatedRoles := strings.Split(updatedData[constants.EnvKeyRoles].(string), ",") |
| 345 | + updatedDefaultRoles := strings.Split(updatedData[constants.EnvKeyDefaultRoles].(string), ",") |
| 346 | + updatedProtectedRoles := strings.Split(updatedData[constants.EnvKeyProtectedRoles].(string), ",") |
294 | 347 | // check the roles change |
295 | | - if len(params.Roles) > 0 { |
296 | | - if len(params.DefaultRoles) > 0 { |
297 | | - // should be subset of roles |
298 | | - for _, role := range params.DefaultRoles { |
299 | | - if !utils.StringSliceContains(params.Roles, role) { |
300 | | - log.Debug("Default roles should be subset of roles") |
301 | | - return res, fmt.Errorf("default role %s is not in roles", role) |
302 | | - } |
| 348 | + if len(updatedRoles) > 0 && len(updatedDefaultRoles) > 0 { |
| 349 | + // should be subset of roles |
| 350 | + for _, role := range updatedDefaultRoles { |
| 351 | + if !utils.StringSliceContains(updatedRoles, role) { |
| 352 | + log.Debug("Default roles should be subset of roles") |
| 353 | + return res, fmt.Errorf("default role %s is not in roles", role) |
303 | 354 | } |
304 | 355 | } |
305 | 356 | } |
306 | 357 |
|
307 | | - if len(params.ProtectedRoles) > 0 { |
308 | | - for _, role := range params.ProtectedRoles { |
309 | | - if utils.StringSliceContains(params.Roles, role) || utils.StringSliceContains(params.DefaultRoles, role) { |
| 358 | + if len(updatedProtectedRoles) > 0 { |
| 359 | + for _, role := range updatedProtectedRoles { |
| 360 | + if utils.StringSliceContains(updatedRoles, role) || utils.StringSliceContains(updatedDefaultRoles, role) { |
310 | 361 | log.Debug("Protected roles should not be in roles or default roles") |
311 | 362 | return res, fmt.Errorf("protected role %s found roles or default roles", role) |
312 | 363 | } |
313 | 364 | } |
314 | 365 | } |
315 | 366 |
|
| 367 | + deletedRoles := utils.FindDeletedValues(previousRoles, updatedRoles) |
| 368 | + if len(deletedRoles) > 0 { |
| 369 | + go updateRoles(ctx, deletedRoles) |
| 370 | + } |
| 371 | + |
| 372 | + deletedProtectedRoles := utils.FindDeletedValues(previousProtectedRoles, updatedProtectedRoles) |
| 373 | + if len(deletedProtectedRoles) > 0 { |
| 374 | + go updateRoles(ctx, deletedProtectedRoles) |
| 375 | + } |
| 376 | + |
316 | 377 | // Update local store |
317 | 378 | memorystore.Provider.UpdateEnvStore(updatedData) |
318 | 379 | jwk, err := crypto.GenerateJWKBasedOnEnv() |
|
0 commit comments