Search in sources :

Example 1 with PermissionLevel

use of com.jeesuite.common.constants.PermissionLevel in project jeesuite-libs by vakinge.

the class SecurityDelegating method doAuthorization.

/**
 * 鉴权
 * @param userId
 * @param uri
 */
public static UserSession doAuthorization() throws UnauthorizedException, ForbiddenAccessException {
    UserSession session = getCurrentSession();
    String uri = CurrentRuntimeContext.getRequest().getRequestURI();
    boolean isAdmin = session != null && session.getUser() != null && session.getUser().isAdmin();
    if (!isAdmin && !getInstance().resourceManager.isAnonymous(uri)) {
        if (session == null || session.isAnonymous()) {
            throw new UnauthorizedException();
        }
        String permissionKey = ApiPermssionCheckHelper.buildPermissionKey(CurrentRuntimeContext.getRequest().getMethod(), uri);
        PermissionLevel permissionLevel = ApiPermssionCheckHelper.matchPermissionLevel(getInstance().resourceManager, permissionKey);
        // 如果需鉴权
        if (permissionLevel == PermissionLevel.PermissionRequired) {
            List<String> permissions = getInstance().resourceManager.getUserPermissions(session);
            if (!ApiPermssionCheckHelper.checkPermissions(getInstance().resourceManager, permissionKey, permissions)) {
                throw new ForbiddenAccessException();
            }
        }
    }
    // 
    if (session != null) {
        CurrentRuntimeContext.setAuthUser(session.getUser());
        if (StringUtils.isNotBlank(session.getTenantId())) {
            CurrentRuntimeContext.setTenantId(session.getTenantId());
        }
        // 续租
        if (session.getExpiredAt() - System.currentTimeMillis() < SESSION_RNEWAL_BEFORE_MILLS) {
            getInstance().sessionManager.storageLoginSession(session);
        }
    }
    return session;
}
Also used : UserSession(com.jeesuite.security.model.UserSession) UnauthorizedException(com.jeesuite.common.exception.UnauthorizedException) PermissionLevel(com.jeesuite.common.constants.PermissionLevel) ForbiddenAccessException(com.jeesuite.common.exception.ForbiddenAccessException)

Aggregations

PermissionLevel (com.jeesuite.common.constants.PermissionLevel)1 ForbiddenAccessException (com.jeesuite.common.exception.ForbiddenAccessException)1 UnauthorizedException (com.jeesuite.common.exception.UnauthorizedException)1 UserSession (com.jeesuite.security.model.UserSession)1