-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add LDAP group sync to Teams, fixes #1395 #16299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wxiaoguang
merged 33 commits into
go-gitea:main
from
netzbegruenung:feature/ldap-group-sync
Feb 11, 2022
Merged
Changes from 14 commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
136c628
Add LDAP group sync to Teams, fixes #1395
svenseeberg 673df99
Add tests to LDAP group sync
melegiul 3a032cc
Replace funk package by custom utility
melegiul 6ef0722
Merge branch 'main' into feature/ldap-group-sync
melegiul 8339cf9
Clean up test database - revert initial
melegiul 76bb588
Skip adding team/org members when already member
melegiul edd19e2
Rename generic get keys from map function
melegiul ed0bab6
Merge branch 'main' into feature/ldap-group-sync
melegiul eda55b6
Merge branch 'main' into feature/ldap-group-sync
melegiul 5f6f092
Merge branch 'main' into feature/ldap-group-sync
melegiul ba93eb0
Improve non-idiomatic go code
melegiul 4d864b8
Add cache for teams and orgs
melegiul 564b59f
Merge branch 'main' into feature/ldap-group-sync
melegiul 1849924
Fix cli command flag and checkbox listener
melegiul 8865932
Merge branch 'main' into feature/ldap-group-sync
melegiul 6d21c2b
Set log level to warning for missing orgs/teams
melegiul f8d7a39
Remove redundant check remaining team memberships
melegiul c03bcb7
Fix integration tests
melegiul 675d64d
Disable group mapping checkbox on LDAP removal
melegiul 7f6d010
Merge branch 'main' into feature/ldap-group-sync
melegiul 9798db1
Merge branch 'main' into feature/ldap-group-sync
a75516d
Run make fmt
0d402cc
use kebap case for CSS classes
svenseeberg de1fd67
Merge branch 'main' into feature/ldap-group-sync
wxiaoguang 6ef197e
refactor
wxiaoguang 9563483
Merge pull request #4 from wxiaoguang/feature/ldap-group-sync
svenseeberg c965872
Merge branch 'main' into feature/ldap-group-sync
wxiaoguang d01e377
fix lint
wxiaoguang 82d0cb3
try to fix unit test
wxiaoguang dac97ff
Merge branch 'main' into feature/ldap-group-sync
6543 8f0b40a
fix unit test
wxiaoguang 25880d3
Merge branch 'main' into feature/ldap-group-sync
wxiaoguang f65f28f
Merge branch 'main' into feature/ldap-group-sync
wxiaoguang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// Copyright 2021 The Gitea Authors. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package ldap | ||
|
||
import ( | ||
"code.gitea.io/gitea/models" | ||
"code.gitea.io/gitea/modules/log" | ||
) | ||
|
||
// SyncLdapGroupsToTeams maps LDAP groups to organization and team memberships | ||
func (source *Source) SyncLdapGroupsToTeams(user *models.User, ldapTeamAdd map[string][]string, ldapTeamRemove map[string][]string, orgCache map[string]*models.User, teamCache map[string]*models.Team) { | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var err error | ||
if source.TeamGroupMapRemoval { | ||
// when the user is not a member of configs LDAP group, remove mapped organizations/teams memberships | ||
removeMappedMemberships(user, ldapTeamRemove, orgCache, teamCache) | ||
} | ||
wxiaoguang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for orgName, teamNames := range ldapTeamAdd { | ||
org, ok := orgCache[orgName] | ||
if !ok { | ||
org, err = models.GetOrgByName(orgName) | ||
if err != nil { | ||
// organization must be created before LDAP group sync | ||
log.Debug("LDAP group sync: Could not find organisation %s: %v", orgName, err) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
continue | ||
} | ||
orgCache[orgName] = org | ||
} | ||
if isMember, err := models.IsOrganizationMember(org.ID, user.ID); !isMember && err == nil { | ||
log.Trace("LDAP group sync: adding user [%s] to organization [%s]", user.Name, org.Name) | ||
err = org.AddMember(user.ID) | ||
if err != nil { | ||
log.Error("LDAP group sync: Could not add user to organization: %v", err) | ||
continue | ||
} | ||
} | ||
for _, teamName := range teamNames { | ||
team, ok := teamCache[orgName+teamName] | ||
if !ok { | ||
team, err = org.GetTeam(teamName) | ||
if err != nil { | ||
// team must be created before LDAP group sync | ||
log.Debug("LDAP group sync: Could not find team %s: %v", teamName, err) | ||
continue | ||
} | ||
teamCache[orgName+teamName] = team | ||
} | ||
if isMember, err := models.IsTeamMember(org.ID, team.ID, user.ID); !isMember && err == nil { | ||
log.Trace("LDAP group sync: adding user [%s] to team [%s]", user.Name, org.Name) | ||
} else { | ||
continue | ||
} | ||
err := team.AddMember(user.ID) | ||
if err != nil { | ||
log.Error("LDAP group sync: Could not add user to team: %v", err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// remove membership to organizations/teams if user is not member of corresponding LDAP group | ||
// e.g. lets assume user is member of LDAP group "x", but LDAP group team map contains LDAP groups "x" and "y" | ||
// then users membership gets removed for all organizations/teams mapped by LDAP group "y" | ||
func removeMappedMemberships(user *models.User, ldapTeamRemove map[string][]string, orgCache map[string]*models.User, teamCache map[string]*models.Team) { | ||
var err error | ||
for orgName, teamNames := range ldapTeamRemove { | ||
org, ok := orgCache[orgName] | ||
if !ok { | ||
org, err = models.GetOrgByName(orgName) | ||
if err != nil { | ||
// organization must be created before LDAP group sync | ||
log.Debug("LDAP group sync: Could not find organisation %s: %v", orgName, err) | ||
continue | ||
} | ||
orgCache[orgName] = org | ||
} | ||
for _, teamName := range teamNames { | ||
team, ok := teamCache[orgName+teamName] | ||
if !ok { | ||
team, err = org.GetTeam(teamName) | ||
if err != nil { | ||
// team must must be created before LDAP group sync | ||
log.Debug("LDAP group sync: Could not find team %s: %v", teamName, err) | ||
continue | ||
} | ||
} | ||
if isMember, err := models.IsTeamMember(org.ID, team.ID, user.ID); isMember && err == nil { | ||
log.Trace("LDAP group sync: removing user [%s] from team [%s]", user.Name, org.Name) | ||
} else { | ||
continue | ||
} | ||
err = team.RemoveMember(user.ID) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
log.Error("LDAP group sync: Could not remove user from team: %v", err) | ||
} | ||
} | ||
if remainingTeams, err := models.GetUserOrgTeams(org.ID, user.ID); err == nil && len(remainingTeams) == 0 { | ||
// only remove organization membership when no team memberships are left for this organization | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if isMember, err := models.IsOrganizationMember(org.ID, user.ID); isMember && err == nil { | ||
log.Trace("LDAP group sync: removing user [%s] from organization [%s]", user.Name, org.Name) | ||
} else { | ||
continue | ||
} | ||
err = org.RemoveMember(user.ID) | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
log.Error("LDAP group sync: Could not remove user from organization: %v", err) | ||
} | ||
} else if err != nil { | ||
log.Error("LDAP group sync: Could not find users [id: %d] teams for given organization [%s]", user.ID, org.Name) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One slight fly in the ointment here is that the login_source will be read and loaded from the source quite a lot - it's infact in the DB as a JSON where it gets unmarshaled on load.