CmsWing源码分析(7) 用户认证(二)

2021SC@SDUSC

auth.js文件中剩下的大致还剩下对角色的管理和权限列表的查看这些方法没分析过

本次博客当然就和这些后台操作相关了

目录

权限管理

栏目权限

角色的添加、删除、编辑

会员组的添加、删除、修改


权限管理

其一  获取用户列表

首先更新权限节点,使用的方法updaterules正是之前的一篇博客中详细分析过的方法,此处不再赘述

然后在条件状态“状态不为零”、“模板为管理员”、“类型为1”下获取用户的id、描述以及角色列表

再通过遍历的方法,选取出刚刚获取到的列表中唯一符合条件的一项

将网页标题命名为“权限管理”,并对网页的属性作出修改

最后能够展示权限管理这一页面

async accessAction() {
    await this.updaterules();// 更新权限节点
    const auth_role = await this.model('auth_role').where({status: ['!=', 0], module:'admin', 'type': 1}).field('id,desc,rule_ids').select();
    let this_role = {};
    auth_role.forEach(role => {
      if (role.id == this.get('id')) {
        this_role = role;
      }
    });
    this.active = 'admin/auth/index';
    this.meta_title = '权限管理';
    this.assign({
      'tactive': '/admin/user',
      'selfjs': 'auth',
      'thisid': this.get('id'),
      'auth_role': auth_role,
      'this_role': this_role
    });
    return this.display();
  }

其二  获取具体用户数据

要做的事首当其冲也是先更新权限节点

在条件状态“状态不为零”、“模板为管理员”、“类型为1”下获取用户的id、描述以及角色列表的做法也与前文一致

不同的是,这次同时也需要获取节点列表,以及一个条件为“模板为管理员”、“类型为1或2”、”状态为1“的名字、ID作为主要角色

遍历获取唯一需要的角色ID也与前文做法一致

对于主要角色,则将其遍历放入到数组m_rules中,这个数组将作为之后页面的主要角色参数,在网络通信间得到传输

async accessdataAction() {
    await this.updaterules();// 更新权限节点
    const auth_role = await this.model('auth_role').where({status: ['!=', 0], module: 'admin', 'type': 1}).field('id,desc,rule_ids').select();
    const node_list = await this.returnnodes();
    const map = {module: 'admin', type: ['IN', [1, 2]], status: 1};
    const main_rules = await this.model('auth_rule').where(map).field('name,id').select();
    let this_role = {};
    auth_role.forEach(role => {
      if (role.id == this.post('id')) {
        this_role = role;
      }
    });
    const m_rules = {};
    main_rules.forEach(v => {
      const obj = {};
      obj[v.name] = v.id;
      Object.assign(m_rules, obj);
    });
    const data = {
      'main_rules': m_rules,
      'node_list': node_list,
      'this_role': this_role
    };
    return this.json(data);
  }

栏目权限

栏目权限是指网站前台的栏目的管理,网站栏目包括了比如公司简介、产品介绍、新闻等栏目,可以在后台通过选择相应栏目授权,用户在添加内容的时候只能选择被授权栏目,没有被授权的内容无法管理。

首先需要初始化一个权限数组

在成功获取到服务器传输来的“priv_roleid”后

对数组进行遍历写入;对单个数据,直接在priv中添加一个包含这个数据内容的项,项的其他参数按照给定的来

 async cateprivAction() {
    if (this.isPost) {
      const data = this.post();
      // 构造权限
      const priv = [];
      if (!think.isEmpty(data.priv_roleid)) {
        if (think.isArray(data.priv_roleid)) {
          // 构造 角色权限
          for (const v of data.priv_roleid) {
            const arr = v.split(',');
            const obj = {};
            obj.catid = arr[1];
            obj.roleid = arr[2];
            obj.action = arr[0];
            obj.is_admin = 1;
            priv.push(obj);
          }
        } else {
          const arr = (data.priv_roleid).split(',');
          priv.push({ catid: arr[1], roleid: arr[2], action: arr[0], is_admin: 1 });
        }
      }

如果未能获得权限列表,则需要拿到栏目权限相关的缓存,将获取到的数据进行修改,成功后可以提示一下

一切顺利的话,就可以在网页的树节点这一栏,添加属性

这个属性是由gettree方法得到的,传入的参数包括不限于名字、ID、函数、pid、权限许可、状态和模型,甚至还有对于操作系统的判断

await this.model('category_priv').delete({where: {is_admin: 1, roleid: data.roleid}});
      if (!think.isEmpty(priv)) {
        await this.model('category_priv').addMany(priv);
      }
      think.cache('all_priv', null);// 栏目权限缓存
      return this.success({name: '修改成功!'});
    } else {
      const tree = await this.model('cmswing/category').gettree(0, 'id,name,title,sort,pid,allow_publish,status,model,mold,isapp');
      this.assign({
        'tactive': '/admin/user',
        'tree': tree
      });
      this.active = 'admin/auth/index';
      this.meta_title = '栏目权限';
      return this.display();

角色的添加、删除、编辑

从原理上来说几乎是一样的三个功能,细节有些许差别

在这里仅分析一下编辑这一块

在此之前,我们的开发人员设计了角色动作这个模板,它的data属性包含了draw和data

我看其他地方几乎没用到过,但为了全文理解,还是分析一下

async roleAction() {
    const gets = this.get();
    const draw = gets.draw;
    const res = await this.model('auth_role').field('id,desc,status,description').order('id ASC').select();
    const data = {
      'draw': draw,
      'data': res
    };
    return this.json(data);
  }

向服务器发送请求后,如能收到服务器返还的信息,则可以开始修改

依次向服务器发送id、描述、详细、数据等内容,并在发送data过后,检查是否成功,发出提示

如果未能接收到服务器返还的信息,获取本网页的id,对本网页进行修改...?

async roleeditAction() {
    if (this.isAjax('post')) {
      const id = this.post('id');
      const desc = this.post('desc');
      const description = this.post('description');
      const data = await this.model('auth_role').where({id: id}).update({desc: desc, description: description});
      if (data) {
        return this.success({ name: '修改成功!'});
      } else {
        return this.fail('修改失败!');
      }
    } else {
      const id = this.get('id');
      const res = await this.model('auth_role').where({id: id}).find();
      this.assign({
        data: res
      });
      return this.display();
    }
  }

会员组的添加、删除、修改

同样是三位一体的三种功能

我们同样选取编辑这一栏进行分析,因为编辑既涉及发送消息,又涉及接收消息,比较全面发展...

首先查看正在操作的管理者权限是否全部拥有

有了的话,也是在服务器能够收到回应的基础上,直接开始进行编辑

否则,修改本网页...?

async edituserAction() {
    if (this.isPost) {
      const data = this.post();
      data.allowpost = data.allowpost || 0;
      data.allowpostverify = data.allowpostverify || 0;
      data.allowupgrade = data.allowupgrade || 0;
      data.allowsendmessage = data.allowsendmessage || 0;
      data.allowattachment = data.allowattachment || 0;
      data.allowsearch = data.allowsearch || 0;
      const update = await this.model('member_group').where({groupid: data.groupid}).update(data);
      if (update) {
        return this.success({ name: '编辑成功!', url: '/admin/auth/index'});
      } else {
        return this.fail('编辑失败!');
      }
    } else {
      const info = await this.model('member_group').where({groupid: this.get('id')}).find();
      this.assign('info', info);
      this.meta_title = '编辑会员组';
      this.active = 'admin/auth/index';
      return this.display();
    }
  }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值