【Saas-export项目】15--(用户user角色role)给用户授予角色,更新角色授权

48 篇文章 1 订阅
37 篇文章 3 订阅



界面显示

在这里插入图片描述
在这里插入图片描述

后台代码

(1)Role.java添加check类

Role 回显的时候需要给用户已经拥有的角色打钩,通过checked来实现判断。

private boolean checked;//角色列表打勾

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

(2)TestRoleService.java测试类

src\test\java\com\smp\service\company

@Test
    public void test05(){
        //老王的角色列表
        String userId="002108e2-9a10-4510-9683-8d8fd1d374ef";
        String companyId="1";
        List<Role> all=iRoleService.findAll(companyId);
        //查找老王自己的
        List<Role> wanglist=iRoleService.findRolesByUserId(userId);
        l.info("test05 all="+all);
        l.info("test05 wanglist="+wanglist);
    }
    @Test
    public void test06(){
        //老王的角色列表
        String userId="002108e2-9a10-4510-9683-8d8fd1d374ef";
        String[] roleIds={"4028a1cd4ee2d9d6014ee2df4c6a0007"};
        iRoleService.updateUserRole(userId,roleIds);
    }

(3-1)IRoleService.java

src\main\java\com\smp\service\system\role

    //通过companyId查询该公司的所有角色列表(给用户授予角色的时候要显示所有角色列表,勾选授权)
    List<Role> findAll(String companyId);
    //通过userid查询用户的角色列表
    List<Role> findRolesByUserId(String userId);
    //更新用户的角色 (包括删除角色(deleteUserRoleByUserId)、新增角色两个(saveUserRole))
    void updateUserRole(String userId, String[] roleIds);

(3-2)RoleServiceImpl.java

src\main\java\com\smp\service\system\role\impl

@Override
    public List<Role> findAll(String companyId) {
        return iRoleDao.findAll(companyId);
    }

    @Override
    public List<Role> findRolesByUserId(String userId) {
        return iRoleDao.findByUserId(userId);
    }

    @Override
    public void updateUserRole(String userId, String[] roleIds) {
        //删除
        iRoleDao.deleteUserRoleByUserId(userId);
        for (String roleId:roleIds){
            iRoleDao.saveUserRole(userId,roleId);
        }
    }

(4-1)IRoleDao.java

src\main\java\com\smp\dao\system\role

//通多id查找用户
    List<Role> findByUserId(String userId);
    //通过用户id删除用户角色
    void deleteUserRoleByUserId(String userId);
    //保存用户角色
    void saveUserRole(String userId,String roleId);

(4-2)IRoleDao.xml

src\main\resources\com\smp\dao\system\role

<!--//通多id查找用户
    List<Role> findByUserId(String userId);-->
    <select id="findByUserId" parameterType="string" resultMap="roleMap">
        select *
            from pe_role_user ru inner join pe_role r
            on ru.role_id= r.role_id
            where ru.user_id = #{userId}
    </select>

    <!--//通过用户id删除用户角色
    void deleteUserRoleByUserId(String userId);-->
    <delete id="deleteUserRoleByUserId" parameterType="string">
        delete from pe_role_user  where user_id=#{userId}
    </delete>

    <!--//保存用户角色
    void saveUserRole(String userId,String roleId);-->
    <insert id="saveUserRole">
        insert into pe_role_user values(#{arg0},#{arg1})
    </insert>

前台代码

(1)UserController.java

src\main\java\com\smp\web\controller\system\user

@Autowired
    IRoleService iRoleService;
    @RequestMapping(path = "/toUserRole", method = {RequestMethod.GET, RequestMethod.POST})
    public String toUserRole(String userId){
        //接收页面传过来的userId
        l.info("toUserRole userId="+userId);
        //使用userId查找用户对象
        User user = iUserService.findUserById(userId);
        //转发给页面
        request.setAttribute("user",user);

        //所有角色的数据
        String companyId=getLoginCompanyId();
        List<Role> roleList =  iRoleService.findAll(companyId);
        //老王的角色数据
        List<Role> userRoleList =  iRoleService.findRolesByUserId(userId);
        l.info("toUserRole roleList = "+roleList);
        l.info("toUserRole userRoleList = "+userRoleList);

        for(Role role: roleList){
            //当前公司的所有的角色
            if(isInUserRoleList(role,userRoleList)){
                role.setChecked(true);
            }
        }
        //转发到页面
        request.setAttribute("roleList",roleList);
        request.setAttribute("userRoleList",userRoleList);
        return "system/user/user-role";
    }

    //当前的复选框 要不要打勾 取决于是否在 用户的角色列表中
    private boolean isInUserRoleList(Role role, List<Role> userRoleList) {
        for( Role r:userRoleList){
            if(r.getRoleId().equals(role.getRoleId())){
                return true;
            }
        }//end for
        return false;
    }

    //${path}/system/user/updateUserRole.do
    //userId
    //String[] roleIds
    @RequestMapping(path = "/updateUserRole", method = {RequestMethod.GET, RequestMethod.POST})
    public String updateUserRole(String userId,String[] roleIds){//接收用户的userId与角色的roleIds
        //String userId="002108e2-9a10-4510-9683-8d8fd1d374ef";
        //String[] roleIds = {"4028a1cd4ee2d9d6014ee2df4c6a0010"};
        l.info("updateUserRole userId = "+userId);
        l.info("updateUserRole roleIds = "+ Arrays.toString(roleIds));
        //用户的角色修改
        iRoleService.updateUserRole(userId,roleIds);
        //打开列表页面
        return "redirect:/system/user/toList.do";
    }

(2)role-list.jsp

<button type="button" class="btn btn-default" title="角色" onclick="roleList()"><i class="fa fa-user-circle-o"></i> 角色</button>
function roleList() {
        var id = getCheckId()
        if(id) {
            location.href="${path}/system/user/toUserRole.do?userId="+id;
        }else{
            alert("请勾选待处理的记录,且每次只能勾选一个")
        }
    }

(3)user-role.jsp

 <!-- 正文区域 -->
    <section class="content">
        <!-- .box-body -->
        <div class="box box-primary">
            <div class="box-header with-border">
                <h3 class="box-title">用户 [${user.userName}] 的角色列表</h3>
            </div>

            <div class="box-body">
                <form name="icform" method="post" >
                    <input type="hidden" name="userId" value="${user.userId}"/>

                    <div class="textbox" id="centerTextbox">
                        <div style="text-align:left">

                            <c:forEach items="${roleList}" var="role" varStatus="vs">
                                     <span style="padding:3px;margin-right:30px;width: 160px;display: inline-block">
                                         <input type="checkbox" name="roleIds" value="${role.roleId}"
                                             <%--                                               根据role对象中的checked属性进行判断,如果为true,则打勾,否不打勾--%>
                                                 <c:if test="${role.checked}">
                                                     checked
                                                 </c:if>

                                         />
                                         ${role.name}
                                     </span>
                            </c:forEach>
                        </div>
                    </div>
                </form>
            </div>
        </div>

        <div class="box-tools text-center">
            <button type="button" class="btn bg-maroon" onclick="formSubmit()">保存</button>
            <button type="button" class="btn bg-default" onclick="history.back(-1);">返回</button>
        </div>
    </section>
<script>
    //表单提交
    function formSubmit() {
        document.icform.action="${path}/system/user/updateUserRole.do";
        document.icform.submit();
    }
</script>
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值