使用MEAN.JS用户授权

Working with MEAN.JS users authorization

本文关键字:授权 用户 JS MEAN 使用      更新时间:2023-09-26

我已经开始使用MEAN.js全栈解决方案来学习如何创建web应用程序。

我已经使用生成器创建了几个CRUD模型,它已经生成了所有的快车路线等…

它还创建了一个User模型作为模板,其中包含一些角色和一些经过身份验证和授权的中间件。

我想稍微调整一下中间件,但我不知道最好的方法。

对于我的一个模型(名为themparks),我想确保用户不仅登录,而且根据他们的角色具有执行post操作的授权。

下面的代码是我目前从生成器中获得的代码,您可以看到'/themeparks/:themeparkId'路由具有主题公园。hasAuthorization中间件函数调用。然而,这在"/themparks"路由上不起作用,用户也不起作用。hasAuthorization中间件功能。

所以我想知道什么是最好的方式来添加用户授权的post方法的'/themeparks'路线?也许有一些资源有教程或涵盖了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);
};

/中间件/User

/**
 * 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();
};

您以相同的方式添加中间件:

app.route('/themeparks')
.get(themeparks.list)
.post(users.requiresLogin, themeparks.userRoleHasAuthorization, themeparks.create);

在themeparks模块中,添加这个函数,就像hasAuthorization函数一样,还包括一个检查用户是否在角色中。您已经获得了来自请求的用户id。使用它从数据库中查询该用户具有访问权限的角色。

(理想情况下,如果角色在数据库中,我会在检索用户对象本身时检索它,这样前端也可以在需要时使用角色信息,而不必在授权模块中查询。)

根据用户id和角色信息确定用户是否处于允许更新此模块的角色中。否则返回401 Not Authorized。下面是如何构建代码。canUpdate函数需要根据你希望如何存储角色和应该访问角色的模块的信息来实现。

exports.userRoleHasAuthorization = function(req, res, next) {
if (req.themepark.user.id !== req.user.id || !canUpdate(req.user.id, 'themeparks') {
    return res.status(403).send('User is not authorized');
}
next();
};