Migrate User.ClearAllCustomRoleAssignments to sync by default (#11506)

Этот коммит содержится в:
Rodrigo Villablanca Vásquez
2019-07-07 13:50:18 -04:00
коммит произвёл Jesús Espino
родитель 89d8dd6816
Коммит 4380c0b7a8
5 изменённых файлов: 45 добавлений и 49 удалений

Просмотреть файл

@@ -28,8 +28,8 @@ func (a *App) ResetPermissionsSystem() *model.AppError {
} }
// Reset all Custom Role assignments to Users. // Reset all Custom Role assignments to Users.
if result := <-a.Srv.Store.User().ClearAllCustomRoleAssignments(); result.Err != nil { if err := a.Srv.Store.User().ClearAllCustomRoleAssignments(); err != nil {
return result.Err return err
} }
// Reset all Custom Role assignments to TeamMembers. // Reset all Custom Role assignments to TeamMembers.

Просмотреть файл

@@ -1436,8 +1436,7 @@ func (us SqlUserStore) GetEtagForProfilesNotInTeam(teamId string) store.StoreCha
}) })
} }
func (us SqlUserStore) ClearAllCustomRoleAssignments() store.StoreChannel { func (us SqlUserStore) ClearAllCustomRoleAssignments() *model.AppError {
return store.Do(func(result *store.StoreResult) {
builtInRoles := model.MakeDefaultRoles() builtInRoles := model.MakeDefaultRoles()
lastUserId := strings.Repeat("0", 26) lastUserId := strings.Repeat("0", 26)
@@ -1446,15 +1445,13 @@ func (us SqlUserStore) ClearAllCustomRoleAssignments() store.StoreChannel {
var err error var err error
if transaction, err = us.GetMaster().Begin(); err != nil { 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 model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
defer finalizeTransaction(transaction) defer finalizeTransaction(transaction)
var users []*model.User 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 _, err := transaction.Select(&users, "SELECT * from Users WHERE Id > :Id ORDER BY Id LIMIT 1000", map[string]interface{}{"Id": lastUserId}); err != nil {
result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.select.app_error", nil, err.Error(), http.StatusInternalServerError) return 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 { if len(users) == 0 {
@@ -1478,18 +1475,17 @@ func (us SqlUserStore) ClearAllCustomRoleAssignments() store.StoreChannel {
newRolesString := strings.Join(newRoles, " ") newRolesString := strings.Join(newRoles, " ")
if newRolesString != user.Roles { 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 _, err := transaction.Exec("UPDATE Users SET Roles = :Roles WHERE Id = :Id", map[string]interface{}{"Roles": newRolesString, "Id": user.Id}); err != nil {
result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.update.app_error", nil, err.Error(), http.StatusInternalServerError) return 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 err := transaction.Commit(); err != nil {
result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) return model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
return
} }
} }
})
return nil
} }
func (us SqlUserStore) InferSystemInstallDate() store.StoreChannel { func (us SqlUserStore) InferSystemInstallDate() store.StoreChannel {

Просмотреть файл

@@ -300,7 +300,7 @@ type UserStore interface {
AnalyticsGetSystemAdminCount() (int64, *model.AppError) AnalyticsGetSystemAdminCount() (int64, *model.AppError)
GetProfilesNotInTeam(teamId string, groupConstrained bool, offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError) GetProfilesNotInTeam(teamId string, groupConstrained bool, offset int, limit int, viewRestrictions *model.ViewUsersRestrictions) ([]*model.User, *model.AppError)
GetEtagForProfilesNotInTeam(teamId string) StoreChannel GetEtagForProfilesNotInTeam(teamId string) StoreChannel
ClearAllCustomRoleAssignments() StoreChannel ClearAllCustomRoleAssignments() *model.AppError
InferSystemInstallDate() StoreChannel InferSystemInstallDate() StoreChannel
GetAllAfter(limit int, afterId string) ([]*model.User, *model.AppError) GetAllAfter(limit int, afterId string) ([]*model.User, *model.AppError)
GetUsersBatchForIndexing(startTime, endTime int64, limit int) ([]*model.UserForIndexing, *model.AppError) GetUsersBatchForIndexing(startTime, endTime int64, limit int) ([]*model.UserForIndexing, *model.AppError)

Просмотреть файл

@@ -83,15 +83,15 @@ func (_m *UserStore) AnalyticsGetSystemAdminCount() (int64, *model.AppError) {
} }
// ClearAllCustomRoleAssignments provides a mock function with given fields: // ClearAllCustomRoleAssignments provides a mock function with given fields:
func (_m *UserStore) ClearAllCustomRoleAssignments() store.StoreChannel { func (_m *UserStore) ClearAllCustomRoleAssignments() *model.AppError {
ret := _m.Called() ret := _m.Called()
var r0 store.StoreChannel var r0 *model.AppError
if rf, ok := ret.Get(0).(func() store.StoreChannel); ok { if rf, ok := ret.Get(0).(func() *model.AppError); ok {
r0 = rf() r0 = rf()
} else { } else {
if ret.Get(0) != nil { if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel) r0 = ret.Get(0).(*model.AppError)
} }
} }

Просмотреть файл

@@ -3506,7 +3506,7 @@ func testUserStoreClearAllCustomRoleAssignments(t *testing.T, ss store.Store) {
store.Must(ss.User().Save(&u4)) store.Must(ss.User().Save(&u4))
defer func() { require.Nil(t, ss.User().PermanentDelete(u4.Id)) }() defer func() { require.Nil(t, ss.User().PermanentDelete(u4.Id)) }()
require.Nil(t, (<-ss.User().ClearAllCustomRoleAssignments()).Err) require.Nil(t, ss.User().ClearAllCustomRoleAssignments())
r1 := <-ss.User().GetByUsername(u1.Username) r1 := <-ss.User().GetByUsername(u1.Username)
require.Nil(t, r1.Err) require.Nil(t, r1.Err)