npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-authority

v0.1.2

Published

node access control module

Downloads

8

Readme

权限控制模块

要求

  • 用户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(permission)

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