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

super-roles

v0.0.1

Published

roles manager

Downloads

4

Readme

super-roles

基于Node.js的权限管理组件

原理

定义角色的访问权限,其中:

  • 用户特定配置优于角色配置
  • 访问列表的两种模式:
    • 普通模式:没有明确指定权限时默认全部允许(默认)
    • 严格模式:没有明确指定权限时默认全部禁止
  • 冲突:禁止优先于允许

使用方法

定义角色:

var roles = require('super-roles').create({
  strict: true
});

// 定义角色权限
roles.role('admin').define({
  'list user': true,    // 权限,true表示允许,false表示禁止
  'edit user': true,
  'delete user': true
});
// 继承并定义角色
roles.role('user').extend('admin').define({
  'list user': true,
  'edit user': false,
  'delete user': false
});

判断用户角色权限:

// 创建一个用户角色实例
var user = roles.user().role('admin').define({
  'add user': true
});

// 检查角色是否有权限
if (user.can('list user')) {
  console.log('can list user');
}
// 检查角色是否没有权限
if (user.cant('add user')) {
  console.log('cannot list user');
}
// 检查用户是否为指定角色
if (user.is('admin')) {
  console.log('is admin');
}

connect中间件,用于简单控制某个URL的访问权限:

app.use(roles.middleware({

  // 收到请求时,创建用户角色实例
  request: function (req, res, callback) {
    // req, res 为当前请求的request和response对象
    // 根据这些信息创建一个用户角色实例,通过callback返回
    // 此对象会保存到req.userRole
    callback(null, roles.user().role('admin').define({
      'POST /user': false,  // 定义访问页面: [请求方法] [URL]
      'GET /user': true
    }));
  },

  // 当角色被允许访问当前页面时执行,可选
  allow: function (req, res, next) {
    next();
  },

  // 当角色不被允许访问当前页面时执行,可选
  forbid: function (req, res, next) {
    next('Forbidden');
  }

}));

License

The MIT License (MIT)

Copyright (c) 2015 SuperID | 一切只为简单登录

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.