Search in sources :

Example 1 with SysUserRole

use of com.cdeledu.model.rbac.SysUserRole in project wechat by dllwh.

the class SysUserOperateController method delUser.

/**
 * @方法描述: 用户删除
 * @创建者: 皇族灬战狼
 * @创建时间: 2016年9月27日 下午4:51:19
 * @param managerUser
 * @param request
 * @return
 */
@ResponseBody
@RequestMapping(value = "deleteUser")
public AjaxJson delUser(@RequestParam(value = "id", required = true, defaultValue = "-1") Integer id) {
    AjaxJson resultMsg = new AjaxJson();
    try {
        if (id != 1) {
            SysUser sysUser = new SysUser();
            sysUser.setId(id);
            List<SysUserRole> userRoleList = manageruserService.getUserRole(sysUser);
            if (userRoleList == null || userRoleList.isEmpty() || userRoleList.size() == 0) {
                manageruserService.delete(id);
                resultMsg.setMsg(MessageConstant.MSG_OPERATION_SUCCESS);
            } else {
                resultMsg.setSuccess(false);
                resultMsg.setMsg("删除失败,该用户已分配角色");
            }
        } else {
            resultMsg.setSuccess(false);
            resultMsg.setResultCode(403);
            resultMsg.setMsg("无法删除超级管理员账号");
        }
    } catch (Exception e) {
        resultMsg.setSuccess(false);
        resultMsg.setResultCode(10001);
        resultMsg.setMsg(MessageConstant.MSG_OPERATION_SUCCESS);
    }
    return resultMsg;
}
Also used : SysUser(com.cdeledu.model.rbac.SysUser) AjaxJson(com.cdeledu.common.base.AjaxJson) SysUserRole(com.cdeledu.model.rbac.SysUserRole) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with SysUserRole

use of com.cdeledu.model.rbac.SysUserRole in project wechat by dllwh.

the class SysUserOperateController method saveRoleUser.

/**
 * @方法描述: 用户-角色录入
 * @创建者: 皇族灬战狼
 * @创建时间: 2016年9月27日 下午4:51:27
 * @param managerUser
 * @param request
 * @return
 */
@ResponseBody
@RequestMapping(value = "roleAssign", params = "saveRoleUser")
@SystemLog(desc = "用户-角色录入(授权)", opType = SysOpType.INSERT, tableName = "sys_user_role")
public AjaxJson saveRoleUser(@RequestParam(value = "userCode", required = true) int id, @RequestParam(value = "roleID", defaultValue = "1", required = false) int roleID) {
    AjaxJson resultMsg = new AjaxJson();
    SysUser user = new SysUser();
    user.setId(id);
    try {
        if (roleID == 1) {
            // 超级管理员不参与分配
            resultMsg.setSuccess(false);
            resultMsg.setMsg("无法授予权限");
        }
        SysUser tSUser = manageruserService.findOneForJdbc(user);
        if (null == tSUser) {
            resultMsg.setMsg("不存在");
            resultMsg.setSuccess(false);
        } else {
            SysUserRole managerUserRole = new SysUserRole();
            managerUserRole.setUserId(tSUser.getId());
            managerUserRole.setRoleId(roleID);
            manageruserService.saveRoleUser(managerUserRole);
        }
    } catch (Exception e) {
        resultMsg.setResultCode(500);
        resultMsg.setMsg("分配角色时出现异常");
        resultMsg.setSuccess(false);
    }
    return resultMsg;
}
Also used : SysUser(com.cdeledu.model.rbac.SysUser) AjaxJson(com.cdeledu.common.base.AjaxJson) SysUserRole(com.cdeledu.model.rbac.SysUserRole) SystemLog(com.cdeledu.core.annotation.SystemLog) ResponseBody(org.springframework.web.bind.annotation.ResponseBody) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with SysUserRole

use of com.cdeledu.model.rbac.SysUserRole in project wechat by dllwh.

the class LoginController method doLogin.

/**
 * @方法:用户登录
 * @创建人:独泪了无痕
 * @param request
 * @return
 */
@RequestMapping(params = "doLogin")
public String doLogin(Model model) {
    SysUser managerUser = ShiroHelper.getPrincipal();
    List<SysUserRole> roleList = null;
    try {
        if (null != managerUser) {
            // 获取菜单、角色列表
            roleList = userService.getUserRole(managerUser);
            // 如果没有角色,则不允许登录
            if (roleList != null && roleList.size() > 0) {
                // model.addAttribute("avatar", );
                return "main/center";
            } else {
                model.addAttribute("tips", "该用户没有角色,无法登录");
                return FilterHelper.LOGIN_SHORT;
            }
        } else {
            model.addAttribute("tips", "该用户长时间未操作,请重新登录");
            return FilterHelper.LOGIN_SHORT;
        }
    } catch (Exception e) {
        return FilterHelper.LOGIN_SHORT;
    }
}
Also used : SysUser(com.cdeledu.model.rbac.SysUser) SysUserRole(com.cdeledu.model.rbac.SysUserRole) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with SysUserRole

use of com.cdeledu.model.rbac.SysUserRole in project wechat by dllwh.

the class ShiroRealm method doGetAuthorizationInfo.

/**
 * @方法描述: 为当前登录的Subject授予角色和权限
 * @说明: 该方法的调用时机为需授权资源被访问时,:并且每次访问需授权资源时都会执行该方法中的逻辑
 * @param principals
 * @return
 */
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    try {
        // ① 获取当前登录的用户名
        SysUser currentUser = (SysUser) principals.fromRealm(getName()).iterator().next();
        if (currentUser == null) {
            // 自动跳转到unauthorizedUrl指定的地址
            return null;
        }
        // ② 从数据库中获取当前登录用户的详细信息
        // ③ 获取当前登录用户的角色
        /**
         * 角色名的集合
         */
        Set<String> roleList = Sets.newConcurrentHashSet();
        List<SysUserRole> sysUserRolelist = userService.getUserRole(currentUser);
        for (SysUserRole role : sysUserRolelist) {
            if (role != null) {
                roleList.add(role.getRoleCode());
            }
        }
        // ④ 获取权限
        SimpleAuthorizationInfo simpleAuthorInfo = new SimpleAuthorizationInfo();
        // ④ 1.为当前用户设置角色
        simpleAuthorInfo.addRoles(roleList);
        // ④ 2.为当前用户设置访问权限
        List<String> opPerms = sysMenuService.getMenuPermsByUserId(currentUser);
        if (ListUtilHelper.isNotEmpty(opPerms)) {
            simpleAuthorInfo.addStringPermissions(opPerms);
        }
        return simpleAuthorInfo;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}
Also used : SimpleAuthorizationInfo(org.apache.shiro.authz.SimpleAuthorizationInfo) SysUser(com.cdeledu.model.rbac.SysUser) AuthenticationException(org.apache.shiro.authc.AuthenticationException) DisabledAccountException(org.apache.shiro.authc.DisabledAccountException) ExcessiveAttemptsException(org.apache.shiro.authc.ExcessiveAttemptsException) LockedAccountException(org.apache.shiro.authc.LockedAccountException) UnknownAccountException(org.apache.shiro.authc.UnknownAccountException) SysUserRole(com.cdeledu.model.rbac.SysUserRole)

Aggregations

SysUser (com.cdeledu.model.rbac.SysUser)4 SysUserRole (com.cdeledu.model.rbac.SysUserRole)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 AjaxJson (com.cdeledu.common.base.AjaxJson)2 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)2 SystemLog (com.cdeledu.core.annotation.SystemLog)1 AuthenticationException (org.apache.shiro.authc.AuthenticationException)1 DisabledAccountException (org.apache.shiro.authc.DisabledAccountException)1 ExcessiveAttemptsException (org.apache.shiro.authc.ExcessiveAttemptsException)1 LockedAccountException (org.apache.shiro.authc.LockedAccountException)1 UnknownAccountException (org.apache.shiro.authc.UnknownAccountException)1 SimpleAuthorizationInfo (org.apache.shiro.authz.SimpleAuthorizationInfo)1