mirror of
https://github.com/go-gitea/gitea
synced 2025-07-05 01:57:20 +00:00
Add setting to disable user features when user login type is not plain (#29615)
## Changes - Adds setting `EXTERNAL_USER_DISABLE_FEATURES` to disable any supported user features when login type is not plain - In general, this is necessary for SSO implementations to avoid inconsistencies between the external account management and the linked account - Adds helper functions to encourage correct use
This commit is contained in:
@ -16,6 +16,7 @@ import (
|
||||
"code.gitea.io/gitea/models/unittest"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/modules/auth/password/hash"
|
||||
"code.gitea.io/gitea/modules/container"
|
||||
"code.gitea.io/gitea/modules/optional"
|
||||
"code.gitea.io/gitea/modules/setting"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
@ -526,3 +527,37 @@ func Test_NormalizeUserFromEmail(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDisabledUserFeatures(t *testing.T) {
|
||||
assert.NoError(t, unittest.PrepareTestDatabase())
|
||||
|
||||
testValues := container.SetOf(setting.UserFeatureDeletion,
|
||||
setting.UserFeatureManageSSHKeys,
|
||||
setting.UserFeatureManageGPGKeys)
|
||||
|
||||
oldSetting := setting.Admin.ExternalUserDisableFeatures
|
||||
defer func() {
|
||||
setting.Admin.ExternalUserDisableFeatures = oldSetting
|
||||
}()
|
||||
setting.Admin.ExternalUserDisableFeatures = testValues
|
||||
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
||||
|
||||
assert.Len(t, setting.Admin.UserDisabledFeatures.Values(), 0)
|
||||
|
||||
// no features should be disabled with a plain login type
|
||||
assert.LessOrEqual(t, user.LoginType, auth.Plain)
|
||||
assert.Len(t, user_model.DisabledFeaturesWithLoginType(user).Values(), 0)
|
||||
for _, f := range testValues.Values() {
|
||||
assert.False(t, user_model.IsFeatureDisabledWithLoginType(user, f))
|
||||
}
|
||||
|
||||
// check disabled features with external login type
|
||||
user.LoginType = auth.OAuth2
|
||||
|
||||
// all features should be disabled
|
||||
assert.NotEmpty(t, user_model.DisabledFeaturesWithLoginType(user).Values())
|
||||
for _, f := range testValues.Values() {
|
||||
assert.True(t, user_model.IsFeatureDisabledWithLoginType(user, f))
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user