This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
SecurityFunctions.java
468 lines (399 loc) · 17.8 KB
/
SecurityFunctions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* Copyright 2010, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/
package org.zanata.security;
import com.google.common.base.Optional;
import org.jboss.seam.ScopeType;
import org.jboss.seam.security.management.JpaIdentityStore;
import org.zanata.dao.PersonDAO;
import org.zanata.model.HAccount;
import org.zanata.model.HAccountRole;
import org.zanata.model.HIterationGroup;
import org.zanata.model.HLocale;
import org.zanata.model.HLocaleMember;
import org.zanata.model.HProject;
import org.zanata.model.HProjectIteration;
import org.zanata.security.permission.GrantsPermission;
import org.zanata.util.HttpUtil;
import org.zanata.util.ServiceLocator;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import lombok.extern.slf4j.Slf4j;
/**
* Contains static helper functions used inside the rules files.
*
* @author Carlos Munoz <a
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Slf4j
public class SecurityFunctions {
protected SecurityFunctions() {
}
/* admin can do anything */
@GrantsPermission
public static boolean isAdmin() {
return getIdentity().hasRole("admin");
}
public static boolean isProjectMaintainer(HProject project) {
Optional<HAccount> account = getAuthenticatedAccount();
return account.isPresent() && account.get().getPerson().isMaintainer(project);
}
/***************************************************************************
* The Following Rules are for Identity Management
**************************************************************************/
@GrantsPermission(actions = "create")
public static boolean canCreateAccount(String target) {
return target.equals("seam.account") && isAdmin();
}
@GrantsPermission
public static boolean canManageUsers(String target) {
return target.equals("seam.user") && isAdmin();
}
@GrantsPermission
public static boolean canManageRoles(String target) {
return target.equals("seam.role") && isAdmin();
}
/***************************************************************************
* Project ownership rules
**************************************************************************/
/* Any authenticated user can create a project */
@GrantsPermission(actions = "insert")
public static boolean canCreateProject(HProject target) {
return getIdentity().isLoggedIn();
}
/* anyone can read a project */
@GrantsPermission(actions = "read")
public static boolean canReadProject(HProject target) {
return true;
}
/* anyone can read a project iteration */
@GrantsPermission(actions = "read")
public static boolean canReadProjectIteration(HProjectIteration target) {
return true;
}
/*
* Project maintainers may edit (but not delete) a project, or add an
* iteration. Note that 'add-iteration' (on a project) should be granted in
* the same circumstances that 'insert' is granted (on an iteration). In
* other words, make sure the rules agree with each other. (NB:
* 'add-iteration' is used in the UI to enable buttons etc, without
* requiring the construction of HProjectIteration just to do a permission
* check.)
*/
@GrantsPermission(actions = { "update",
"add-iteration" })
public static boolean canUpdateProjectOrAddIteration(HProject project) {
if (!getIdentity().isLoggedIn()) {
return false;
}
return isProjectMaintainer(project);
}
/*
* Project maintainers may create or edit (but not delete) a project
* iteration
*/
@GrantsPermission(actions = {
"insert", "update", "import-template" })
public static boolean canInsertOrUpdateProjectIteration(
HProjectIteration iteration) {
return isProjectMaintainer(iteration.getProject());
}
/***************************************************************************
* Translation rules
**************************************************************************/
/* Language Team members can add a translation for their language teams */
@GrantsPermission(actions = { "add-translation", "modify-translation" })
public static boolean canTranslate(HProject project, HLocale lang) {
return isUserAllowedAccess(project) && isUserTranslatorOfLanguage(lang);
}
public static boolean isUserTranslatorOfLanguage(HLocale lang) {
Optional<HAccount> authenticatedAccount = getAuthenticatedAccount();
PersonDAO personDAO =
ServiceLocator.instance().getInstance(PersonDAO.class);
if (authenticatedAccount.isPresent()) {
return personDAO.isUserInLanguageTeamWithRoles(
authenticatedAccount.get().getPerson(), lang, true, null, null);
}
return false; // No authenticated user
}
public static boolean isUserAllowedAccess(HProject project) {
if (project.isRestrictedByRoles()) {
ZanataIdentity identity = getIdentity();
for (HAccountRole role : project.getAllowedRoles()) {
if (identity.hasRole(role.getName())) {
return true;
}
}
// no access
return false;
} else {
return true;
}
}
/***************************************************************************
* Review translation rules
**************************************************************************/
/* Language Team reviewer can approve/reject translation */
// TODO Unify these two permission actions into a single one
@GrantsPermission(
actions = { "review-translation", "translation-review" })
public static boolean
canReviewTranslation(HProject project, HLocale locale) {
return isUserAllowedAccess(project) && isUserReviewerOfLanguage(locale);
}
public static boolean isUserReviewerOfLanguage(HLocale lang) {
Optional<HAccount> authenticatedAccount = getAuthenticatedAccount();
PersonDAO personDAO =
ServiceLocator.instance().getInstance(PersonDAO.class);
if (authenticatedAccount.isPresent()) {
return personDAO.isUserInLanguageTeamWithRoles(
authenticatedAccount.get().getPerson(), lang, null, true, null);
} else {
return false;
}
}
/*
* Project Maintainers can add, modify or review a translation for their
* projects
*/
@GrantsPermission(actions = { "add-translation", "modify-translation",
"review-translation", "translation-review" })
public static boolean canAddOrReviewTranslation(
HProject project, HLocale locale) {
Optional<HAccount> authenticatedAccount = getAuthenticatedAccount();
return authenticatedAccount.isPresent() && isProjectMaintainer(project);
}
/* Project Maintainer can import translation (merge type is IMPORT) */
@GrantsPermission(actions = "import-translation")
public static boolean canImportTranslation(
HProjectIteration projectIteration) {
Optional<HAccount> account = getAuthenticatedAccount();
return account.isPresent() && account.get().getPerson().isMaintainer(projectIteration.getProject());
}
public static boolean isLanguageTeamMember(HLocale lang) {
Optional<HAccount> authenticatedAccount = getAuthenticatedAccount();
PersonDAO personDAO =
ServiceLocator.instance().getInstance(PersonDAO.class);
if (authenticatedAccount.isPresent()) {
return personDAO.isUserInLanguageTeamWithRoles(
authenticatedAccount.get().getPerson(), lang, null, null, null);
} else {
return false;
}
}
/***************************************************************************
* Glossary rules
**************************************************************************/
/* 'glossarist' can push and update glossaries */
@GrantsPermission(actions = { "glossary-insert", "glossary-update" })
public static boolean canPushGlossary() {
return getIdentity().hasRole("glossarist");
}
/* 'glossarist-admin' can also delete */
@GrantsPermission(actions = { "glossary-insert", "glossary-update",
"glossary-delete" })
public static boolean canAdminGlossary() {
return getIdentity().hasRole("glossary-admin");
}
/***************************************************************************
* Language Team Coordinator rules
**************************************************************************/
/* Anyone can read Locale members */
@GrantsPermission(actions = "read")
public static boolean canSeeLocaleMembers(HLocaleMember localeMember) {
return true;
}
/* 'team coordinator' can manage language teams */
@GrantsPermission(actions = "manage-language-team")
public static boolean isUserCoordinatorOfLanguage(HLocale lang) {
Optional<HAccount> authenticatedAccount = getAuthenticatedAccount();
PersonDAO personDAO =
ServiceLocator.instance().getInstance(PersonDAO.class);
if (authenticatedAccount.isPresent()) {
return personDAO.isUserInLanguageTeamWithRoles(
authenticatedAccount.get().getPerson(), lang, null, null, true);
} else {
return false;
}
}
/* 'team coordinator' can insert/update/delete language team members */
@GrantsPermission(actions = { "insert", "update", "delete" })
public static boolean canModifyLanguageTeamMembers(
HLocaleMember localeMember) {
return isUserCoordinatorOfLanguage(localeMember.getSupportedLanguage());
}
/***************************************************************************
* View Obsolete Project and Project Iteration rules
**************************************************************************/
// Only admin can view obsolete projects
@GrantsPermission(actions = "view-obsolete")
public static boolean canViewObsoleteProject(HProject project) {
return getIdentity().hasRole("admin");
}
// Only admin can view obsolete project iterations
@GrantsPermission(actions = "view-obsolete")
public static boolean canViewObsoleteProjectIteration(
HProjectIteration projectIteration) {
return getIdentity().hasRole("admin");
}
/***************************************************************************
* Mark Project and Project Iteration obsolete rules
**************************************************************************/
// Project maintainer can archive/delete projects
@GrantsPermission(actions = "mark-obsolete")
public static boolean canArchiveProject(HProject project) {
return isProjectMaintainer(project);
}
// Project maintainer can archive/delete project iterations
@GrantsPermission(actions = "mark-obsolete")
public static boolean canArchiveProjectIteration(
HProjectIteration projectIteration) {
return isProjectMaintainer(projectIteration.getProject());
}
/***************************************************************************
* File Download rules
**************************************************************************/
/*
* Permissions to download files. NOTE: Currently any authenticated user can
* download files
*/
@GrantsPermission(actions = { "download-single", "download-all" })
public static boolean canDownloadFiles(HProjectIteration projectIteration) {
return getIdentity().isLoggedIn();
}
/***************************************************************************
* Version Group rules
**************************************************************************/
@GrantsPermission(actions = "update")
public static boolean canUpdateVersionGroup(HIterationGroup group) {
Optional<HAccount> account = getAuthenticatedAccount();
return account.isPresent() && account.get().getPerson().isMaintainer(group);
}
@GrantsPermission(actions = "insert")
public static boolean canInsertVersionGroup(HIterationGroup group) {
return isAdmin();
}
@GrantsPermission(actions = "view-obsolete")
public static boolean canViewObsoleteVersionGroup(HIterationGroup group) {
return isAdmin();
}
/***************************************************************************
* Copy Trans rules
**************************************************************************/
/** Admins and Project maintainers can perform copy-trans */
@GrantsPermission(actions = "copy-trans")
public static boolean canRunCopyTrans(HProjectIteration iteration) {
Optional<HAccount> account = getAuthenticatedAccount();
return account.isPresent() && account.get().getPerson().isMaintainer(iteration.getProject());
}
/*****************************************************************************************
* Review comment rules
******************************************************************************************/
@GrantsPermission(actions = "review-comment")
public static boolean canCommentOnReview(HLocale locale, HProject project) {
return isUserAllowedAccess(project) && isLanguageTeamMember(locale);
}
@GrantsPermission(actions = "review-comment")
public static boolean canMaintainerCommentOnReview(HLocale locale,
HProject project) {
Optional<HAccount> account = getAuthenticatedAccount();
return account.isPresent() && account.get().getPerson().isMaintainer(project);
}
private static final ZanataIdentity getIdentity() {
return ServiceLocator.instance().getInstance(ZanataIdentity.class);
}
private static final Optional<HAccount> getAuthenticatedAccount() {
return Optional.fromNullable(ServiceLocator.instance().getInstance(
JpaIdentityStore.AUTHENTICATED_USER, HAccount.class));
}
private static final <T> T extractTarget(Object[] array, Class<T> type) {
for (int i = 0; i < array.length; i++) {
Object arrayElement = array[i];
if (type.isAssignableFrom(arrayElement.getClass())) {
return (T) arrayElement;
}
}
return null;
}
/*****************************************************************************************
* TMX rules
******************************************************************************************/
@GrantsPermission(actions = "download-tmx")
public static boolean canDownloadTMX() {
Optional<HAccount> account = getAuthenticatedAccount();
return account.isPresent();
}
/*****************************************************************************************
* HTTP request rules
******************************************************************************************/
/**
* Check if user can access to REST URL with httpMethod.
* 1) Check if request can communicate to with rest service path,
* 2) then check if request can perform the specific API action.
*
* If request is from anonymous user(account == null),
* only 'Read' action are allowed. Additionally, role-based check will be
* performed in the REST service class.
*
* This rule apply to all REST endpoint.
*
* @param account - Authenticated account
* @param httpMethod - {@link javax.ws.rs.HttpMethod}
* @param restServicePath - service path of rest request.
* See annotation @Path in REST service class.
*/
public static final boolean canAccessRestPath(@Nullable HAccount account,
String httpMethod, String restServicePath) {
//This is to allow data injection for function-test/rest-test
if(isTestServicePath(restServicePath)) {
log.debug("Allow rest access for Zanata test");
return true;
}
if (account != null) {
return true;
}
if (HttpUtil.isReadMethod(httpMethod)) {
return true;
}
return false;
}
public static final boolean canAccessRestPath(
@Nonnull ZanataIdentity identity,
String httpMethod, String restServicePath) {
// This is to allow data injection for function-test/rest-test
if (isTestServicePath(restServicePath)) {
log.debug("Allow rest access for Zanata test");
return true;
}
if (identity.isLoggedIn()) {
return true;
}
return false;
}
/**
* Check if request path are from functional test or RestTest
*
* @param servicePath - service path of rest request.
* See annotation @Path in REST service class.
*/
private static boolean isTestServicePath(String servicePath) {
return servicePath != null && servicePath.startsWith("/test");
}
}