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

Commit 42280c4

Browse files
committed
Avoid crash if button configured having none existing repo/project #154
1 parent 235ae47 commit 42280c4

File tree

2 files changed

+71
-58
lines changed

2 files changed

+71
-58
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
Changelog of Pull Request Notifier for Bitbucket.
44

55
## Unreleased
6-
### No issue
7-
Replacing unresolved variables with nothing
6+
### GitHub [#159](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/159) Replacement failure can result in bad URLs
7+
Replacing unresolved variables with nothing
88

99
* If a crash happens, like NPE, when a variable was resolved. Then that variable would be kept unchanged. Resulting in illegal chars in URL.
1010

11-
[f279f19796c0fed](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/f279f19796c0fed) Tomas Bjerre *2016-11-16 20:23:33*
11+
[316cb796c3eedcf](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/316cb796c3eedcf) Tomas Bjerre *2016-11-16 20:39:20*
1212

1313
## 2.38
1414
### No issue

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

Lines changed: 68 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
import static com.google.common.base.Strings.emptyToNull;
77
import static com.google.common.base.Throwables.propagate;
88
import static com.google.common.collect.Iterables.filter;
9+
import static org.slf4j.LoggerFactory.getLogger;
910
import static se.bjurr.prnfb.settings.USER_LEVEL.ADMIN;
1011
import static se.bjurr.prnfb.settings.USER_LEVEL.EVERYONE;
1112

1213
import java.util.List;
1314

1415
import javax.annotation.Nullable;
1516

16-
import se.bjurr.prnfb.settings.PrnfbButton;
17-
import se.bjurr.prnfb.settings.USER_LEVEL;
17+
import org.slf4j.Logger;
1818

1919
import com.atlassian.bitbucket.permission.PermissionService;
2020
import com.atlassian.bitbucket.project.Project;
@@ -29,7 +29,11 @@
2929
import com.google.common.annotations.VisibleForTesting;
3030
import com.google.common.base.Predicate;
3131

32+
import se.bjurr.prnfb.settings.PrnfbButton;
33+
import se.bjurr.prnfb.settings.USER_LEVEL;
34+
3235
public class UserCheckService {
36+
private static final Logger LOG = getLogger(UserCheckService.class);
3337
private final PermissionService permissionService;
3438
private final ProjectService projectService;
3539
private final RepositoryService repositoryService;
@@ -57,8 +61,40 @@ public boolean apply(PrnfbButton input) {
5761
return allowedButtons;
5862
}
5963

64+
@VisibleForTesting
65+
private Project getProject(String projectKey) {
66+
try {
67+
return securityService//
68+
.withPermission(SYS_ADMIN, "Getting project")//
69+
.call(new Operation<Project, Exception>() {
70+
@Override
71+
public Project perform() throws Exception {
72+
return projectService.getByKey(projectKey);
73+
}
74+
});
75+
} catch (Exception e) {
76+
throw propagate(e);
77+
}
78+
}
79+
80+
@VisibleForTesting
81+
Repository getRepo(String projectKey, String repositorySlug) {
82+
try {
83+
return securityService//
84+
.withPermission(SYS_ADMIN, "Getting repo")//
85+
.call(new Operation<Repository, Exception>() {
86+
@Override
87+
public Repository perform() throws Exception {
88+
return repositoryService.getBySlug(projectKey, repositorySlug);
89+
}
90+
});
91+
} catch (Exception e) {
92+
throw propagate(e);
93+
}
94+
}
95+
6096
public boolean isAdmin(UserKey userKey, String projectKey, String repositorySlug) {
61-
boolean isAdmin = this.userManager.isAdmin(userKey);
97+
boolean isAdmin = userManager.isAdmin(userKey);
6298
if (isAdmin) {
6399
return isAdmin;
64100
}
@@ -68,15 +104,24 @@ public boolean isAdmin(UserKey userKey, String projectKey, String repositorySlug
68104

69105
if (projectKey != null && repositorySlug == null) {
70106
Project project = getProject(projectKey);
71-
boolean isAllowed = this.permissionService.hasProjectPermission(project, PROJECT_ADMIN);
107+
if (project == null) {
108+
LOG.error("Button with project " + projectKey + " configured. But no such project exists!");
109+
return false;
110+
}
111+
boolean isAllowed = permissionService.hasProjectPermission(project, PROJECT_ADMIN);
72112
if (isAllowed) {
73113
return true;
74114
}
75115
}
76116

77117
if (projectKey != null && repositorySlug != null) {
78118
Repository repository = getRepo(projectKey, repositorySlug);
79-
return this.permissionService.hasRepositoryPermission(repository, REPO_ADMIN);
119+
if (repository == null) {
120+
LOG.error(
121+
"Button with project " + projectKey + " and repo " + repositorySlug + " configured. But no such repo exists!");
122+
return false;
123+
}
124+
return permissionService.hasRepositoryPermission(repository, REPO_ADMIN);
80125
}
81126
return false;
82127
}
@@ -85,14 +130,28 @@ public boolean isAdmin(UserKey userKey, String projectKey, String repositorySlug
85130
* null if global.
86131
*/
87132
public boolean isAdminAllowed(@Nullable String projectKey, @Nullable String repositorySlug) {
88-
final UserProfile user = this.userManager.getRemoteUser();
133+
final UserProfile user = userManager.getRemoteUser();
89134
if (user == null) {
90135
return false;
91136
}
92-
USER_LEVEL adminRestriction = this.settingsService.getPrnfbSettingsData().getAdminRestriction();
137+
USER_LEVEL adminRestriction = settingsService.getPrnfbSettingsData().getAdminRestriction();
93138
return isAdminAllowed(adminRestriction, projectKey, repositorySlug);
94139
}
95140

141+
private boolean isAdminAllowed(USER_LEVEL adminRestriction, @Nullable String projectKey,
142+
@Nullable String repositorySlug) {
143+
UserKey userKey = userManager.getRemoteUser().getUserKey();
144+
boolean isAdmin = isAdmin(userKey, projectKey, repositorySlug);
145+
boolean isSystemAdmin = isSystemAdmin(userKey);
146+
return isAdminAllowedCheck(adminRestriction, isAdmin, isSystemAdmin);
147+
}
148+
149+
boolean isAdminAllowedCheck(USER_LEVEL userLevel, boolean isAdmin, boolean isSystemAdmin) {
150+
return userLevel == EVERYONE //
151+
|| isSystemAdmin //
152+
|| isAdmin && userLevel == ADMIN;
153+
}
154+
96155
public boolean isAllowedUseButton(PrnfbButton candidate) {
97156
return isAdminAllowed(//
98157
candidate.getUserLevel(), //
@@ -101,61 +160,15 @@ public boolean isAllowedUseButton(PrnfbButton candidate) {
101160
}
102161

103162
public boolean isSystemAdmin(UserKey userKey) {
104-
return this.userManager.isSystemAdmin(userKey);
163+
return userManager.isSystemAdmin(userKey);
105164
}
106165

107166
public boolean isViewAllowed() {
108-
UserProfile user = this.userManager.getRemoteUser();
167+
UserProfile user = userManager.getRemoteUser();
109168
if (user == null) {
110169
return false;
111170
}
112171
return true;
113172
}
114173

115-
@VisibleForTesting
116-
private Project getProject(String projectKey) {
117-
try {
118-
return this.securityService//
119-
.withPermission(SYS_ADMIN, "Getting project")//
120-
.call(new Operation<Project, Exception>() {
121-
@Override
122-
public Project perform() throws Exception {
123-
return UserCheckService.this.projectService.getByKey(projectKey);
124-
}
125-
});
126-
} catch (Exception e) {
127-
throw propagate(e);
128-
}
129-
}
130-
131-
private boolean isAdminAllowed(USER_LEVEL adminRestriction, @Nullable String projectKey,
132-
@Nullable String repositorySlug) {
133-
UserKey userKey = this.userManager.getRemoteUser().getUserKey();
134-
boolean isAdmin = isAdmin(userKey, projectKey, repositorySlug);
135-
boolean isSystemAdmin = isSystemAdmin(userKey);
136-
return isAdminAllowedCheck(adminRestriction, isAdmin, isSystemAdmin);
137-
}
138-
139-
@VisibleForTesting
140-
Repository getRepo(String projectKey, String repositorySlug) {
141-
try {
142-
return this.securityService//
143-
.withPermission(SYS_ADMIN, "Getting repo")//
144-
.call(new Operation<Repository, Exception>() {
145-
@Override
146-
public Repository perform() throws Exception {
147-
return UserCheckService.this.repositoryService.getBySlug(projectKey, repositorySlug);
148-
}
149-
});
150-
} catch (Exception e) {
151-
throw propagate(e);
152-
}
153-
}
154-
155-
boolean isAdminAllowedCheck(USER_LEVEL userLevel, boolean isAdmin, boolean isSystemAdmin) {
156-
return userLevel == EVERYONE //
157-
|| isSystemAdmin //
158-
|| isAdmin && userLevel == ADMIN;
159-
}
160-
161174
}

0 commit comments

Comments
 (0)