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

Commit d6d3dce

Browse files
committed
Getting clone URL:s with admin permission #119
1 parent 97dc536 commit d6d3dce

File tree

6 files changed

+1152
-1165
lines changed

6 files changed

+1152
-1165
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ target
33
.classpath
44
.settings
55
*~
6+
.okhttpcache
7+
node_modules

src/main/java/se/bjurr/prnfs/ManualResource.java

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,39 @@
5656

5757
@Path("/manual")
5858
public class ManualResource {
59-
private static Gson gson = new Gson();
60-
private final UserManager userManager;
61-
private final UserService userService;
62-
private final PullRequestService pullRequestService;
63-
private final PrnfsPullRequestEventListener prnfsPullRequestEventListener;
64-
private final SecurityService securityService;
65-
private final PluginSettingsFactory pluginSettingsFactory;
66-
private final ApplicationPropertiesService propertiesService;
67-
private final RepositoryService repositoryService;
6859
private static final List<AdminFormValues.BUTTON_VISIBILITY> adminOk = newArrayList();
60+
private static Gson gson = new Gson();
6961
private static final List<AdminFormValues.BUTTON_VISIBILITY> systemAdminOk = newArrayList();
7062
static {
7163
adminOk.add(BUTTON_VISIBILITY.ADMIN);
7264
adminOk.add(SYSTEM_ADMIN);
7365
systemAdminOk.add(SYSTEM_ADMIN);
7466
}
7567

68+
static boolean allowedUseButton(PrnfsButton candidate, boolean isAdmin, boolean isSystemAdmin) {
69+
if (candidate.getVisibility().equals(EVERYONE)) {
70+
return TRUE;
71+
}
72+
if (isSystemAdmin && systemAdminOk.contains(candidate.getVisibility())) {
73+
return TRUE;
74+
} else if (isAdmin && adminOk.contains(candidate.getVisibility())) {
75+
return TRUE;
76+
} else if (candidate.getVisibility().equals(EVERYONE)) {
77+
return TRUE;
78+
}
79+
return FALSE;
80+
}
81+
82+
private final PluginSettingsFactory pluginSettingsFactory;
83+
private final PrnfsPullRequestEventListener prnfsPullRequestEventListener;
84+
private final ApplicationPropertiesService propertiesService;
85+
private final PullRequestService pullRequestService;
86+
private final RepositoryService repositoryService;
87+
private final SecurityService securityService;
88+
private final UserManager userManager;
89+
90+
private final UserService userService;
91+
7692
public ManualResource(UserManager userManager, UserService userService, PluginSettingsFactory pluginSettingsFactory,
7793
PullRequestService pullRequestService, PrnfsPullRequestEventListener prnfsPullRequestEventListener,
7894
RepositoryService repositoryService, ApplicationPropertiesService propertiesService, SecurityService securityService) {
@@ -90,59 +106,65 @@ public ManualResource(UserManager userManager, UserService userService, PluginSe
90106
@Produces(APPLICATION_JSON)
91107
public Response get(@Context HttpServletRequest request, @QueryParam("repositoryId") Integer repositoryId,
92108
@QueryParam("pullRequestId") Long pullRequestId) throws Exception {
93-
if (userManager.getRemoteUser(request) == null) {
109+
if (this.userManager.getRemoteUser(request) == null) {
94110
return status(UNAUTHORIZED).build();
95111
}
96112
List<PrnfsButton> buttons = newArrayList();
97113
final PrnfsSettings settings = getSettings();
98114
for (PrnfsButton candidate : settings.getButtons()) {
99-
UserKey userKey = userManager.getRemoteUserKey();
115+
UserKey userKey = this.userManager.getRemoteUserKey();
100116
PrnfsPullRequestAction pullRequestAction = PrnfsPullRequestAction.valueOf(BUTTON_TRIGGER);
101-
final PullRequest pullRequest = pullRequestService.getById(repositoryId, pullRequestId);
117+
final PullRequest pullRequest = this.pullRequestService.getById(repositoryId, pullRequestId);
102118
Map<PrnfsVariable, Supplier<String>> variables = getVariables(settings, candidate.getFormIdentifier());
103-
if (allowedUseButton(candidate, userManager.isAdmin(userKey), userManager.isSystemAdmin(userKey))
119+
if (allowedUseButton(candidate, this.userManager.isAdmin(userKey), this.userManager.isSystemAdmin(userKey))
104120
&& triggeredByAction(settings, pullRequestAction, pullRequest, variables, request)) {
105121
buttons.add(candidate);
106122
}
107123
}
108124
return ok(gson.toJson(buttons), APPLICATION_JSON).build();
109125
}
110126

111-
private boolean triggeredByAction(PrnfsSettings settings, PrnfsPullRequestAction pullRequestAction,
112-
PullRequest pullRequest, Map<PrnfsVariable, Supplier<String>> variables, HttpServletRequest request) {
113-
for (PrnfsNotification prnfsNotification : settings.getNotifications()) {
114-
PrnfsRenderer renderer = getRenderer(pullRequest, prnfsNotification, pullRequestAction, variables, request);
115-
if (prnfsPullRequestEventListener.notificationTriggeredByAction(prnfsNotification, pullRequestAction, renderer,
116-
pullRequest)) {
117-
return TRUE;
118-
}
119-
}
120-
return FALSE;
121-
}
122-
123127
@POST
124128
@Produces(APPLICATION_JSON)
125129
public Response post(@Context HttpServletRequest request, @QueryParam("repositoryId") Integer repositoryId,
126130
@QueryParam("pullRequestId") Long pullRequestId, @QueryParam("formIdentifier") final String formIdentifier)
127131
throws Exception {
128-
if (userManager.getRemoteUser(request) == null) {
132+
if (this.userManager.getRemoteUser(request) == null) {
129133
return status(UNAUTHORIZED).build();
130134
}
131135

132136
final PrnfsSettings settings = getSettings();
133137
for (PrnfsNotification prnfsNotification : settings.getNotifications()) {
134138
PrnfsPullRequestAction pullRequestAction = PrnfsPullRequestAction.valueOf(BUTTON_TRIGGER);
135-
final PullRequest pullRequest = pullRequestService.getById(repositoryId, pullRequestId);
139+
final PullRequest pullRequest = this.pullRequestService.getById(repositoryId, pullRequestId);
136140
Map<PrnfsVariable, Supplier<String>> variables = getVariables(settings, formIdentifier);
137141
PrnfsRenderer renderer = getRenderer(pullRequest, prnfsNotification, pullRequestAction, variables, request);
138-
if (prnfsPullRequestEventListener.notificationTriggeredByAction(prnfsNotification, pullRequestAction, renderer,
142+
if (this.prnfsPullRequestEventListener.notificationTriggeredByAction(prnfsNotification, pullRequestAction, renderer,
139143
pullRequest)) {
140-
prnfsPullRequestEventListener.notify(prnfsNotification, pullRequestAction, pullRequest, variables, renderer);
144+
this.prnfsPullRequestEventListener.notify(prnfsNotification, pullRequestAction, pullRequest, variables, renderer);
141145
}
142146
}
143147
return status(OK).build();
144148
}
145149

150+
private PrnfsRenderer getRenderer(final PullRequest pullRequest, PrnfsNotification prnfsNotification,
151+
PrnfsPullRequestAction pullRequestAction, Map<PrnfsVariable, Supplier<String>> variables, HttpServletRequest request) {
152+
StashUser stashUser = this.userService.getUserBySlug(this.userManager.getRemoteUser(request).getUsername());
153+
return new PrnfsRenderer(pullRequest, pullRequestAction, stashUser, this.repositoryService, this.propertiesService,
154+
prnfsNotification, variables, this.securityService);
155+
}
156+
157+
private PrnfsSettings getSettings() throws Exception {
158+
final PrnfsSettings settings = this.securityService.withPermission(ADMIN, "Getting config").call(
159+
new Operation<PrnfsSettings, Exception>() {
160+
@Override
161+
public PrnfsSettings perform() throws Exception {
162+
return getPrnfsSettings(ManualResource.this.pluginSettingsFactory.createGlobalSettings());
163+
}
164+
});
165+
return settings;
166+
}
167+
146168
private Map<PrnfsVariable, Supplier<String>> getVariables(final PrnfsSettings settings, final String formIdentifier) {
147169
Map<PrnfsVariable, Supplier<String>> variables = new HashMap<PrnfsRenderer.PrnfsVariable, Supplier<String>>();
148170
variables.put(BUTTON_TRIGGER_TITLE, new Supplier<String>() {
@@ -159,35 +181,15 @@ public boolean apply(PrnfsButton input) {
159181
return variables;
160182
}
161183

162-
private PrnfsRenderer getRenderer(final PullRequest pullRequest, PrnfsNotification prnfsNotification,
163-
PrnfsPullRequestAction pullRequestAction, Map<PrnfsVariable, Supplier<String>> variables, HttpServletRequest request) {
164-
StashUser stashUser = userService.getUserBySlug(userManager.getRemoteUser(request).getUsername());
165-
return new PrnfsRenderer(pullRequest, pullRequestAction, stashUser, repositoryService, propertiesService,
166-
prnfsNotification, variables);
167-
}
168-
169-
static boolean allowedUseButton(PrnfsButton candidate, boolean isAdmin, boolean isSystemAdmin) {
170-
if (candidate.getVisibility().equals(EVERYONE)) {
171-
return TRUE;
172-
}
173-
if (isSystemAdmin && systemAdminOk.contains(candidate.getVisibility())) {
174-
return TRUE;
175-
} else if (isAdmin && adminOk.contains(candidate.getVisibility())) {
176-
return TRUE;
177-
} else if (candidate.getVisibility().equals(EVERYONE)) {
178-
return TRUE;
184+
private boolean triggeredByAction(PrnfsSettings settings, PrnfsPullRequestAction pullRequestAction,
185+
PullRequest pullRequest, Map<PrnfsVariable, Supplier<String>> variables, HttpServletRequest request) {
186+
for (PrnfsNotification prnfsNotification : settings.getNotifications()) {
187+
PrnfsRenderer renderer = getRenderer(pullRequest, prnfsNotification, pullRequestAction, variables, request);
188+
if (this.prnfsPullRequestEventListener.notificationTriggeredByAction(prnfsNotification, pullRequestAction, renderer,
189+
pullRequest)) {
190+
return TRUE;
191+
}
179192
}
180193
return FALSE;
181194
}
182-
183-
private PrnfsSettings getSettings() throws Exception {
184-
final PrnfsSettings settings = securityService.withPermission(ADMIN, "Getting config").call(
185-
new Operation<PrnfsSettings, Exception>() {
186-
@Override
187-
public PrnfsSettings perform() throws Exception {
188-
return getPrnfsSettings(pluginSettingsFactory.createGlobalSettings());
189-
}
190-
});
191-
return settings;
192-
}
193195
}

0 commit comments

Comments
 (0)