diff options
Diffstat (limited to 'store/sqlstore/user_store.go')
-rw-r--r-- | store/sqlstore/user_store.go | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/store/sqlstore/user_store.go b/store/sqlstore/user_store.go index a695e4aa8..203ad4c26 100644 --- a/store/sqlstore/user_store.go +++ b/store/sqlstore/user_store.go @@ -10,6 +10,8 @@ import ( "strconv" "strings" + "github.com/mattermost/gorp" + "github.com/mattermost/mattermost-server/einterfaces" "github.com/mattermost/mattermost-server/model" "github.com/mattermost/mattermost-server/store" @@ -1247,3 +1249,70 @@ func (us SqlUserStore) GetEtagForProfilesNotInTeam(teamId string) store.StoreCha } }) } + +func (us SqlUserStore) ClearAllCustomRoleAssignments() store.StoreChannel { + return store.Do(func(result *store.StoreResult) { + builtInRoles := model.MakeDefaultRoles() + lastUserId := strings.Repeat("0", 26) + + for true { + var transaction *gorp.Transaction + var err error + + if transaction, err = us.GetMaster().Begin(); err != nil { + result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + var users []*model.User + if _, err := transaction.Select(&users, "SELECT * from Users WHERE Id > :Id ORDER BY Id LIMIT 1000", map[string]interface{}{"Id": lastUserId}); err != nil { + if err2 := transaction.Rollback(); err2 != nil { + result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.rollback_transaction.app_error", nil, err2.Error(), http.StatusInternalServerError) + return + } + result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.select.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + + if len(users) == 0 { + break + } + + for _, user := range users { + lastUserId = user.Id + + var newRoles []string + + for _, role := range strings.Fields(user.Roles) { + for name := range builtInRoles { + if name == role { + newRoles = append(newRoles, role) + break + } + } + } + + newRolesString := strings.Join(newRoles, " ") + if newRolesString != user.Roles { + if _, err := transaction.Exec("UPDATE Users SET Roles = :Roles WHERE Id = :Id", map[string]interface{}{"Roles": newRolesString, "Id": user.Id}); err != nil { + if err2 := transaction.Rollback(); err2 != nil { + result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.rollback_transaction.app_error", nil, err2.Error(), http.StatusInternalServerError) + return + } + result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.update.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + } + } + + if err := transaction.Commit(); err != nil { + if err2 := transaction.Rollback(); err2 != nil { + result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.rollback_transaction.app_error", nil, err2.Error(), http.StatusInternalServerError) + return + } + result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) + return + } + } + }) +} |