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

Commit 15d2bd2

Browse files
committed
Checking for admin permission on the repo/project #123
* If a user is only admin in one repo, the user should not be able to administrate the plugin in another repo. * Hiding admin restriction levels, in buttons config, that the user does not have access to. So that the user cannot create buttons that the user cannot see. * Sorting notifications and buttons by name in REST API.
1 parent 043117b commit 15d2bd2

17 files changed

+203
-69
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,24 @@
22

33
Changelog of Pull Request Notifier for Bitbucket.
44

5+
## Unreleased
6+
### GitHub [#123](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/123) Question on the configuration permissions.
7+
Checking for admin permission on the repo/project
8+
9+
* If a user is only admin in one repo, the user should not be able to administrate the plugin in another repo.
10+
* Hiding admin restriction levels, in buttons config, that the user does not have access to. So that the user cannot create buttons that the user cannot see.
11+
* Sorting notifications and buttons by name in REST API.
12+
13+
[8eb180033a79346](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/8eb180033a79346) Tomas Bjerre *2016-06-20 17:32:05*
14+
15+
## 2.23
16+
### GitHub [#122](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/122) Preserving configs when upgrading from stash 3.x to bitbucket 4.x
17+
Loading legacy settings correctly
18+
19+
* Did not save loaded legacy settings in new format when found. Got new UUID:s on every load.
20+
21+
[56827de4eb8310d](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/commit/56827de4eb8310d) Tomas Bjerre *2016-06-04 21:25:42*
22+
523
## 2.22
624
### GitHub [#119](https://github.com/tomasbjerre/pull-request-notifier-for-bitbucket/issues/119) You are not permitted to access this resource
725
Getting clone URL:s with admin permission

src/main/java/se/bjurr/prnfb/presentation/ButtonServlet.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import static se.bjurr.prnfb.transformer.ButtonTransformer.toButtonDtoList;
1010
import static se.bjurr.prnfb.transformer.ButtonTransformer.toPrnfbButton;
1111

12+
import java.util.Collections;
1213
import java.util.List;
1314
import java.util.UUID;
1415

@@ -46,8 +47,11 @@ public ButtonServlet(ButtonsService buttonsService, SettingsService settingsServ
4647
@Consumes(APPLICATION_JSON)
4748
@Produces(APPLICATION_JSON)
4849
public Response create(ButtonDTO buttonDto) {
49-
if (!this.userCheckService.isAdminAllowed()) {
50-
return status(UNAUTHORIZED).build();
50+
if (!this.userCheckService.isAdminAllowed(//
51+
buttonDto.getProjectKey().orNull()//
52+
, buttonDto.getRepositorySlug().orNull())) {
53+
return status(UNAUTHORIZED)//
54+
.build();
5155
}
5256
PrnfbButton prnfbButton = toPrnfbButton(buttonDto);
5357
PrnfbButton created = this.settingsService.addOrUpdateButton(prnfbButton);
@@ -62,11 +66,15 @@ public Response create(ButtonDTO buttonDto) {
6266
@Path("{uuid}")
6367
@XsrfProtectionExcluded
6468
@Produces(APPLICATION_JSON)
65-
public Response delete(@PathParam("uuid") UUID prnfbButton) {
66-
if (!this.userCheckService.isAdminAllowed()) {
67-
return status(UNAUTHORIZED).build();
69+
public Response delete(@PathParam("uuid") UUID prnfbButtonUuid) {
70+
PrnfbButton prnfbButton = this.settingsService.getButton(prnfbButtonUuid);
71+
if (!this.userCheckService.isAdminAllowed(//
72+
prnfbButton.getProjectKey().orNull()//
73+
, prnfbButton.getRepositorySlug().orNull())) {
74+
return status(UNAUTHORIZED)//
75+
.build();
6876
}
69-
this.settingsService.deleteButton(prnfbButton);
77+
this.settingsService.deleteButton(prnfbButtonUuid);
7078
return status(OK).build();
7179
}
7280

@@ -89,6 +97,7 @@ public Response get(@PathParam("repositoryId") Integer repositoryId, @PathParam(
8997
List<PrnfbButton> buttons = this.buttonsService.getButtons(repositoryId, pullRequestId);
9098
Iterable<PrnfbButton> allowedButtons = this.userCheckService.filterAllowed(buttons);
9199
List<ButtonDTO> dtos = toButtonDtoList(allowedButtons);
100+
Collections.sort(dtos);
92101
return ok(dtos, APPLICATION_JSON).build();
93102
}
94103

@@ -113,6 +122,7 @@ public Response get(@PathParam("projectKey") String projectKey, @PathParam("repo
113122
}
114123
List<PrnfbButton> buttons = this.settingsService.getButtons(projectKey, repositorySlug);
115124
List<ButtonDTO> dtos = toButtonDtoList(buttons);
125+
Collections.sort(dtos);
116126
return ok(dtos, APPLICATION_JSON).build();
117127
}
118128

src/main/java/se/bjurr/prnfb/presentation/GlobalAdminServlet.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import javax.servlet.http.HttpServletRequest;
1414
import javax.servlet.http.HttpServletResponse;
1515

16+
import se.bjurr.prnfb.service.UserCheckService;
17+
1618
import com.atlassian.bitbucket.repository.Repository;
1719
import com.atlassian.bitbucket.repository.RepositoryService;
1820
import com.atlassian.sal.api.auth.LoginUriProvider;
@@ -27,14 +29,16 @@ public class GlobalAdminServlet extends HttpServlet {
2729
private final LoginUriProvider loginUriProvider;
2830
private final TemplateRenderer renderer;
2931
private final RepositoryService repositoryService;
32+
private final UserCheckService userCheckService;
3033
private final UserManager userManager;
3134

3235
public GlobalAdminServlet(UserManager userManager, LoginUriProvider loginUriProvider, TemplateRenderer renderer,
33-
RepositoryService repositoryService) {
36+
RepositoryService repositoryService, UserCheckService userCheckService) {
3437
this.userManager = userManager;
3538
this.loginUriProvider = loginUriProvider;
3639
this.renderer = renderer;
3740
this.repositoryService = repositoryService;
41+
this.userCheckService = userCheckService;
3842
}
3943

4044
@Override
@@ -47,11 +51,21 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) {
4751
}
4852

4953
final Optional<Repository> repository = getRepository(request.getPathInfo());
54+
boolean isSystemAdmin = this.userCheckService.isSystemAdmin(user.getUserKey());
55+
String projectKey = null;
56+
String repositorySlug = null;
57+
if (repository.isPresent()) {
58+
projectKey = repository.get().getProject().getKey();
59+
repositorySlug = repository.get().getSlug();
60+
}
61+
boolean isAdmin = this.userCheckService.isAdmin(user.getUserKey(), projectKey, repositorySlug);
62+
5063
Map<String, Object> context = newHashMap();
5164
if (repository.isPresent()) {
5265
context = of( //
53-
"repository", repository.orNull() //
54-
);
66+
"repository", repository.orNull(), //
67+
"isAdmin", isAdmin, //
68+
"isSystemAdmin", isSystemAdmin);
5569
}
5670

5771
response.setContentType("text/html;charset=UTF-8");

src/main/java/se/bjurr/prnfb/presentation/NotificationServlet.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import static se.bjurr.prnfb.transformer.NotificationTransformer.toNotificationDtoList;
1111
import static se.bjurr.prnfb.transformer.NotificationTransformer.toPrnfbNotification;
1212

13+
import java.util.Collections;
1314
import java.util.List;
1415
import java.util.UUID;
1516

@@ -44,7 +45,7 @@ public NotificationServlet(SettingsService settingsService, UserCheckService use
4445
@Consumes(APPLICATION_JSON)
4546
@Produces(APPLICATION_JSON)
4647
public Response create(NotificationDTO notificationDto) {
47-
if (!this.userCheckService.isAdminAllowed()) {
48+
if (!this.userCheckService.isAdminAllowed(notificationDto.getProjectKey(), notificationDto.getRepositorySlug())) {
4849
return status(UNAUTHORIZED).build();
4950
}
5051
try {
@@ -64,7 +65,10 @@ public Response create(NotificationDTO notificationDto) {
6465
@XsrfProtectionExcluded
6566
@Produces(APPLICATION_JSON)
6667
public Response delete(@PathParam("uuid") UUID notification) {
67-
if (!this.userCheckService.isAdminAllowed()) {
68+
PrnfbNotification notificationDto = this.settingsService.getNotification(notification);
69+
if (!this.userCheckService.isAdminAllowed(//
70+
notificationDto.getProjectKey().orNull(), //
71+
notificationDto.getRepositorySlug().orNull())) {
6872
return status(UNAUTHORIZED).build();
6973
}
7074
this.settingsService.deleteNotification(notification);
@@ -79,6 +83,7 @@ public Response get() {
7983
}
8084
List<PrnfbNotification> notifications = this.settingsService.getNotifications();
8185
List<NotificationDTO> dtos = toNotificationDtoList(notifications);
86+
Collections.sort(dtos);
8287
return ok(dtos).build();
8388
}
8489

@@ -91,6 +96,7 @@ public Response get(@PathParam("projectKey") String projectKey) {
9196
}
9297
List<PrnfbNotification> notifications = this.settingsService.getNotifications(projectKey);
9398
List<NotificationDTO> dtos = toNotificationDtoList(notifications);
99+
Collections.sort(dtos);
94100
return ok(dtos).build();
95101
}
96102

@@ -103,6 +109,7 @@ public Response get(@PathParam("projectKey") String projectKey, @PathParam("repo
103109
}
104110
List<PrnfbNotification> notifications = this.settingsService.getNotifications(projectKey, repositorySlug);
105111
List<NotificationDTO> dtos = toNotificationDtoList(notifications);
112+
Collections.sort(dtos);
106113
return ok(dtos).build();
107114
}
108115

src/main/java/se/bjurr/prnfb/presentation/SettingsDataServlet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public Response get() {
5050
@Consumes(APPLICATION_JSON)
5151
@Produces(APPLICATION_JSON)
5252
public Response post(SettingsDataDTO settingsDataDto) {
53-
if (!this.userCheckService.isAdminAllowed()) {
53+
if (!this.userCheckService.isAdminAllowed(null, null)) {
5454
return status(UNAUTHORIZED).build();
5555
}
5656

src/main/java/se/bjurr/prnfb/presentation/dto/ButtonDTO.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@
99

1010
import se.bjurr.prnfb.settings.USER_LEVEL;
1111

12+
import com.google.common.base.Optional;
13+
1214
@XmlRootElement
1315
@XmlAccessorType(FIELD)
14-
public class ButtonDTO {
16+
public class ButtonDTO implements Comparable<ButtonDTO> {
1517

1618
private String name;
1719
private String projectKey;
1820
private String repositorySlug;
1921
private USER_LEVEL userLevel;
2022
private UUID uuid;
2123

24+
@Override
25+
public int compareTo(ButtonDTO o) {
26+
return this.name.compareTo(o.name);
27+
}
28+
2229
@Override
2330
public boolean equals(Object obj) {
2431
if (this == obj) {
@@ -69,12 +76,12 @@ public String getName() {
6976
return this.name;
7077
}
7178

72-
public String getProjectKey() {
73-
return this.projectKey;
79+
public Optional<String> getProjectKey() {
80+
return Optional.fromNullable(this.projectKey);
7481
}
7582

76-
public String getRepositorySlug() {
77-
return this.repositorySlug;
83+
public Optional<String> getRepositorySlug() {
84+
return Optional.fromNullable(this.repositorySlug);
7885
}
7986

8087
public USER_LEVEL getUserLevel() {

src/main/java/se/bjurr/prnfb/presentation/dto/NotificationDTO.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
@XmlRootElement
1515
@XmlAccessorType(FIELD)
16-
public class NotificationDTO {
16+
public class NotificationDTO implements Comparable<NotificationDTO> {
1717
private String filterRegexp;
1818
private String filterString;
1919
private List<HeaderDTO> headers;
@@ -36,6 +36,11 @@ public class NotificationDTO {
3636
private String user;
3737
private UUID uuid;
3838

39+
@Override
40+
public int compareTo(NotificationDTO o) {
41+
return this.name.compareTo(o.name);
42+
}
43+
3944
@Override
4045
public boolean equals(Object obj) {
4146
if (this == obj) {

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,45 +75,48 @@ private boolean isTriggeredByAction(ClientKeyStore clientKeyStore, List<PrnfbNot
7575
return FALSE;
7676
}
7777

78+
/**
79+
* Checks if the given button is visible on the pull request by either the from
80+
* or to repository.
81+
*/
82+
private boolean isVisibleOnPullRequest(PrnfbButton button, PullRequest pullRequest) {
83+
return (pullRequest.getFromRef() != null && isVisibleOnRepository(button, pullRequest.getFromRef().getRepository()))
84+
|| (pullRequest.getToRef() != null && isVisibleOnRepository(button, pullRequest.getToRef().getRepository()));
85+
}
86+
7887
/**
7988
* Checks if the given button is visible in the given repository.
80-
*
81-
* @param button Button under test
82-
* @param repository Repository to check for
83-
* @return True if the button is either globally visible or matches with the given repository
89+
*
90+
* @param button
91+
* Button under test
92+
* @param repository
93+
* Repository to check for
94+
* @return True if the button is either globally visible or matches with the
95+
* given repository
8496
*/
8597
private boolean isVisibleOnRepository(PrnfbButton button, Repository repository) {
86-
if(button.getRepositorySlug().isPresent()) {
98+
if (button.getRepositorySlug().isPresent()) {
8799
boolean visible = false;
88100
do {
89101
visible |= button.getProjectKey().get().equals(repository.getProject().getKey())
90102
&& button.getRepositorySlug().get().equals(repository.getSlug());
91-
} while(!visible && (repository = repository.getOrigin()) != null);
103+
} while (!visible && (repository = repository.getOrigin()) != null);
92104
return visible;
93105
} else {
94106
return TRUE;
95107
}
96108
}
97109

98-
/**
99-
* Checks if the given button is visible on the pull request by either the from or to repository.
100-
*/
101-
private boolean isVisibleOnPullRequest(PrnfbButton button, PullRequest pullRequest) {
102-
return
103-
(pullRequest.getFromRef() != null && isVisibleOnRepository(button, pullRequest.getFromRef().getRepository()))
104-
|| (pullRequest.getToRef() != null && isVisibleOnRepository(button, pullRequest.getToRef().getRepository()));
105-
}
106-
107110
@VisibleForTesting
108111
List<PrnfbButton> doGetButtons(List<PrnfbNotification> notifications, ClientKeyStore clientKeyStore,
109112
final PullRequest pullRequest, boolean shouldAcceptAnyCertificate) {
110113
List<PrnfbButton> allFoundButtons = newArrayList();
111114
for (PrnfbButton candidate : this.settingsService.getButtons()) {
112115
Map<PrnfbVariable, Supplier<String>> variables = getVariables(candidate.getUuid());
113116
PrnfbPullRequestAction pullRequestAction = BUTTON_TRIGGER;
114-
if (this.userCheckService.isAllowedUseButton(candidate)
117+
if (this.userCheckService.isAllowedUseButton(candidate)//
115118
&& isTriggeredByAction(clientKeyStore, notifications, shouldAcceptAnyCertificate, pullRequestAction, pullRequest,
116-
variables)
119+
variables) //
117120
&& (isVisibleOnPullRequest(candidate, pullRequest))) {
118121
allFoundButtons.add(candidate);
119122
}

0 commit comments

Comments
 (0)