【问题标题】:How can I filter Sails.js blueprint queries using a policy?如何使用策略过滤 Sails.js 蓝图查询?
【发布时间】:2017-04-09 04:28:48
【问题描述】:

我有一个isAuthorized 策略,如果用户被授权对给定模型和模型实例执行给定操作,则返回 true。

有没有办法将此策略应用于蓝图路由,例如,对 file 的 GET 请求仅返回允许当前用户执行 findOne 的文件?

同样,是否可以将相同的策略应用于蓝图填充结果,以便仅在填充数组中返回用户的一些关联文件?

为此,目前我正在覆盖每个控制器中的 find 操作,这不太理想。如果可以在不破坏蓝图路由/操作的情况下使用策略应用它,那就太棒了。

【问题讨论】:

    标签: javascript sails.js


    【解决方案1】:

    在我的 sails-permissions 模块中,我覆盖了sails.js response 类型,以便控制器只响应允许用户访问的模型。

    见:

    【讨论】:

    • 很好的帮助答案。不过,由于提交,链接现在已经失效,因此应该使用 permalink form 发布它们也许 =)。
    【解决方案2】:

    这是风帆的一部分,也许是一个新功能?

    http://sailsjs.com/documentation/concepts/policies#?using-policies-with-blueprint-actions

    {
      UserController: {
          find: ['isAuthorized', 'filterByUserId'],
          findOne: ['isAuthorized', 'filterByUserId']
      }
    }
    

    api/policies/filterByUserId.js

    module.exports = function filterByUserId(req, res, next) {
    
        if ( req.session.user ){
            // Use existing req.options.where, or initialize it to an empty object
            req.options.where = req.options.where || {};
    
            // Set the default `userId`
            req.options.where.id = req.session.user.id;
        }
        //safe to do if isAuthorized policy is enforced in tandem.
        return next();
    }
    

    【讨论】:

      猜你喜欢
      • 2015-02-14
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 1970-01-01
      • 1970-01-01
      • 2017-07-13
      • 1970-01-01
      相关资源
      最近更新 更多