javascript - 用户授权不适用于 Mean.JS

标签 javascript acl mean meanjs

我使用mean.js让注册用户访问内容。这有点起作用。我可以将 isAllowed 更改为 !isAllowed 让人们看到内容。问题是用户登录时内容未获得授权。文章示例工作正常。但是当我创建自己的部分时,登录用户无法访问页面!

所以基本上,如果我登录,如果我尝试访问 localhost:3000/requestoffwork,我会收到消息:“用户未授权”

如果我登录并将 isAllowed 更改为 !isAllowed,我就可以访问它

'use strict';

/**
 * Module dependencies.
 */
var acl = require('acl');

// Using the memory backend
acl = new acl(new acl.memoryBackend());

/**
 * Invoke Articles Permissions
 */
exports.invokeRolesPolicies = function () {
  acl.allow([{
    roles: ['admin'],
    allows: [{
      resources: '/api/articles',
      permissions: '*'
    }, {
      resources: '/api/articles/:articleId',
      permissions: '*'
    }]
  }, {
    roles: ['user'],
    allows: [{
      resources: '/requestoffwork',
      permissions: '*'
    }, {
      resources: '/api/articles/:articleId',
      permissions: ['get']
    }]
  }, {
    roles: ['guest'],
    allows: [{
      resources: '/api/articles',
      permissions: ['get']
    }, {
      resources: '/api/articles/:articleId',
      permissions: ['get']
    }]
  }]);
};

/**
 * Check If Articles Policy Allows
 */
exports.isAllowed = function (req, res, next) {
  var roles = (req.user) ? req.user.roles : ['guest'];

  // If an article is being processed and the current user created it then allow any manipulation
  if (req.article && req.user && req.article.user.id === req.user.id) {
    return next();
  }

  // Check for user roles
  acl.areAnyRolesAllowed(roles, req.route.path, req.method.toLowerCase(), function (err, isAllowed) {
    if (err) {
      // An authorization error occurred.
      return res.status(500).send('Unexpected authorization error');
    } else {
      if (isAllowed) {
        // Access granted! Invoke next middleware
        return next();
      } else {
        return res.status(403).json({
          message: 'User is not authorized'
        });
      }
    }
  });
};

这是路线

app.route('/requestoffwork').all(managementPolicy.isAllowed)
    .get(management.list)
    .post(management.submit);

这是用户的数据

{"_id":"5788fe46587a1c0b07a04078","displayName":"","provider":"local","__v":0,"created":"2016-07-15T15:16:22.625Z","roles":["user"],"profileImageURL":"modules/users/client/img/profile/default.png","email":"email@gmail.com","lastName":"","firstName":”"}

最佳答案

您是否也向客户端路由添加了权限?

modules/youModule/client/config/youModule.client.routes.js中添加以下内容:

  function routeConfig($stateProvider) {
    $stateProvider
      .state('yourState', {
        abstract: true,
        url: '/requestoffwork',
        template: '<ui-view/>',
        data: {
          roles: ['user'], //here you must specify the roles as well
          pageTitle: 'requestoffwork'
        }
      })
    }

希望这有帮助。

关于javascript - 用户授权不适用于 Mean.JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38459896/

相关文章:

javascript - 控制台登录 WordPress 站点时出现 JS 错误,但 html 站点中却没有

javascript - AngularJs:将 ng-model 绑定(bind)到单选按钮列表

javascript - 如何从 html/javascript 或 jquery 调用 WSO2 数据服务

Powershell 使用 get-acl 只返回用户/组名?

c++ - 带行的列 vector - 带 std::accumulate?

javascript - 为 Vue Select2 包装器组件使用动态 AJAX URL

windows - Windows 在哪里存储 ACL,ACL 是否会跟随一个文件从一台机器到另一台机器?

cakephp acl 控制插件的访问(FullCalendar)

javascript - 使用 d3.js 计算平均 json

python - pandas 和 numpy 之间 std 的不同结果