Migrate User.ClearAllCustomRoleAssignments to sync by default (#11506)
Этот коммит содержится в:
коммит произвёл
Jesús Espino
родитель
89d8dd6816
Коммит
4380c0b7a8
@@ -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,60 +1436,56 @@ 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)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
var transaction *gorp.Transaction
|
var transaction *gorp.Transaction
|
||||||
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 {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
lastUserId = user.Id
|
lastUserId = user.Id
|
||||||
|
|
||||||
var newRoles []string
|
var newRoles []string
|
||||||
|
|
||||||
for _, role := range strings.Fields(user.Roles) {
|
for _, role := range strings.Fields(user.Roles) {
|
||||||
for name := range builtInRoles {
|
for name := range builtInRoles {
|
||||||
if name == role {
|
if name == role {
|
||||||
newRoles = append(newRoles, role)
|
newRoles = append(newRoles, role)
|
||||||
break
|
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 {
|
|
||||||
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 {
|
newRolesString := strings.Join(newRoles, " ")
|
||||||
result.Err = model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
|
if newRolesString != user.Roles {
|
||||||
return
|
if _, err := transaction.Exec("UPDATE Users SET Roles = :Roles WHERE Id = :Id", map[string]interface{}{"Roles": newRolesString, "Id": user.Id}); err != nil {
|
||||||
|
return model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.update.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
|
||||||
|
if err := transaction.Commit(); err != nil {
|
||||||
|
return model.NewAppError("SqlUserStore.ClearAllCustomRoleAssignments", "store.sql_user.clear_all_custom_role_assignments.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user