【发布时间】:2015-10-25 19:47:10
【问题描述】:
我已经开始使用 MEAN.js 全栈解决方案来学习如何创建 Web 应用程序。
我已经使用生成器创建了几个 CRUD 模型,它已经生成了所有的快速路线等......
它还创建了一个用户模型作为模板,其中包含一些角色和一些经过身份验证和授权的中间件。
我想稍微调整中间件,但我不知道最好的方法。
对于我的一个模型(称为主题公园),我想确保用户不仅已登录,而且还根据他们的角色获得授权以执行发布操作。
下面的代码是我目前从生成器获得的代码,正如您所见,'/themeparks/:themeparkId' 路由具有 themeparks.hasAuthorization 中间件函数调用。但是,这不适用于“/themeparks”路线,也不适用于 users.hasAuthorization 中间件功能。
所以我想知道将用户授权添加到“/themeparks”路线的 post 方法的最佳方法是什么?或许还有一些包含教程或涵盖 MEAN.js 堆栈中的用户模型的资源?
//路线
'use strict';
module.exports = function(app) {
var users = require('../../app/controllers/users.server.controller');
var themeparks = require('../../app/controllers/themeparks.server.controller');
// Themeparks Routes
app.route('/themeparks')
.get(themeparks.list)
.post(users.requiresLogin, themeparks.create); //<----How do I add authorization based on a role here??
app.route('/themeparks/:themeparkId')
.get(themeparks.read)
.put(users.requiresLogin, themeparks.hasAuthorization, themeparks.update)
.delete(users.requiresLogin, themeparks.hasAuthorization, themeparks.delete);
// Finish by binding the Themepark middleware
app.param('themeparkId', themeparks.themeparkByID);
};
//用户中间件
/**
* Require login routing middleware
*/
exports.requiresLogin = function(req, res, next) {
if (!req.isAuthenticated()) {
return res.status(401).send({
message: 'User is not logged in'
});
}
next();
};
/**
* User authorizations routing middleware
*/
exports.hasAuthorization = function(roles) {
var _this = this;
return function(req, res, next) {
_this.requiresLogin(req, res, function() {
if (_.intersection(req.user.roles, roles).length) {
return next();
} else {
return res.status(403).send({
message: 'User is not authorized'
});
}
});
};
};
//主题公园中间件
/**
* Themepark middleware
*/
exports.themeparkByID = function(req, res, next, id) { //TODO: This is probably what is pulling the user in through the middleware.
Themepark.findById(id).populate('user', 'displayName').exec(function(err, themepark) {
if (err) return next(err);
if (! themepark) return next(new Error('Failed to load Themepark ' + id));
req.themepark = themepark ;
next();
});
};
/**
* Themepark authorization middleware
*/
exports.hasAuthorization = function(req, res, next) {
if (req.themepark.user.id !== req.user.id) {
return res.status(403).send('User is not authorized');
}
next();
};
【问题讨论】:
标签: javascript node.js passport.js mean-stack meanjs