Skip to content

Set "can admin" permission on LDAP users and teams correctly #1152

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
merged 1 commit into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions src/main/java/com/gitblit/auth/LdapAuthProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ public synchronized void sync() {
final Map<String, TeamModel> userTeams = new HashMap<String, TeamModel>();
for (UserModel user : ldapUsers.values()) {
for (TeamModel userTeam : user.teams) {
// Is this an administrative team?
setAdminAttribute(userTeam);
userTeams.put(userTeam.name, userTeam);
}
}
Expand Down Expand Up @@ -238,10 +240,7 @@ public boolean supportsTeamMembershipChanges() {
public boolean supportsRoleChanges(UserModel user, Role role) {
if (Role.ADMIN == role) {
if (!supportsTeamMembershipChanges()) {
List<String> admins = settings.getStrings(Keys.realm.ldap.admins);
if (admins.contains(user.username)) {
return false;
}
return false;
}
}
return true;
Expand All @@ -251,10 +250,7 @@ public boolean supportsRoleChanges(UserModel user, Role role) {
public boolean supportsRoleChanges(TeamModel team, Role role) {
if (Role.ADMIN == role) {
if (!supportsTeamMembershipChanges()) {
List<String> admins = settings.getStrings(Keys.realm.ldap.admins);
if (admins.contains("@" + team.name)) {
return false;
}
return false;
}
}
return true;
Expand Down Expand Up @@ -325,6 +321,8 @@ public UserModel authenticate(String username, char[] password) {

if (!supportsTeamMembershipChanges()) {
for (TeamModel userTeam : user.teams) {
// Is this an administrative team?
setAdminAttribute(userTeam);
updateTeam(userTeam);
}
}
Expand Down Expand Up @@ -355,10 +353,7 @@ private void setAdminAttribute(UserModel user) {
if (!ArrayUtils.isEmpty(admins)) {
user.canAdmin = false;
for (String admin : admins) {
if (admin.startsWith("@") && user.isTeamMember(admin.substring(1))) {
// admin team
user.canAdmin = true;
} else if (user.getName().equalsIgnoreCase(admin)) {
if (user.getName().equalsIgnoreCase(admin)) {
// admin user
user.canAdmin = true;
}
Expand All @@ -367,6 +362,30 @@ private void setAdminAttribute(UserModel user) {
}
}

/**
* Set the canAdmin attribute for team retrieved from LDAP.
* If we are not storing teams in LDAP and/or we have not defined any
* administrator teams, then do not change the admin flag.
*
* @param team
*/
private void setAdminAttribute(TeamModel team) {
if (!supportsTeamMembershipChanges()) {
List<String> admins = settings.getStrings(Keys.realm.ldap.admins);
// if we have defined administrative teams, then set admin flag
// otherwise leave admin flag unchanged
if (!ArrayUtils.isEmpty(admins)) {
team.canAdmin = false;
for (String admin : admins) {
if (admin.startsWith("@") && team.name.equalsIgnoreCase(admin.substring(1))) {
// admin team
team.canAdmin = true;
}
}
}
}
}

private void setUserAttributes(UserModel user, SearchResultEntry userEntry) {
// Is this user an admin?
setAdminAttribute(user);
Expand Down Expand Up @@ -462,6 +481,7 @@ private void getEmptyTeamsFromLdap(LdapConnection ldapConnection) {
TeamModel teamModel = userManager.getTeamModel(teamName);
if (teamModel == null) {
teamModel = createTeamFromLdap(teamEntry);
setAdminAttribute(teamModel);
userManager.updateTeamModel(teamModel);
}
}
Expand Down
Loading