Search in sources :

Example 1 with PermissionException

use of org.craftercms.commons.security.exception.PermissionException in project commons by craftercms.

the class HasPermissionAnnotationHandler method checkPermissions.

// cortiz, OK permissionEvaluator.isAllowed
@SuppressWarnings("unchecked")
@Around("@within(org.craftercms.commons.security.permissions.annotations.HasPermission) || " + "@annotation(org.craftercms.commons.security.permissions.annotations.HasPermission)")
public Object checkPermissions(ProceedingJoinPoint pjp) throws Throwable {
    boolean allowed;
    Method method = AopUtils.getActualMethod(pjp);
    HasPermission hasPermission = getHasPermissionAnnotation(method, pjp);
    Class<?> type = hasPermission.type();
    String action = hasPermission.action();
    Object securedObject = getAnnotatedSecuredObject(method, pjp);
    PermissionEvaluator permissionEvaluator = permissionEvaluators.get(type);
    if (securedObject != null) {
        logger.debug(LOG_KEY_METHOD_INT, method, hasPermission, securedObject);
    } else {
        logger.debug(LOG_KEY_METHOD_INT_NO_SEC_OBJ, method, hasPermission);
    }
    if (permissionEvaluator == null) {
        throw new PermissionException(ERROR_KEY_EVALUATOR_NOT_FOUND, type);
    }
    try {
        allowed = permissionEvaluator.isAllowed(securedObject, action);
    } catch (PermissionException e) {
        throw new PermissionException(ERROR_KEY_EVALUATION_FAILED, e);
    }
    if (allowed) {
        return pjp.proceed();
    } else if (securedObject != null) {
        throw new ActionDeniedException(hasPermission.action(), securedObject);
    } else {
        throw new ActionDeniedException(hasPermission.action());
    }
}
Also used : PermissionException(org.craftercms.commons.security.exception.PermissionException) PermissionEvaluator(org.craftercms.commons.security.permissions.PermissionEvaluator) ActionDeniedException(org.craftercms.commons.security.exception.ActionDeniedException) Method(java.lang.reflect.Method) Around(org.aspectj.lang.annotation.Around)

Aggregations

Method (java.lang.reflect.Method)1 Around (org.aspectj.lang.annotation.Around)1 ActionDeniedException (org.craftercms.commons.security.exception.ActionDeniedException)1 PermissionException (org.craftercms.commons.security.exception.PermissionException)1 PermissionEvaluator (org.craftercms.commons.security.permissions.PermissionEvaluator)1