6
6
import static com .google .common .base .Strings .emptyToNull ;
7
7
import static com .google .common .base .Throwables .propagate ;
8
8
import static com .google .common .collect .Iterables .filter ;
9
+ import static org .slf4j .LoggerFactory .getLogger ;
9
10
import static se .bjurr .prnfb .settings .USER_LEVEL .ADMIN ;
10
11
import static se .bjurr .prnfb .settings .USER_LEVEL .EVERYONE ;
11
12
12
13
import java .util .List ;
13
14
14
15
import javax .annotation .Nullable ;
15
16
16
- import se .bjurr .prnfb .settings .PrnfbButton ;
17
- import se .bjurr .prnfb .settings .USER_LEVEL ;
17
+ import org .slf4j .Logger ;
18
18
19
19
import com .atlassian .bitbucket .permission .PermissionService ;
20
20
import com .atlassian .bitbucket .project .Project ;
29
29
import com .google .common .annotations .VisibleForTesting ;
30
30
import com .google .common .base .Predicate ;
31
31
32
+ import se .bjurr .prnfb .settings .PrnfbButton ;
33
+ import se .bjurr .prnfb .settings .USER_LEVEL ;
34
+
32
35
public class UserCheckService {
36
+ private static final Logger LOG = getLogger (UserCheckService .class );
33
37
private final PermissionService permissionService ;
34
38
private final ProjectService projectService ;
35
39
private final RepositoryService repositoryService ;
@@ -57,8 +61,40 @@ public boolean apply(PrnfbButton input) {
57
61
return allowedButtons ;
58
62
}
59
63
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
+
60
96
public boolean isAdmin (UserKey userKey , String projectKey , String repositorySlug ) {
61
- boolean isAdmin = this . userManager .isAdmin (userKey );
97
+ boolean isAdmin = userManager .isAdmin (userKey );
62
98
if (isAdmin ) {
63
99
return isAdmin ;
64
100
}
@@ -68,15 +104,24 @@ public boolean isAdmin(UserKey userKey, String projectKey, String repositorySlug
68
104
69
105
if (projectKey != null && repositorySlug == null ) {
70
106
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 );
72
112
if (isAllowed ) {
73
113
return true ;
74
114
}
75
115
}
76
116
77
117
if (projectKey != null && repositorySlug != null ) {
78
118
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 );
80
125
}
81
126
return false ;
82
127
}
@@ -85,14 +130,28 @@ public boolean isAdmin(UserKey userKey, String projectKey, String repositorySlug
85
130
* null if global.
86
131
*/
87
132
public boolean isAdminAllowed (@ Nullable String projectKey , @ Nullable String repositorySlug ) {
88
- final UserProfile user = this . userManager .getRemoteUser ();
133
+ final UserProfile user = userManager .getRemoteUser ();
89
134
if (user == null ) {
90
135
return false ;
91
136
}
92
- USER_LEVEL adminRestriction = this . settingsService .getPrnfbSettingsData ().getAdminRestriction ();
137
+ USER_LEVEL adminRestriction = settingsService .getPrnfbSettingsData ().getAdminRestriction ();
93
138
return isAdminAllowed (adminRestriction , projectKey , repositorySlug );
94
139
}
95
140
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
+
96
155
public boolean isAllowedUseButton (PrnfbButton candidate ) {
97
156
return isAdminAllowed (//
98
157
candidate .getUserLevel (), //
@@ -101,61 +160,15 @@ public boolean isAllowedUseButton(PrnfbButton candidate) {
101
160
}
102
161
103
162
public boolean isSystemAdmin (UserKey userKey ) {
104
- return this . userManager .isSystemAdmin (userKey );
163
+ return userManager .isSystemAdmin (userKey );
105
164
}
106
165
107
166
public boolean isViewAllowed () {
108
- UserProfile user = this . userManager .getRemoteUser ();
167
+ UserProfile user = userManager .getRemoteUser ();
109
168
if (user == null ) {
110
169
return false ;
111
170
}
112
171
return true ;
113
172
}
114
173
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
-
161
174
}
0 commit comments