1

我是 Sails 后端开发的新手,我想知道如何防止用户列出他们不拥有的资源,这是我的模型:

模型/用户.js:

var bcrypt = require('bcryptjs')

module.exports = {
  attributes: {
    //  ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦  ╦╔═╗╔═╗
    //  ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗
    //  ╩  ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝
    password: {type: 'string', required: true},
    email: {type: 'string', required: true, unique: true},
    firstName: {type: 'string', allowNull: true},
    lastName: {type: 'string', allowNull: true},
    phoneNumber: {type: 'string', allowNull: true},

    //  ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
    //  ║╣ ║║║╠╩╗║╣  ║║╚═╗
    //  ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝

    //  ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
    //  ╠═╣╚═╗╚═╗║ ║║  ║╠═╣ ║ ║║ ║║║║╚═╗
    //  ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝
    bankAccounts: {collection: 'bankAccount', via: 'user'},
  },

  customToJSON: function() {
    return _.omit(this, ['password', 'updatedAt'])
  },
  beforeCreate: function(values, cb) {
    bcrypt.hash(values.password, 10, (err, hash) => {
      if (err) return cb(err)
      values.password = hash
      cb()
    })
  },
}

模型/BankAccount.js:

module.exports = {
  attributes: {
    //  ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦  ╦╔═╗╔═╗
    //  ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗
    //  ╩  ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝
    user: {model: 'user'},
    name: {type: 'string', required: true},
    iban: {type: 'string', required: true, unique: true, maxLength: 34},
    bic: {type: 'string', required: true, maxLength: 11},
    //  ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
    //  ║╣ ║║║╠╩╗║╣  ║║╚═╗
    //  ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝

    //  ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
    //  ╠═╣╚═╗╚═╗║ ║║  ║╠═╣ ║ ║║ ║║║║╚═╗
    //  ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝
  },
}

我想要的是防止用户在执行 a 时查看所有帐户,GET /user/并防止他在执行 a 时列出其他银行帐户,或者如果不是他的帐户之一则GET /bank_account/访问。GET /bank_account/:idid

要恢复,可以看作是一种isOwner策略,但找不到一个!

希望你能帮助我,我很清楚你可以理解,不要犹豫,告诉我是否可以提供更多细节或解释更多我的问题。

4

0 回答 0