[MM-62687] Patch permission check to avoid modifying the system admin (#30292)

* [MM-62687] Patch permission check to avoid modifying the system admin

* Check for manage system first

* PR feedback

* Add another test

* Lint

* Fix test
This commit is contained in:
Devin Binnie 2025-02-26 15:25:02 -05:00 committed by GitHub
parent a732962f0a
commit 9f49403d0a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 4 deletions

View file

@ -4237,6 +4237,14 @@ func TestSetDefaultProfileImage(t *testing.T) {
_, err = th.SystemAdminClient.SetDefaultProfileImage(context.Background(), user.Id)
require.NoError(t, err)
// Check that a system admin can set the default profile image for another system admin
anotherAdmin := th.CreateUser()
_, appErr := th.App.UpdateUserRoles(th.Context, anotherAdmin.Id, model.SystemAdminRoleId+" "+model.SystemUserRoleId, false)
require.Nil(t, appErr)
_, err = th.SystemAdminClient.SetDefaultProfileImage(context.Background(), anotherAdmin.Id)
require.NoError(t, err)
ruser, appErr := th.App.GetUser(user.Id)
require.Nil(t, appErr)
assert.Less(t, ruser.LastPictureUpdate, iuser.LastPictureUpdate, "LastPictureUpdate should be updated to a lower negative number")

View file

@ -202,7 +202,7 @@ func (a *App) SessionHasPermissionToUser(session model.Session, userID string) b
if userID == "" {
return false
}
if session.IsUnrestricted() {
if session.IsUnrestricted() || a.SessionHasPermissionTo(session, model.PermissionManageSystem) {
return true
}
@ -210,11 +210,20 @@ func (a *App) SessionHasPermissionToUser(session model.Session, userID string) b
return true
}
if a.SessionHasPermissionTo(session, model.PermissionEditOtherUsers) {
return true
if !a.SessionHasPermissionTo(session, model.PermissionEditOtherUsers) {
return false
}
return false
user, err := a.GetUser(userID)
if err != nil {
return false
}
if user.IsSystemAdmin() {
return false
}
return true
}
func (a *App) SessionHasPermissionToUserOrBot(rctx request.CTX, session model.Session, userID string) bool {

View file

@ -382,6 +382,7 @@ func TestSessionHasPermissionToUser(t *testing.T) {
th.AddPermissionToRole(model.PermissionEditOtherUsers.Id, model.SystemUserManagerRoleId)
assert.True(t, th.App.SessionHasPermissionToUser(session, th.BasicUser2.Id))
assert.False(t, th.App.SessionHasPermissionToUser(session, th.SystemAdminUser.Id))
th.RemovePermissionFromRole(model.PermissionEditOtherUsers.Id, model.SystemUserManagerRoleId)
bot, err := th.App.CreateBot(th.Context, &model.Bot{