Package ch.tocco.nice2.security.api
Class SecurityUtil
java.lang.Object
ch.tocco.nice2.security.api.SecurityUtil
Utility methods dealing with
Principal
and SecurityManager
.-
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkPermission
(Guard g, Permission... permissions) Checks all permissions against the given guard.static void
checkPermission
(Guard g, String... permissions) Checks all permissions against the given guard.static @Nullable Principal
Finds the current principal on the thread.static @Nullable String
Returns the username of the currently logged in principal ornull
if not logged in.static @Nullable SecurityContext
Returns theSecurityContext
.static @Nullable SecurityContext
Returns theSecurityContext
ensuring a valid thread.static boolean
hasPermission
(Guard g, Permission... permissions) Checks all permissions against the given guard.static boolean
hasPermission
(Guard g, String... permissions) Checks all permissions against the given guard.static boolean
Returnstrue
if the current principal exists and is anonymous.static boolean
Returnstrue
if the current principal exists and is not anonymous.static Principal
Always returns a valid principal.static SecurityContext
Returns theSecurityContext
, throwing an exception if it is not available.
-
Method Details
-
isAuthenticated
public static boolean isAuthenticated()Returnstrue
if the current principal exists and is not anonymous.- Returns:
true
if non-anonymous principal,false
otherwise
-
isAnonymous
public static boolean isAnonymous()Returnstrue
if the current principal exists and is anonymous.- Returns:
true
if anonymous principal,false
otherwise
-
getCurrentUsername
Returns the username of the currently logged in principal ornull
if not logged in. -
getCurrentPrincipal
Finds the current principal on the thread. If the thread is not setup correctly (noSecurityContext
available) this method returnsnull
.- Returns:
- the current principal or
null
-
requireCurrentPrincipal
Always returns a valid principal. Throws an exception, if no principal was found.- Returns:
- the current principal
-
getSecurityContext
Returns theSecurityContext
. If it is not available (i.e. the thread is not setup correctly)null
is returned.- Returns:
- the current security context or
null
-
getSecurityContextOnValidThread
Returns theSecurityContext
ensuring a valid thread. It will throw an exception if noExecutionContext
has been set. Otherwise it returns theSecurityContext
which may benull
. -
requireSecurityContext
Returns theSecurityContext
, throwing an exception if it is not available. -
checkPermission
Checks all permissions against the given guard. If one permission fails to authorize, an exception is thrown. -
checkPermission
Checks all permissions against the given guard. If one permission fails to authorize, an exception is thrown. -
hasPermission
Checks all permissions against the given guard. If one or more permission fails to authorize,false
is returned, otherwisetrue
. -
hasPermission
Checks all permissions against the given guard. If one or more permission fails to authorize,false
is returned, otherwisetrue
.
-