diff options
author | Lauris BH <lauris@nix.lv> | 2020-03-05 08:30:33 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-05 08:30:33 +0200 |
commit | 37c3db7be6dd6fc5ee085979cc5f5dda09d978c3 (patch) | |
tree | f77e1d61daaee11f6aab8e491120620e597c2782 /models | |
parent | be544e8e6a78360d87315ada9695cc6d70c3617c (diff) | |
download | gitea-37c3db7be6dd6fc5ee085979cc5f5dda09d978c3.tar.gz gitea-37c3db7be6dd6fc5ee085979cc5f5dda09d978c3.zip |
Add restricted user filter to LDAP authentication (#10600)
* Add restricted user filter to LDAP authentification
* Fix unit test cases
Diffstat (limited to 'models')
-rw-r--r-- | models/login_source.go | 43 | ||||
-rw-r--r-- | models/user.go | 26 |
2 files changed, 43 insertions, 26 deletions
diff --git a/models/login_source.go b/models/login_source.go index 2774d6f80d..88028283e8 100644 --- a/models/login_source.go +++ b/models/login_source.go @@ -475,13 +475,23 @@ func LoginViaLDAP(user *User, login, password string, source *LoginSource) (*Use return nil, err } } - if user != nil && - !user.ProhibitLogin && len(source.LDAP().AdminFilter) > 0 && user.IsAdmin != sr.IsAdmin { - // Change existing admin flag only if AdminFilter option is set - user.IsAdmin = sr.IsAdmin - err = UpdateUserCols(user, "is_admin") - if err != nil { - return nil, err + if user != nil && !user.ProhibitLogin { + cols := make([]string, 0) + if len(source.LDAP().AdminFilter) > 0 && user.IsAdmin != sr.IsAdmin { + // Change existing admin flag only if AdminFilter option is set + user.IsAdmin = sr.IsAdmin + cols = append(cols, "is_admin") + } + if !user.IsAdmin && len(source.LDAP().RestrictedFilter) > 0 && user.IsRestricted != sr.IsRestricted { + // Change existing restricted flag only if RestrictedFilter option is set + user.IsRestricted = sr.IsRestricted + cols = append(cols, "is_restricted") + } + if len(cols) > 0 { + err = UpdateUserCols(user, cols...) + if err != nil { + return nil, err + } } } } @@ -504,15 +514,16 @@ func LoginViaLDAP(user *User, login, password string, source *LoginSource) (*Use } user = &User{ - LowerName: strings.ToLower(sr.Username), - Name: sr.Username, - FullName: composeFullName(sr.Name, sr.Surname, sr.Username), - Email: sr.Mail, - LoginType: source.Type, - LoginSource: source.ID, - LoginName: login, - IsActive: true, - IsAdmin: sr.IsAdmin, + LowerName: strings.ToLower(sr.Username), + Name: sr.Username, + FullName: composeFullName(sr.Name, sr.Surname, sr.Username), + Email: sr.Mail, + LoginType: source.Type, + LoginSource: source.ID, + LoginName: login, + IsActive: true, + IsAdmin: sr.IsAdmin, + IsRestricted: sr.IsRestricted, } err := CreateUser(user) diff --git a/models/user.go b/models/user.go index f91ffa7169..06f11c968c 100644 --- a/models/user.go +++ b/models/user.go @@ -1875,15 +1875,16 @@ func SyncExternalUsers(ctx context.Context) { log.Trace("SyncExternalUsers[%s]: Creating user %s", s.Name, su.Username) usr = &User{ - LowerName: strings.ToLower(su.Username), - Name: su.Username, - FullName: fullName, - LoginType: s.Type, - LoginSource: s.ID, - LoginName: su.Username, - Email: su.Mail, - IsAdmin: su.IsAdmin, - IsActive: true, + LowerName: strings.ToLower(su.Username), + Name: su.Username, + FullName: fullName, + LoginType: s.Type, + LoginSource: s.ID, + LoginName: su.Username, + Email: su.Mail, + IsAdmin: su.IsAdmin, + IsRestricted: su.IsRestricted, + IsActive: true, } err = CreateUser(usr) @@ -1906,6 +1907,7 @@ func SyncExternalUsers(ctx context.Context) { // Check if user data has changed if (len(s.LDAP().AdminFilter) > 0 && usr.IsAdmin != su.IsAdmin) || + (len(s.LDAP().RestrictedFilter) > 0 && usr.IsRestricted != su.IsRestricted) || !strings.EqualFold(usr.Email, su.Mail) || usr.FullName != fullName || !usr.IsActive { @@ -1918,9 +1920,13 @@ func SyncExternalUsers(ctx context.Context) { if len(s.LDAP().AdminFilter) > 0 { usr.IsAdmin = su.IsAdmin } + // Change existing restricted flag only if RestrictedFilter option is set + if !usr.IsAdmin && len(s.LDAP().RestrictedFilter) > 0 { + usr.IsRestricted = su.IsRestricted + } usr.IsActive = true - err = UpdateUserCols(usr, "full_name", "email", "is_admin", "is_active") + err = UpdateUserCols(usr, "full_name", "email", "is_admin", "is_restricted", "is_active") if err != nil { log.Error("SyncExternalUsers[%s]: Error updating user %s: %v", s.Name, usr.Name, err) } |