node package manager

membership

Install

To install the most recent release from npm, run:

npm install membership

使用说明

membership主要功能是登录身份认证和访问控制部分,在数据库中需要设计用户集合,其中有用户ID, 用户名称,用户所属的角色,目前没有设置密码.

如下是一个使用例子:

var mongodb = require('mongodb');
var server = new mongodb.Server('127.0.0.1', 27017, {});
var client = new mongodb.Db('test', server, {});
var mongoStore = require('connect-mongodb');
var express = require('express');
var app = express.createServer();

`身份认证权限控制`
client.authority_config = { login_path: '/login', login_service_url: '/authentication', login_success_path: '/rooms' };
var membership = new (require('../index'))(client);
var authentication = membership.authentication;
var authorize = membership.authorization;

`定义共享环境`
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/static', { redirect: false }));
app.use(express.cookieParser());
app.use(express.session({ secret: 'foobar', store: new mongoStore({ db: client }), cookie: { maxAge: 1000 * 60 * 60 * 12} }));
app.use(express.bodyParser());
app.use(authentication);
app.use(app.router);

`有权限例子`
app.get('/rooms', authorize('doctor', 'nurse'), function (req, res) {
	res.send('successed login');
});

`无权限例子`
app.get('/combos', authorize('boss'), function (req, res) {
	res.send('combos');
});

`登录`
app.get('/login', function (req, res) {
	res.render('login', { layout: false });
});

client.open(function (error, client) {
    baseInformationInit(client)
	app.listen(3005);
});

function baseInformationInit(client) {
	var collenction = new mongodb.Collection(client, 'users');
	var users = [{ _id: '001', name: '0001', roles: ['doctor', 'nurse'] }, { _id: '002', name: '0002', roles: ['doctor'] }, { _id: '003', name: '0003', roles: ['nurse']}];
	collenction.drop(function (error) {
	  if (error) console.log(error.toString());
	});
	collenction.insert(users, { safe: true }, function (error, objects) {
	  if (error) console.log(error.toString());
	  else console.log('add users collection success!');
    });
}

process.on('exit', function () {
	client.close();
});