egg(47,48)--rbac之角色和权限关联,角色授权

news/2023/12/1 8:31:59

新增角色

clipboard.png

角色授权

clipboard.png

查找出所有的权限

clipboard.png

勾选上想要增加的权限(角色新增权限)

view

表单包括

  1. role_id(黄色框): 当前角色的id
  2. access_node(红色框): 把勾选上的权限id,都放到一个数组里
view/admin/role/auth.html

一级access_node

                                    <label>
                                        <input type="checkbox" class="module_cbo" <%if(list[i].checked){%> checked
                                        <%}%> value="<%=list[i]._id %>" name="access_node[]">&nbsp;&nbsp;
                                        <%=list[i].module_name %> <%=list[i]._id %>
                                    </label>

二级access_node

                                <td>
                                    <%for(var j=0;j<list[i].items.length;j++){%>
                                    &nbsp;&nbsp;
                                    <label>
                                        <input type="checkbox" class="action_cbo" <%if(list[i].items[j].checked){%>
                                        checked
                                        <%}%> value="<%=list[i].items[j]._id%>" name="access_node[]">
                                        &nbsp;&nbsp;
                                        <%=list[i].items[j].action_name%> <%=list[i].items[j]._id%></label>&nbsp;&nbsp;
                                    <%}%>
                                </td>

全部view

                    <form action="/admin/role/doAuth" method="POST">
                        <input type="hidden" name="_csrf" value="<%=csrf%>" />
                        <input type="hidden" name="role_id" value="<%=role_id%>" />
                        <table class="table table-bordered">
                            <%for(var i=0;i<list.length;i++){%>
                            <tr>
                                <td align="right" style="background: #D8E2FA; padding-right: 10px; width: 150px;">
                                    <label>
                                        <input type="checkbox" class="module_cbo" <%if(list[i].checked){%> checked
                                        <%}%> value="<%=list[i]._id %>" name="access_node[]">&nbsp;&nbsp;
                                        <%=list[i].module_name %> <%=list[i]._id %>
                                    </label>
                                </td>
                                <td>
                                    <%for(var j=0;j<list[i].items.length;j++){%>
                                    &nbsp;&nbsp;
                                    <label>
                                        <input type="checkbox" class="action_cbo" <%if(list[i].items[j].checked){%>
                                        checked
                                        <%}%> value="<%=list[i].items[j]._id%>" name="access_node[]">
                                        &nbsp;&nbsp;
                                        <%=list[i].items[j].action_name%> <%=list[i].items[j]._id%></label>&nbsp;&nbsp;
                                    <%}%>
                                </td>
                            </tr>
                            <%}%>
                        </table>
                        <button type="submit" class="btn btn-primary">提交</button>
                    </form>

clipboard.png

controller

controller/admin/role.js
  1. 删除当前角色下面的所有权限
  2. 把获取的权限和角色增加到数据库
  async doAuth() {
    /*
    1、删除当前角色下面的所有权限
    2、把获取的权限和角色增加到数据库
    */
    // console.log(this.ctx.request.body);
    var role_id = this.ctx.request.body.role_id;
    var access_node = this.ctx.request.body.access_node;
    // console.log(access_node)
    //1、删除当前角色下面的所有权限
    await this.ctx.model.RoleAccess.deleteMany({
      "role_id": role_id
    });

    //2、给role_access增加数据 把获取的权限和角色增加到数据库
    if (access_node) {
      for (var i = 0; i < access_node.length; i++) {
        var roleAccessData = new this.ctx.model.RoleAccess({
          role_id: role_id,
          access_id: access_node[i]
        })
        roleAccessData.save();
      }
    }
    await this.success('/admin/role/auth?id=' + role_id, "授权成功");
  }
  1. role_id(黄色框): 角色id
  2. access_id(右边红色框): 表单勾选上的权限id,要给角色增加的权限
  3. access_id(左边多个红色框): 查找role_access表,得到的role_access关联的数据

clipboard.png

查看授权的时候,把角色拥有的权限默认勾选上(角色查看权限)

view

view/admin/role/auth.html

左边是第一级<%if(list[i].checked){%>

                                    <label>
                                        <input type="checkbox" class="module_cbo" <%if(list[i].checked){%> checked
                                        <%}%> value="<%=list[i]._id %>" name="access_node[]">&nbsp;&nbsp;
                                        <%=list[i].module_name %> <%=list[i]._id %>
                                    </label>

右边是第二级<%if(list[i].items[j].checked){%>

                                <td>
                                    <%for(var j=0;j<list[i].items.length;j++){%>
                                    &nbsp;&nbsp;
                                    <label>
                                        <input type="checkbox" class="action_cbo" <%if(list[i].items[j].checked){%>
                                        checked
                                        <%}%> value="<%=list[i].items[j]._id%>" name="access_node[]">
                                        &nbsp;&nbsp;
                                        <%=list[i].items[j].action_name%> <%=list[i].items[j]._id%></label>&nbsp;&nbsp;
                                    <%}%>
                                </td>

clipboard.png

controller

  1. 获取全部的权限
  2. 查询当前角色拥有的权限(查询当前角色的权限id) 把查找到的数据放在数组中
  3. 循环遍历所有的权限数据 判断当前权限是否在角色权限的数组中, 如果在角色权限的数组中:选中 如果不在角色权限的数组中不选中
controller/admin/role.js
  async auth() {
    /*
     1、获取全部的权限  
     2、查询当前角色拥有的权限(查询当前角色的权限id) 把查找到的数据放在数组中
     3、循环遍历所有的权限数据     判断当前权限是否在角色权限的数组中,   如果在角色权限的数组中:选中    如果不在角色权限的数组中不选中
    */
    var role_id = this.ctx.request.query.id;
    //1、获取全部的权限
    var result = await this.ctx.model.Access.aggregate([
      {
        $lookup: {
          from: 'access',
          localField: '_id',
          foreignField: 'module_id',
          as: 'items'
        }
      },
      {
        $match: {
          "module_id": '0'
        }
      }
    ]);

    //2、查询当前角色拥有的权限(查询当前角色的权限id) 把查找到的数据放在数组中
    var accessReulst = await this.ctx.model.RoleAccess.find({
      "role_id": role_id
    });
    console.log(accessReulst)
    var roleAccessArray = [];
    accessReulst.forEach(function (value) {
      roleAccessArray.push(value.access_id.toString());
    })
    console.log(roleAccessArray)
    // console.log(roleAccessArray);

    // 3、循环遍历所有的权限数据     判断当前权限是否在角色权限的数组中
    for (var i = 0; i < result.length; i++) {
      if (roleAccessArray.indexOf(result[i]._id.toString()) != -1) {
        result[i].checked = true;
      }
      for (var j = 0; j < result[i].items.length; j++) {
        if (roleAccessArray.indexOf(result[i].items[j]._id.toString()) != -1) {
          result[i].items[j].checked = true;
        }
      }
    }
    // console.log(result);
    await this.ctx.render('admin/role/auth', {
      list: result,
      role_id: role_id
    });
  }
  1. 右边红色框为,当前角色(技术部门),所拥有的权限
  2. 左边红色框为,所有的权限列表

clipboard.png


http://www.niftyadmin.cn/n/2518599.html

相关文章

mosquitto 管理常用命令

发送信号&#xff1a; # kill -USR2 ps -e | grep mosquitto |awk {print $1} 启动&#xff1a; # mosquitto -c /etc/mosquitto/mosquitto.conf -d -v 查看端口占用&#xff1a; # netstat -anpt | grep 9001转载于:https://www.cnblogs.com/mkany/p/5091946.html

对偶理论和灵敏度分析---参数线性规划

为什么80%的码农都做不了架构师&#xff1f;>>> 参数 c的变化 参数 b的变化分析 转载于:https://my.oschina.net/liyangke/blog/2981225

HTML5新特性总结及对应详解

HTML是创建网页的标准标记语言&#xff0c;描述了使用标记的网页结构&#xff0c;元素由标签显示&#xff0c;浏览器不会显示HTML标签&#xff0c;而是使用它们呈现页面的内容。 目录 HTML5标记方法 新增的元素和删除的元素 表单相关元素新增的属性 全局属性 事件 canvas…

小鱼的数字游戏

#include<iostream> using namespace std; int main() {int a[200]; int z0; do{ z; cin>>a[z];} while(a[z]!0); for(int iz-1;i>1;i--) cout<<a[i]<<" "; return 0; }

2015.12.31 iOS程序准备(developer.apple.com)

程序启动的过程 .在桌面找到相应的应用的图标 点击图标 .main函数UIApplication类  Every app has exactly one instance of UIApplication  每个应用程序都只有一个UIApplication类的实例对象  运行起来的应用程序就是一个UIApplication对象。UIApplication…

Vuex 状态管理模式/插件

为什么要使用状态管理&#xff1f; 在开发整站时会有很多数据&#xff0c;也是由许多组件拼成&#xff0c;会共享一些状态&#xff0c;如用户的登录状态&#xff0c;或购物车信息在头部用到了&#xff0c;或在页面等其他位置也有使用到。我在其中一个位置进行增删&#xff0c;…

箭头函数使用及不能使用箭头函数的情况

基本用法 ES6允许使用“箭头”&#xff08;>&#xff09;定义函数 var f v > v; //一个参数// 等同于 var f function (v) {return v; };如果箭头函数不需要参数或者需要多个参数&#xff0c;就用一个圆括号表示参数部分。 var f () > 5; // 等同于 var f f…

Now.sh: 最好的 Serverless Deployment Dashboard

首发于 http://szhshp.org/tech/2018/12/09/deploymentyoursitenow.html 转载请注明 Now.sh: The best Serverless Deployment Dashboard 终于发现一个免费的比较方便的部署环境。 免费服务支持一天 1000 次 invocation, 所有形式的请求都算作 invocation, 反正我就部署一些小…

UINavigationBar

iOS项目&#xff0c;根据设计图&#xff0c;有时需要自定义UIView的UINavigationBar的背景。可以切出来一张1像素左右的背景图片&#xff0c;来充当UINavigationBar的背景。 可以利用NavigationBar的- (void)setBackgroundImage:(UIImage *)backgroundImage forBarMetrics:(UIB…

let、const、var的用法及区别

let ES6新增了let命令&#xff0c;用来声明变量。它的用法类似于var&#xff0c;但是所声明的变量&#xff0c;只在let命令所在的代码块内有效。 {var a 1;let b 2; } a; // 1 b; // ReferenceError: b is not defined上面代码在代码块中&#xff0c;分别用let和va…

ES6 Set数据结构的使用

ES6 内 的数据结构Set 特性&#xff1a;类似数组&#xff0c;但是它的最大特性就是所有元素都是唯一的&#xff0c;没有重复的值。 Set本身是一个构造函数&#xff0c;用来生成Set数据结构。 const s new Set();[2,3,3,5,6,7,7,4,3].forEach( x > s.add(x));for(let i o…

第27课 二阶构造模式(构造函数二阶构造)

1. 构造函数的回顾 &#xff08;1&#xff09;关于构造函数 ①类的构造函数用于对象的初始化 ②构造函数与类同名并且没有返回值&#xff08;思考&#xff1a;无返回值如何判断构造函数的执行结果&#xff1f;&#xff09; ③构造函数在对象定义时自动被调用 &#xff08;2&…

解构(Destructuring)赋值

在es5中我们进行相关赋值的时候&#xff0c;只能一个一个进行赋值&#xff0c;使用逗号的进行赋值也只是最右边的赋值 才生效。在es6中出现了赋值解构&#xff0c;分两种情况一个是数组解构&#xff0c;一个是对象解构赋值。这么好用的方法给我带来很多便利。 在解构中有以下两…

hibernate缓存说明

hibernate缓存说明&#xff1a;1.一级缓存&#xff08;session级别缓存&#xff09; 一级缓存&#xff0c;不是用来提升性能&#xff0c;是用来处理事务的2.二级缓存&#xff08;sessionFactory级别缓存&#xff09;&#xff1a; 二级缓存&#xff0c;对所有的session都…

MVVM框架理解

什么是MVVM&#xff1f;MVVM是Model-View-ViewModel的缩写。、 在了解MVVM之前&#xff0c;我们先回顾一下前端发展的历史。 在上个世纪的1989年&#xff0c;欧洲核子研究中心的物理学家Tim Berners-Lee发明了超文本标记语言&#xff08;HyperText Markup Language&#xff0…

设置text-overflow文本溢出隐藏时的对齐问题

设置text-overflow: ellipsis后引起的文本对齐问题 最近在做网页的时候用到了文本溢出隐藏的功能&#xff0c;但是出现了一些小问题&#xff0c;下面先放上示例代码吧。 <p><span class"left">Hello Hello Hello</span><span class"right&…
最新文章