Skip to content
This repository was archived by the owner on Jun 9, 2021. It is now read-only.

Commit 474e1fd

Browse files
committed
Getting project and repo with sys admin permissions #135
1 parent ae65c27 commit 474e1fd

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
Changelog of Pull Request Notifier for Bitbucket.
44

55
## Unreleased
6+
### GitHub [#135](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/135) Not permitted to access buttons settings as project/repo admin
7+
Getting project and repo with sys admin permissions
8+
9+
[4af3fd9f87fa409](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/4af3fd9f87fa409) Tomas Bjerre *2016-07-30 06:42:38*
10+
11+
## 2.32
612
### No issue
713
Fine tuning notification confirmation feature
814
* Using AUI flag.
915
* Showing invoked URL and response content.
1016
* Also logging error when variable cant be resolved. Was giving up entirely. Will now log and continue trying to resolve other variables.
1117

12-
[cd772334597de1f](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/cd772334597de1f) Tomas Bjerre *2016-07-29 22:25:58*
18+
[aee524c305eb666](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/aee524c305eb666) Tomas Bjerre *2016-07-29 22:47:15*
1319

1420
Add Button Trigger Confirmation Dialog
1521

src/main/java/se/bjurr/prnfb/service/UserCheckService.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import static com.atlassian.bitbucket.permission.Permission.PROJECT_ADMIN;
44
import static com.atlassian.bitbucket.permission.Permission.REPO_ADMIN;
5+
import static com.atlassian.bitbucket.permission.Permission.SYS_ADMIN;
56
import static com.google.common.base.Strings.emptyToNull;
7+
import static com.google.common.base.Throwables.propagate;
68
import static com.google.common.collect.Iterables.filter;
79
import static se.bjurr.prnfb.settings.USER_LEVEL.ADMIN;
810
import static se.bjurr.prnfb.settings.USER_LEVEL.EVERYONE;
@@ -19,25 +21,30 @@
1921
import com.atlassian.bitbucket.project.ProjectService;
2022
import com.atlassian.bitbucket.repository.Repository;
2123
import com.atlassian.bitbucket.repository.RepositoryService;
24+
import com.atlassian.bitbucket.user.SecurityService;
25+
import com.atlassian.bitbucket.util.Operation;
2226
import com.atlassian.sal.api.user.UserKey;
2327
import com.atlassian.sal.api.user.UserManager;
2428
import com.atlassian.sal.api.user.UserProfile;
29+
import com.google.common.annotations.VisibleForTesting;
2530
import com.google.common.base.Predicate;
2631

2732
public class UserCheckService {
2833
private final PermissionService permissionService;
2934
private final ProjectService projectService;
3035
private final RepositoryService repositoryService;
36+
private final SecurityService securityService;
3137
private final SettingsService settingsService;
3238
private final UserManager userManager;
3339

3440
public UserCheckService(PermissionService permissionService, UserManager userManager, SettingsService settingsService,
35-
RepositoryService repositoryService, ProjectService projectService) {
41+
RepositoryService repositoryService, ProjectService projectService, SecurityService securityService) {
3642
this.userManager = userManager;
3743
this.settingsService = settingsService;
3844
this.permissionService = permissionService;
3945
this.projectService = projectService;
4046
this.repositoryService = repositoryService;
47+
this.securityService = securityService;
4148
}
4249

4350
public Iterable<PrnfbButton> filterAllowed(List<PrnfbButton> buttons) {
@@ -60,10 +67,10 @@ public boolean isAdmin(UserKey userKey, String projectKey, String repositorySlug
6067
repositorySlug = emptyToNull(repositorySlug);
6168

6269
if (projectKey != null && repositorySlug == null) {
63-
Project project = this.projectService.getByKey(projectKey);
70+
Project project = getProject(projectKey);
6471
return this.permissionService.hasProjectPermission(project, PROJECT_ADMIN);
6572
} else if (repositorySlug != null) {
66-
Repository repository = this.repositoryService.getBySlug(projectKey, repositorySlug);
73+
Repository repository = getRepo(projectKey, repositorySlug);
6774
return this.permissionService.hasRepositoryPermission(repository, REPO_ADMIN);
6875
}
6976
return isAdmin;
@@ -100,6 +107,22 @@ public boolean isViewAllowed() {
100107
return true;
101108
}
102109

110+
@VisibleForTesting
111+
private Project getProject(String projectKey) {
112+
try {
113+
return this.securityService//
114+
.withPermission(SYS_ADMIN, "Getting project")//
115+
.call(new Operation<Project, Exception>() {
116+
@Override
117+
public Project perform() throws Exception {
118+
return UserCheckService.this.projectService.getByKey(projectKey);
119+
}
120+
});
121+
} catch (Exception e) {
122+
throw propagate(e);
123+
}
124+
}
125+
103126
private boolean isAdminAllowed(USER_LEVEL adminRestriction, @Nullable String projectKey,
104127
@Nullable String repositorySlug) {
105128
UserKey userKey = this.userManager.getRemoteUser().getUserKey();
@@ -108,6 +131,22 @@ private boolean isAdminAllowed(USER_LEVEL adminRestriction, @Nullable String pro
108131
return isAdminAllowedCheck(adminRestriction, isAdmin, isSystemAdmin);
109132
}
110133

134+
@VisibleForTesting
135+
Repository getRepo(String projectKey, String repositorySlug) {
136+
try {
137+
return this.securityService//
138+
.withPermission(SYS_ADMIN, "Getting repo")//
139+
.call(new Operation<Repository, Exception>() {
140+
@Override
141+
public Repository perform() throws Exception {
142+
return UserCheckService.this.repositoryService.getBySlug(projectKey, repositorySlug);
143+
}
144+
});
145+
} catch (Exception e) {
146+
throw propagate(e);
147+
}
148+
}
149+
111150
boolean isAdminAllowedCheck(USER_LEVEL userLevel, boolean isAdmin, boolean isSystemAdmin) {
112151
return userLevel == EVERYONE //
113152
|| isSystemAdmin //

src/test/java/se/bjurr/prnfb/service/UserCheckServiceTest.java

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,39 @@
1212

1313
import org.junit.Before;
1414
import org.junit.Test;
15+
import org.mockito.Matchers;
1516
import org.mockito.Mock;
17+
import org.mockito.invocation.InvocationOnMock;
18+
import org.mockito.stubbing.Answer;
1619

1720
import se.bjurr.prnfb.presentation.dto.ON_OR_OFF;
1821
import se.bjurr.prnfb.settings.PrnfbButton;
1922

2023
import com.atlassian.bitbucket.permission.PermissionService;
2124
import com.atlassian.bitbucket.project.ProjectService;
2225
import com.atlassian.bitbucket.repository.RepositoryService;
26+
import com.atlassian.bitbucket.user.EscalatedSecurityContext;
27+
import com.atlassian.bitbucket.user.SecurityService;
28+
import com.atlassian.bitbucket.util.Operation;
2329
import com.atlassian.sal.api.user.UserKey;
2430
import com.atlassian.sal.api.user.UserManager;
2531
import com.atlassian.sal.api.user.UserProfile;
2632

2733
public class UserCheckServiceTest {
2834

35+
@Mock
36+
private EscalatedSecurityContext escalatedSecurityContext;
37+
2938
@Mock
3039
private PermissionService permissionService;
40+
private String projectKey;
3141
@Mock
3242
private ProjectService projectService;
3343
@Mock
3444
private RepositoryService repositoryService;
45+
private String repositorySlug;
46+
@Mock
47+
private SecurityService securityService;
3548
@Mock
3649
private SettingsService settingsService;
3750
private UserCheckService sut;
@@ -42,19 +55,35 @@ public class UserCheckServiceTest {
4255
private UserManager userManager;
4356

4457
@Before
45-
public void before() {
58+
public void before() throws Exception {
4659
initMocks(this);
4760
this.sut = new UserCheckService(this.permissionService, this.userManager, this.settingsService,
48-
this.repositoryService, this.projectService);
61+
this.repositoryService, this.projectService, this.securityService);
62+
63+
when(this.securityService.withPermission(Matchers.any(), Matchers.any()))//
64+
.thenReturn(this.escalatedSecurityContext);
65+
when(this.escalatedSecurityContext.call(Matchers.any()))//
66+
.thenAnswer(new Answer<Boolean>() {
67+
@Override
68+
public Boolean answer(InvocationOnMock invocation) throws Throwable {
69+
Operation<?, ?> op = (Operation<?, ?>) invocation.getArguments()[0];
70+
return (Boolean) op.perform();
71+
}
72+
});
4973
}
5074

5175
@Test
5276
public void testThatAdminAllowedCanBeChecked() {
53-
this.sut.isAdminAllowed(null, null);
77+
this.projectKey = null;
78+
this.repositorySlug = null;
79+
this.sut.isAdminAllowed(this.projectKey, this.repositorySlug);
5480
}
5581

5682
@Test
5783
public void testThatAllowedButtonsCanBeFiltered() {
84+
this.projectKey = "p1";
85+
this.repositorySlug = "r1";
86+
5887
when(this.userManager.getRemoteUser())//
5988
.thenReturn(this.user);
6089
when(this.userManager.getRemoteUser().getUserKey())//
@@ -77,12 +106,16 @@ public void testThatAllowedButtonsCanBeFiltered() {
77106

78107
@Test
79108
public void testThatAllowedCanBeChecked() {
109+
this.projectKey = "p1";
110+
this.repositorySlug = "r1";
111+
80112
when(this.userManager.getRemoteUser())//
81113
.thenReturn(this.user);
82114
when(this.userManager.getRemoteUser().getUserKey())//
83115
.thenReturn(this.userKey);
84116
when(this.userManager.isSystemAdmin(this.userKey))//
85117
.thenReturn(true);
118+
86119
PrnfbButton candidate = new PrnfbButton(null, "title", ADMIN, ON_OR_OFF.off, "p1", "r1");
87120
assertThat(this.sut.isAllowedUseButton(candidate))//
88121
.isTrue();

0 commit comments

Comments
 (0)