Socket
Socket
Sign inDemoInstall

node-authority

Package Overview
Dependencies
14
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    node-authority

node access control module


Version published
Maintainers
1
Install size
1.23 MB
Created

Readme

Source

权限控制模块

要求

  • 用户id需要存储在req.session.uid中。
  • 数据库使用mysql
  • 数据库中需要有四个表tb_authority、tb_role、tb_map、tb_admin

数据库表说明

表字段可以自由扩展,如下列出的字段不可更改

CREATE TABLE tb_authority(
  auth_id     bigint unsigned PRIMARY KEY AUTO_INCREMENT 	  #id,主键,自增
, name        varchar(40) not null unique                     #权限名
, code        varchar(40) not null unique                     #权限代码
, description varchar(80) not null default ''                 #描述
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE tb_role(
  role_id     bigint unsigned PRIMARY KEY AUTO_INCREMENT 	  #id,主键,自增
, name        varchar(40) not null unique                     #角色名
, description varchar(80) not null default ''                 #描述
, authority   varchar(1000) not null default '[]'             #类别权限 数组的json字符串
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE tb_map(                                          #管理员-权限组关系表
  roleid      bigint unsigned NOT NULL                        #tb_role name
, adminid     bigint unsigned NOT NULL                        #tb_admin id
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

CREATE TABLE tb_admin(                                        #管理表
  admin_id    bigint unsigned PRIMARY KEY AUTO_INCREMENT  	  #id,主键,自增
, username    varchar(50)     NOT NULL DEFAULT '' UNIQUE      #adminname
, realname    varchar(50)     NOT NULL                        #姓名,必填
, password    varchar(80)     NOT NULL                        #登录密码,必填
, disabled    tinyint         NOT NULL DEFAULT 0              #是否停封,默认否
, adminlevel  TINYINT unsigned NOT NULL DEFAULT 1             #账户等级0,1,2,3,...
, authority   varchar(1000)   NOT NULL default '[]'           #类别权限 数组的json字符串
, description varchar(200)    NOT NULL DEFAULT ''             #描述
)AUTO_INCREMENT=30001 ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

安装

npm install node-authroity --save

或者在package.json中加入如下字段

"node-authority": "^0.1.0"

初始化

var express = require('express');
var cookieParser = require('cookie-parser');
var redisSession = require('node-redis-session');

var app = express();

//cookie and session parser
app.use(cookieParser());
app.use(redisSession());

// hang middleware after session middleware
var authority = require('node-authority');
// configure authority
var dbinfo = {
	'host' : 'localhost',
	'port' : 3306,
	'user' : 'user',
	'password' : 'password',
	'database' : 'database'
};
authority.configure({mysql:dbinfo});
app.use(authority.authMiddleWare);

app.listen(3000);

权限举例说明

tb_authority.name为人类易读的权限名称,一般会作为检查权限时传入的参数,tb_authority.code为程序储存时的权限名称。tb_admin.authoritytb_role.authority为权限存储字段,存储方式为JSON.stringify()过后的数组。

API列表

  • check
check(permission)
/**
 * 检查当前用户是否拥有当前指定权限(传入参数)
 * 挂载到res.loacls下方便在页面渲染时使用
 */
req.check('Home.View');
res.locals.check('Home.View');

Keywords

FAQs

Last updated on 21 Feb 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc