Search in sources :

Example 1 with PermissionLevel

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

the class SecurityDelegating method doAuthorization.

/**
 * 鉴权
 * @param userId
 * @param uri
 */
public static UserSession doAuthorization(String method, String uri) throws UnauthorizedException, ForbiddenAccessException {
    UserSession session = getCurrentSession();
    // 续租
    if (session != null) {
        long interval = System.currentTimeMillis() - getInstance().sessionManager.getUpdateTime(session);
        if (interval > SESSION_INTERVAL_MILLS) {
            getInstance().sessionManager.storageLoginSession(session);
        }
    }
    boolean isAdmin = session != null && session.getUser() != null && session.getUser().isAdmin();
    if (!isAdmin && !getInstance().resourceManager.isAnonymous(uri)) {
        if (session == null || session.isAnonymous()) {
            throw new UnauthorizedException();
        }
        if (getInstance().decisionProvider.apiAuthzEnabled()) {
            String permissionKey = ApiPermssionCheckHelper.buildPermissionKey(method, 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());
        }
    }
    return session;
}
Also used : UserSession(com.mendmix.security.model.UserSession) UnauthorizedException(com.mendmix.common.exception.UnauthorizedException) PermissionLevel(com.mendmix.common.constants.PermissionLevel) ForbiddenAccessException(com.mendmix.common.exception.ForbiddenAccessException)

Aggregations

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