1111
1212import java .util .List ;
1313import java .util .Set ;
14+ import java .util .stream .Collectors ;
1415
1516public class UserPermissions {
1617 private static final Log LOG = LogFactory .getLog (UserPermissions .class );
18+
1719 private UserPermissions () {
1820 }
1921
2022 public static void assertSuperUser (User user ) {
2123 if (user == null ) {
22- throw new UserRestrictionException ();
24+ throw new UserRestrictionException ("User is NULL" );
2325 }
2426
2527 if (!user .isSuperUser ()) {
26- throw new UserRestrictionException ();
28+ throw new UserRestrictionException ("User is no super user: " + user . getEmail () );
2729 }
2830 }
2931
3032 public static void assertInstitutionAdmin (User user ) {
3133 if (user == null ) {
32- throw new UserRestrictionException ();
34+ throw new UserRestrictionException ("User is NULL" );
3335 }
3436
3537 if (user .isSuperUser () || (user .isInstitutionAdmin () && StringUtils .hasText (user .getOrganizationGUID ()))) {
3638 return ;
3739 }
38- throw new UserRestrictionException ();
40+ throw new UserRestrictionException ("User is no institution admin: " + user . getEmail () );
3941 }
4042
4143 public static void assertAuthority (User user , Authority authority ) {
4244 if (user == null ) {
43- throw new UserRestrictionException ();
45+ throw new UserRestrictionException ("User is NULL" );
4446 }
4547 LOG .debug (String .format ("assertAuthority for user %s" , user .getEduPersonPrincipalName ()));
4648
@@ -54,21 +56,21 @@ public static void assertAuthority(User user, Authority authority) {
5456 return ;
5557 }
5658 if (user .getUserRoles ().stream ()
57- .noneMatch (userRole -> userRole .getAuthority ().hasEqualOrHigherRights (authority )))
58- throw new UserRestrictionException ();
59+ .noneMatch (userRole -> userRole .getAuthority ().hasEqualOrHigherRights (authority )))
60+ throw new UserRestrictionException (String . format ( "User %s is not an Authority %s" , user . getEmail (), authority ) );
5961 }
6062
6163 public static void assertValidInvitation (User user , Authority intendedAuthority , List <Role > roles ) {
6264 if (user == null ) {
63- throw new UserRestrictionException ();
65+ throw new UserRestrictionException ("User is NULL" );
6466 }
6567 LOG .debug (String .format ("assertValidInvitation for user %s" , user .getEduPersonPrincipalName ()));
6668
6769 if (user .isSuperUser ()) {
6870 return ;
6971 }
7072 if (intendedAuthority .equals (Authority .SUPER_USER )) {
71- throw new UserRestrictionException ();
73+ throw new UserRestrictionException ("Invalid invitation for super-user by " + user . getEmail () );
7274 }
7375 Set <UserRole > userRoles = user .getUserRoles ();
7476 //Institution admin needs to own all roles or be a member of the role for at least the authority of invitationo
@@ -85,21 +87,22 @@ public static void assertValidInvitation(User user, Authority intendedAuthority,
8587 return mayInviteByInstitutionAdmin || mayInviteByApplication || mayInviteByAuthority ;
8688 });
8789 if (!allowed ) {
88- throw new UserRestrictionException ();
90+ throw new UserRestrictionException (String .format ("Invalid invation by %s for roles %s" ,
91+ user .getEmail (), roles .stream ().map (role -> role .getName ()).collect (Collectors .joining (", " ))));
8992 }
9093 }
9194
9295 public static void assertRoleAccess (User user , Role accessRole , Authority authority ) {
9396 if (user == null ) {
94- throw new UserRestrictionException ();
97+ throw new UserRestrictionException ("USer is NULL" );
9598 }
9699 LOG .debug (String .format ("assertRoleAccess for user %s" , user .getEduPersonPrincipalName ()));
97100
98101 if (user .isSuperUser ()) {
99102 return ;
100103 }
101104 if (accessRole == null ) {
102- throw new UserRestrictionException ();
105+ throw new UserRestrictionException ("Role is NULL" );
103106 }
104107 if (user .isInstitutionAdmin () && user .getOrganizationGUID ().equals (accessRole .getOrganizationGUID ())) {
105108 return ;
@@ -110,7 +113,8 @@ public static void assertRoleAccess(User user, Role accessRole, Authority author
110113 (userRole .hasAccessToApplication (accessRole ) &&
111114 userRole .getAuthority ().hasEqualOrHigherRights (Authority .INSTITUTION_ADMIN )))
112115 .findFirst ()
113- .orElseThrow (UserRestrictionException ::new );
116+ .orElseThrow (() -> new UserRestrictionException (String .format ("User %s has no access to role %s" ,
117+ user .getEmail (), accessRole .getName ())));
114118 }
115119
116120 //Does one of the userRoles has Authority.MANAGE and has the same application as the role
0 commit comments