forked from forgejo/forgejo
Backport #25278 by @Zettat123
Fix #21072

Username Attribute is not a required item when creating an
authentication source. If Username Attribute is empty, the username
value of LDAP user cannot be read, so all users from LDAP will be marked
as inactive by mistake when synchronizing external users.
This PR improves the sync logic, if username is empty, the email address
will be used to find user.
Co-authored-by: Zettat123 <zettat123@gmail.com>
This commit is contained in:
parent
10fcb55507
commit
dfefe86045
2 changed files with 84 additions and 31 deletions
|
@ -268,6 +268,57 @@ func TestLDAPUserSync(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestLDAPUserSyncWithEmptyUsernameAttribute(t *testing.T) {
|
||||
if skipLDAPTests() {
|
||||
t.Skip()
|
||||
return
|
||||
}
|
||||
defer tests.PrepareTestEnv(t)()
|
||||
|
||||
session := loginUser(t, "user1")
|
||||
csrf := GetCSRF(t, session, "/admin/auths/new")
|
||||
payload := buildAuthSourceLDAPPayload(csrf, "", "", "", "")
|
||||
payload["attribute_username"] = ""
|
||||
req := NewRequestWithValues(t, "POST", "/admin/auths/new", payload)
|
||||
session.MakeRequest(t, req, http.StatusSeeOther)
|
||||
|
||||
for _, u := range gitLDAPUsers {
|
||||
req := NewRequest(t, "GET", "/admin/users?q="+u.UserName)
|
||||
resp := session.MakeRequest(t, req, http.StatusOK)
|
||||
|
||||
htmlDoc := NewHTMLParser(t, resp.Body)
|
||||
|
||||
tr := htmlDoc.doc.Find("table.table tbody tr")
|
||||
assert.True(t, tr.Length() == 0)
|
||||
}
|
||||
|
||||
for _, u := range gitLDAPUsers {
|
||||
req := NewRequestWithValues(t, "POST", "/user/login", map[string]string{
|
||||
"_csrf": csrf,
|
||||
"user_name": u.UserName,
|
||||
"password": u.Password,
|
||||
})
|
||||
MakeRequest(t, req, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
auth.SyncExternalUsers(context.Background(), true)
|
||||
|
||||
authSource := unittest.AssertExistsAndLoadBean(t, &auth_model.Source{
|
||||
Name: payload["name"],
|
||||
})
|
||||
unittest.AssertCount(t, &user_model.User{
|
||||
LoginType: auth_model.LDAP,
|
||||
LoginSource: authSource.ID,
|
||||
}, len(gitLDAPUsers))
|
||||
|
||||
for _, u := range gitLDAPUsers {
|
||||
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{
|
||||
Name: u.UserName,
|
||||
})
|
||||
assert.True(t, user.IsActive)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLDAPUserSyncWithGroupFilter(t *testing.T) {
|
||||
if skipLDAPTests() {
|
||||
t.Skip()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue