首页 > 代码库 > 【JeeSite】角色和权限的修改

【JeeSite】角色和权限的修改

技术分享

技术分享
@RequiresPermissions("sys:role:edit")
    @RequestMapping(value = "save")
    public String save(Role role, Model model, RedirectAttributes redirectAttributes) {
        if(!UserUtils.getUser().isAdmin()&&role.getSysData().equals(Global.YES)){
            addMessage(redirectAttributes, "越权操作,只有超级管理员才能修改此数据!");
            return "redirect:" + adminPath + "/sys/role/?repage";
        }
        if(Global.isDemoMode()){
            addMessage(redirectAttributes, "演示模式,不允许操作!");
            return "redirect:" + adminPath + "/sys/role/?repage";
        }
        if (!beanValidator(model, role)){
            return form(role, model);
        }
        if (!"true".equals(checkName(role.getOldName(), role.getName()))){
            addMessage(model, "保存角色‘" + role.getName() + "‘失败, 角色名已存在");
            return form(role, model);
        }
        if (!"true".equals(checkEnname(role.getOldEnname(), role.getEnname()))){
            addMessage(model, "保存角色‘" + role.getName() + "‘失败, 英文名已存在");
            return form(role, model);
        }
        systemService.saveRole(role);
        addMessage(redirectAttributes, "保存角色‘" + role.getName() + "‘成功");
        return "redirect:" + adminPath + "/sys/role/?repage";
    }


========================================


@Transactional(readOnly = false)
    public void saveRole(Role role) {
        if (StringUtils.isBlank(role.getId())){
            role.preInsert();
            roleDao.insert(role);
            // 同步到Activiti
            saveActivitiGroup(role);
        }else{
            role.preUpdate();
            roleDao.update(role);
        }
        // 更新角色与菜单关联
        roleDao.deleteRoleMenu(role);
        if (role.getMenuList().size() > 0){
            roleDao.insertRoleMenu(role);
        }
        // 更新角色与部门关联
        roleDao.deleteRoleOffice(role);
        if (role.getOfficeList().size() > 0){
            roleDao.insertRoleOffice(role);
        }
        // 同步到Activiti
        saveActivitiGroup(role);
        // 清除用户角色缓存
        UserUtils.removeCache(UserUtils.CACHE_ROLE_LIST);
//        // 清除权限缓存
//        systemRealm.clearAllCachedAuthorizationInfo();
    }
View Code

页面传参数menuIds , 数据库保存用的参数是menuList, 中间通过setMenuIds--->setMenuIdList---->menuList

技术分享
public List<String> getMenuIdList() {
        List<String> menuIdList = Lists.newArrayList();
        for (Menu menu : menuList) {
            menuIdList.add(menu.getId());
        }
        return menuIdList;
    }

    public void setMenuIdList(List<String> menuIdList) {
        menuList = Lists.newArrayList();
        for (String menuId : menuIdList) {
            Menu menu = new Menu();
            menu.setId(menuId);
            menuList.add(menu);
        }
    }

    public String getMenuIds() {
        return StringUtils.join(getMenuIdList(), ",");
    }
    
    public void setMenuIds(String menuIds) {
        menuList = Lists.newArrayList();
        if (menuIds != null){
            String[] ids = StringUtils.split(menuIds, ",");
            setMenuIdList(Lists.newArrayList(ids));
        }
    }
role.java

只贴出保存权限的,角色的省略,sql语句有点变态,很长很长,这里只是对生成的sql语句截取部分

技术分享
    <insert id="insertRoleMenu">
        INSERT INTO sys_role_menu(role_id, menu_id)
        <foreach collection="menuList" item="menu" separator=" union all ">
            SELECT #{id}, #{menu.id}
            <if test="dbName != ‘mssql‘">
            FROM dual
            </if>
        </foreach>
    </insert>
    

==========================================

INSERT INTO sys_role_menu(role_id, menu_id) SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual union all SELECT ?, ? FROM dual
View Code

 

节点的值在保存提交的时候存在这里,

<form:hidden path="menuIds"/>

<form:hidden path="officeIds"/>

技术分享
submitHandler: function(form){
                    var ids = [], nodes = tree.getCheckedNodes(true);
                    for(var i=0; i<nodes.length; i++) {
                        ids.push(nodes[i].id);
                    }
                    $("#menuIds").val(ids);
                    var ids2 = [], nodes2 = tree2.getCheckedNodes(true);
                    for(var i=0; i<nodes2.length; i++) {
                        ids2.push(nodes2[i].id);
                    }
                    $("#officeIds").val(ids2);
                    loading(‘正在提交,请稍等...‘);
                    form.submit();
                },
View Code

 

有个地方注意一点,在roleDao.xml 的get 方法中,column="officeList.id" ,  因为后面命名了 ro.office_id AS "officeList.id":

<collection property="officeList" ofType="Office">
            <id property="id" column="officeList.id" />
        </collection>

 

【JeeSite】角色和权限的修改