Example usage for org.apache.shiro.authz UnauthorizedException UnauthorizedException

List of usage examples for org.apache.shiro.authz UnauthorizedException UnauthorizedException

Introduction

In this page you can find the example usage for org.apache.shiro.authz UnauthorizedException UnauthorizedException.

Prototype

public UnauthorizedException(Throwable cause) 

Source Link

Document

Constructs a new UnauthorizedException.

Usage

From source file:cn.guoyukun.spring.web.controller.permission.PermissionList.java

License:Apache License

public void assertHasPermission(String permission, String errorCode) {
    if (StringUtils.isEmpty(errorCode)) {
        errorCode = getDefaultErrorCode();
    }/*from   w w  w  .  ja v a2 s  .  c  o m*/
    String resourcePermission = resourcePermissions.get(permission);
    if (resourcePermission == null) {
        resourcePermission = this.resourceIdentity + ":" + permission;
    }
    if (!SecurityUtils.getSubject().isPermitted(resourcePermission)) {
        throw new UnauthorizedException(MessageUtils.message(errorCode, resourcePermission));
    }
}

From source file:cn.guoyukun.spring.web.controller.permission.PermissionList.java

License:Apache License

public void assertHasAllPermission(String[] permissions, String errorCode) {
    if (StringUtils.isEmpty(errorCode)) {
        errorCode = getDefaultErrorCode();
    }//from   w  w w.j a  va  2  s.  co m

    if (permissions == null || permissions.length == 0) {
        throw new UnauthorizedException(
                MessageUtils.message(errorCode, resourceIdentity + ":" + Arrays.toString(permissions)));
    }

    Subject subject = SecurityUtils.getSubject();

    for (String permission : permissions) {
        String resourcePermission = resourcePermissions.get(permission);
        if (resourcePermission == null) {
            resourcePermission = this.resourceIdentity + ":" + permission;
        }
        if (!subject.isPermitted(resourcePermission)) {
            throw new UnauthorizedException(
                    MessageUtils.message(errorCode, resourceIdentity + ":" + Arrays.toString(permissions)));
        }
    }

}

From source file:cn.guoyukun.spring.web.controller.permission.PermissionList.java

License:Apache License

public void assertHasAnyPermission(String[] permissions, String errorCode) {
    if (StringUtils.isEmpty(errorCode)) {
        errorCode = getDefaultErrorCode();
    }/* w  w  w.  j  a  v  a 2  s .c om*/
    if (permissions == null || permissions.length == 0) {
        throw new UnauthorizedException(
                MessageUtils.message(errorCode, resourceIdentity + ":" + Arrays.toString(permissions)));
    }

    Subject subject = SecurityUtils.getSubject();

    for (String permission : permissions) {
        String resourcePermission = resourcePermissions.get(permission);
        if (resourcePermission == null) {
            resourcePermission = this.resourceIdentity + ":" + permission;
        }
        if (subject.isPermitted(resourcePermission)) {
            return;
        }
    }

    throw new UnauthorizedException(
            MessageUtils.message(errorCode, resourceIdentity + ":" + Arrays.toString(permissions)));
}

From source file:com.axelor.dms.db.repo.DMSFileRepository.java

License:Open Source License

@Override
@SuppressWarnings("unchecked")
public Map<String, Object> validate(Map<String, Object> json, Map<String, Object> context) {
    final DMSFile file = findFrom(json);
    final DMSFile parent = findFrom((Map<String, Object>) json.get("parent"));
    if (parent == null) {
        return json;
    }/*www. ja v  a2  s.c o m*/
    if (file != null && file.getParent() == parent) {
        return json;
    }

    // check whether user can create/move document here
    if (file == null && !canCreate(parent)) {
        throw new UnauthorizedException(I18n.get("You can't create document here."));
    }
    if (file != null && file.getParent() != parent && !canCreate(parent)) {
        throw new UnauthorizedException(I18n.get("You can't move document here."));
    }

    return json;
}

From source file:com.blogzhou.web.sys.user.web.controller.UserOnlineController.java

License:Apache License

@Override
public String list(Searchable searchable, Model model) {
    if (!SecurityUtils.getSubject().isPermitted("sys:userOnline:view or monitor:userOnline:view")) {
        throw new UnauthorizedException(
                MessageUtils.message("no.view.permission", "sys:userOnline:viewmonitor:userOnline:view"));
    }//w  w  w  . j  a v a  2 s .  c o  m
    return super.list(searchable, model);
}

From source file:com.blogzhou.web.sys.user.web.controller.UserOnlineController.java

License:Apache License

@RequestMapping("/forceLogout")
public String forceLogout(@RequestParam(value = "ids") String[] ids) {

    if (!SecurityUtils.getSubject().isPermitted("sys:userOnline or monitor:userOnline")) {
        throw new UnauthorizedException(
                MessageUtils.message("no.view.permission", "sys:userOnlinemonitor:userOnline"));
    }//from  ww w.  j a va  2  s.  c  om

    for (String id : ids) {
        UserOnline online = baseService.findOne(id);
        if (online == null) {
            continue;
        }
        OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getId());
        if (onlineSession == null) {
            continue;
        }
        onlineSession.setStatus(OnlineSession.OnlineStatus.force_logout);
        online.setStatus(OnlineSession.OnlineStatus.force_logout);
        baseService.update(online);
    }
    return redirectToUrl(null);
}

From source file:com.epimorphics.registry.security.BaseUserStore.java

License:Apache License

private void checkIsAdministrator() {
    try {/*from   w w  w  . jav a2  s. c  om*/
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            throw new UnauthenticatedException();
        }
        if (!subject.hasRole(RegAuthorizationInfo.ADMINSTRATOR_ROLE)) {
            throw new UnauthorizedException("You must be an administrator to do this");
        }
    } catch (UnavailableSecurityManagerException e) {
        // Allow to proceed if no security system is configured
    }
}

From source file:com.epimorphics.registry.security.RegRealm.java

License:Apache License

protected void checkPermission(Permission permission, AuthorizationInfo info) {
    if (!isPermitted(permission, info)) {
        String msg = "User is not permitted [" + permission + "]";
        throw new UnauthorizedException(msg);
    }// www. jav a2s .c  om
}

From source file:com.framework.demo.web.controller.sys.user.web.controller.UserOnlineController.java

License:Apache License

public String list(Searchable searchable, Model model) throws Exception {
    if (!SecurityUtils.getSubject().isPermitted("sys:userOnline:view or monitor:userOnline:view")) {
        throw new UnauthorizedException(
                MessageUtils.message("no.view.permission", "sys:userOnline:viewmonitor:userOnline:view"));
    }//w ww .j  av a 2 s.  c o m
    return super.list(searchable, model);
}

From source file:com.framework.demo.web.controller.sys.user.web.controller.UserOnlineController.java

License:Apache License

@RequestMapping("/forceLogout")
public String forceLogout(@RequestParam(value = "ids") String[] ids) {

    if (!SecurityUtils.getSubject().isPermitted("sys:userOnline or monitor:userOnline")) {
        throw new UnauthorizedException(
                MessageUtils.message("no.view.permission", "sys:userOnlinemonitor:userOnline"));
    }//from ww w  .java2 s .c o m

    for (String id : ids) {
        SysUserOnline online = (SysUserOnline) baseService.findById(id);
        if (online == null) {
            continue;
        }
        OnlineSession onlineSession = (OnlineSession) onlineSessionDAO.readSession(online.getId());
        if (onlineSession == null) {
            continue;
        }
        onlineSession.setStatus(OnlineSession.OnlineStatus.force_logout);
        online.setStatus(OnlineSession.OnlineStatus.force_logout.getInfo());
        baseService.saveOrUpdate(online);
    }
    return redirectToUrl(null);
}