Skip to content

Commit f1a20ea

Browse files
committed
Merge branch 'felazuris-fixpermdisplay' into master
This pulls in the rebased branch from PR #1100.
2 parents f0dce60 + f2fc9cc commit f1a20ea

File tree

2 files changed

+59
-4
lines changed

2 files changed

+59
-4
lines changed

src/main/java/com/gitblit/models/UserModel.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public boolean isLocalAccount() {
110110
* @return the user's list of permissions
111111
*/
112112
public List<RegistrantAccessPermission> getRepositoryPermissions() {
113-
List<RegistrantAccessPermission> list = new ArrayList<RegistrantAccessPermission>();
113+
List<RegistrantAccessPermission> list = new ArrayList<>();
114114
if (canAdmin()) {
115115
// user has REWIND access to all repositories
116116
return list;
@@ -135,18 +135,24 @@ public List<RegistrantAccessPermission> getRepositoryPermissions() {
135135
Collections.sort(list);
136136

137137
// include immutable team permissions, being careful to preserve order
138-
Set<RegistrantAccessPermission> set = new LinkedHashSet<RegistrantAccessPermission>(list);
139138
for (TeamModel team : teams) {
140139
for (RegistrantAccessPermission teamPermission : team.getRepositoryPermissions()) {
141140
// we can not change an inherited team permission, though we can override
142141
teamPermission.registrantType = RegistrantType.REPOSITORY;
143142
teamPermission.permissionType = PermissionType.TEAM;
144143
teamPermission.source = team.name;
145144
teamPermission.mutable = false;
146-
set.add(teamPermission);
145+
int i = list.indexOf(teamPermission);
146+
if (i < 0) list.add(teamPermission);
147+
else {
148+
RegistrantAccessPermission lp = list.get(i);
149+
if (teamPermission.permission.exceeds(lp.permission)) {
150+
list.set(i, teamPermission);
151+
}
152+
}
147153
}
148154
}
149-
return new ArrayList<RegistrantAccessPermission>(set);
155+
return list;
150156
}
151157

152158
/**

src/test/java/com/gitblit/tests/UserModelTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
*/
1616
package com.gitblit.tests;
1717

18+
import com.gitblit.Constants;
19+
import com.gitblit.models.RegistrantAccessPermission;
20+
import com.gitblit.models.TeamModel;
1821
import org.junit.Test;
1922

2023
import com.gitblit.models.UserModel;
2124

25+
import java.util.List;
26+
2227
/**
2328
* @author Alfred Schmid
2429
*
@@ -49,4 +54,48 @@ public void whenDisplayNameIsNotEmptyDisplayNameIsUsed() {
4954
assertEquals("When displayName is not empty its value has to be returnd from getDisplayName().", displayName, userModel.getDisplayName());
5055
}
5156

57+
58+
@Test
59+
public void getRepositoryPermissionsMultipleTeams()
60+
{
61+
62+
TeamModel aTeam = new TeamModel("A team");
63+
aTeam.addRepositoryPermission("RW+:acerepo.git");
64+
aTeam.addRepositoryPermission("V:boobrepo.git");
65+
66+
TeamModel bTeam = new TeamModel("Team B");
67+
bTeam.addRepositoryPermission("R:acerepo.git");
68+
bTeam.addRepositoryPermission("RWC:boobrepo.git");
69+
70+
UserModel user = new UserModel("tessiur");
71+
user.teams.add(aTeam);
72+
user.teams.add(bTeam);
73+
user.addRepositoryPermission("RW+:myrepo.git");
74+
75+
List<RegistrantAccessPermission> repoPerms = user.getRepositoryPermissions();
76+
int found = 0;
77+
for (RegistrantAccessPermission p : repoPerms) {
78+
switch (p.registrant) {
79+
case "acerepo.git":
80+
assertEquals("Expected REWIND(RW+) permission for " + p.registrant, Constants.AccessPermission.REWIND, p.permission);
81+
found++;
82+
break;
83+
case "boobrepo.git":
84+
assertEquals("Expected CREATE(RWC) permission for " + p.registrant, Constants.AccessPermission.CREATE, p.permission);
85+
found++;
86+
break;
87+
case "myrepo.git":
88+
assertEquals("Expected REWIND(RW+) permission for " + p.registrant, Constants.AccessPermission.REWIND, p.permission);
89+
found++;
90+
break;
91+
default:
92+
fail("Unknown repository registrant " + p.registrant);
93+
break;
94+
}
95+
}
96+
97+
assertEquals("Repostory permissions missing in list.", 3, found);
98+
99+
}
100+
52101
}

0 commit comments

Comments
 (0)