1
1
package org .ohdsi .webapi .service ;
2
2
3
+ import com .google .common .collect .ImmutableSet ;
3
4
import com .odysseusinc .arachne .commons .types .DBMSType ;
4
5
import com .odysseusinc .arachne .execution_engine_common .api .v1 .dto .DataSourceUnsecuredDTO ;
5
6
import com .odysseusinc .datasourcemanager .krblogin .KerberosService ;
6
7
import com .odysseusinc .datasourcemanager .krblogin .KrbConfig ;
7
8
import com .odysseusinc .datasourcemanager .krblogin .RuntimeServiceMode ;
9
+ import org .apache .commons .collections4 .CollectionUtils ;
8
10
import org .apache .commons .io .FileUtils ;
9
11
import org .apache .commons .lang3 .StringUtils ;
10
12
import org .apache .shiro .SecurityUtils ;
29
31
import org .ohdsi .webapi .reusable .domain .Reusable ;
30
32
import org .ohdsi .webapi .security .PermissionService ;
31
33
import org .ohdsi .webapi .service .dto .CommonEntityDTO ;
34
+ import org .ohdsi .webapi .service .dto .PermissionCheckType ;
32
35
import org .ohdsi .webapi .shiro .Entities .UserEntity ;
33
36
import org .ohdsi .webapi .shiro .Entities .UserRepository ;
34
37
import org .ohdsi .webapi .shiro .management .DisabledSecurity ;
@@ -371,7 +374,7 @@ protected PermissionService getPermissionService() {
371
374
}
372
375
373
376
protected void assignTag (CommonEntityExt <?> entity , int tagId ) {
374
- checkOwnerOrAdminOrGrantedOrTagManager (entity );
377
+ checkPermissions (entity , ImmutableSet . of ( PermissionCheckType . IS_OWNER , PermissionCheckType . IS_ADMIN , PermissionCheckType . HAS_WRITE_ACCESS , PermissionCheckType . IS_TAG_MANAGER ) );
375
378
if (Objects .nonNull (entity )) {
376
379
Tag tag = tagService .getById (tagId );
377
380
if (Objects .nonNull (tag )) {
@@ -398,7 +401,7 @@ protected void assignTag(CommonEntityExt<?> entity, int tagId) {
398
401
}
399
402
400
403
protected void unassignTag (CommonEntityExt <?> entity , int tagId ) {
401
- checkOwnerOrAdminOrGrantedOrTagManager (entity );
404
+ checkPermissions (entity , ImmutableSet . of ( PermissionCheckType . IS_OWNER , PermissionCheckType . IS_ADMIN , PermissionCheckType . HAS_WRITE_ACCESS , PermissionCheckType . IS_TAG_MANAGER ) );
402
405
if (Objects .nonNull (entity )) {
403
406
Tag tag = tagService .getById (tagId );
404
407
if (Objects .nonNull (tag )) {
@@ -422,56 +425,30 @@ private boolean hasPermissionToAssignProtectedTags(final CommonEntityExt<?> enti
422
425
return TagSecurityUtils .checkPermission (TagSecurityUtils .getAssetName (entity ), method );
423
426
}
424
427
425
- protected void checkOwnerOrAdmin ( UserEntity owner ) {
426
- if ( security instanceof DisabledSecurity ) {
428
+ protected void checkPermissions ( CommonEntity <?> entity , Set < PermissionCheckType > permissionsToCheck ) {
429
+ if ( CollectionUtils . isEmpty ( permissionsToCheck )) {
427
430
return ;
428
431
}
429
432
430
- UserEntity user = getCurrentUser ();
431
- Long ownerId = Objects .nonNull (owner ) ? owner .getId () : null ;
432
-
433
- if (!(user .getId ().equals (ownerId ) || isAdmin ())) {
434
- throw new ForbiddenException ();
435
- }
436
- }
437
-
438
- protected void checkOwnerOrAdminOrModerator (UserEntity owner ) {
439
- if (security instanceof DisabledSecurity ) {
440
- return ;
441
- }
442
-
443
- UserEntity user = getCurrentUser ();
444
- Long ownerId = Objects .nonNull (owner ) ? owner .getId () : null ;
445
-
446
- if (!(user .getId ().equals (ownerId ) || isAdmin () || isModerator ())) {
447
- throw new ForbiddenException ();
448
- }
449
- }
450
-
451
- protected void checkOwnerOrAdminOrGranted (CommonEntity <?> entity ) {
452
433
if (security instanceof DisabledSecurity ) {
453
434
return ;
454
435
}
455
436
456
- UserEntity user = getCurrentUser ();
457
- Long ownerId = Objects .nonNull (entity .getCreatedBy ()) ? entity .getCreatedBy ().getId () : null ;
437
+ boolean isAllowed = (permissionsToCheck .contains (PermissionCheckType .IS_OWNER ) && isEntityOwner (entity )) ||
438
+ (permissionsToCheck .contains (PermissionCheckType .IS_ADMIN ) && isAdmin ()) ||
439
+ (permissionsToCheck .contains (PermissionCheckType .IS_MODERATOR ) && isModerator ()) ||
440
+ (permissionsToCheck .contains (PermissionCheckType .HAS_WRITE_ACCESS ) && permissionService .hasWriteAccess (entity )) ||
441
+ (permissionsToCheck .contains (PermissionCheckType .IS_TAG_MANAGER ) && TagSecurityUtils .canManageTags ());
458
442
459
- if (!( user . getId (). equals ( ownerId ) || isAdmin () || permissionService . hasWriteAccess ( entity )) ) {
443
+ if (! isAllowed ) {
460
444
throw new ForbiddenException ();
461
445
}
462
446
}
463
447
464
- protected void checkOwnerOrAdminOrGrantedOrTagManager (CommonEntity <?> entity ) {
465
- if (security instanceof DisabledSecurity ) {
466
- return ;
467
- }
468
-
448
+ private boolean isEntityOwner (CommonEntity <?> entity ){
469
449
UserEntity user = getCurrentUser ();
470
450
Long ownerId = Objects .nonNull (entity .getCreatedBy ()) ? entity .getCreatedBy ().getId () : null ;
471
-
472
- if (!(user .getId ().equals (ownerId ) || isAdmin () || permissionService .hasWriteAccess (entity ) || TagSecurityUtils .canManageTags ())) {
473
- throw new ForbiddenException ();
474
- }
451
+ return user .getId ().equals (ownerId );
475
452
}
476
453
477
454
protected <T extends CommonEntityDTO > List <T > listByTags (List <? extends CommonEntityExt <? extends Number >> entities ,
0 commit comments