Javascript SailsJS:筛选查找以仅显示经过身份验证的用户拥有的资源
我是Sails后端开发的新手,我想知道如何防止用户列出他们不拥有的资源,以下是我的模型: models/User.js:Javascript SailsJS:筛选查找以仅显示经过身份验证的用户拥有的资源,javascript,orm,sails.js,sails-postgresql,Javascript,Orm,Sails.js,Sails Postgresql,我是Sails后端开发的新手,我想知道如何防止用户列出他们不拥有的资源,以下是我的模型: models/User.js: var bcrypt = require('bcryptjs') module.exports = { attributes: { // ╔═╗╦═╗╦╔╦╗╦╔╦╗╦╦ ╦╔═╗╔═╗ // ╠═╝╠╦╝║║║║║ ║ ║╚╗╔╝║╣ ╚═╗ // ╩ ╩╚═╩╩ ╩╩ ╩ ╩ ╚╝ ╚═╝╚═╝ password: {typ
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},
// ╔═╗╔╦╗╔╗ ╔═╗╔╦╗╔═╗
// ║╣ ║║║╠╩╗║╣ ║║╚═╗
// ╚═╝╩ ╩╚═╝╚═╝═╩╝╚═╝
// ╔═╗╔═╗╔═╗╔═╗╔═╗╦╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
// ╠═╣╚═╗╚═╗║ ║║ ║╠═╣ ║ ║║ ║║║║╚═╗
// ╩ ╩╚═╝╚═╝╚═╝╚═╝╩╩ ╩ ╩ ╩╚═╝╝╚╝╚═╝
},
}
我要做的是防止用户在执行GET/user/
操作时看到所有帐户,并防止用户在执行GET/bank\u帐户/
操作时列出除自己以外的其他银行帐户,或者如果id
不是他的帐户之一,则禁止用户访问GET/bank\u帐户/:id
要继续,可以将其视为isoowner
策略,但找不到策略
希望你能帮助我,我很清楚你能理解,如果我能提供更多细节或解释我的问题,请毫不犹豫地告诉我 好问题,我通常会在索引响应上为这些场景创建一个操作,因此类似于
GET/bank\u account/
的东西有一些逻辑,可以从会话中获取用户ID,并且只返回“bank accounts”的子集。有兴趣看看是否有其他解决方案,但我不这么认为。@Glen这是不是意味着您覆盖了默认的蓝图操作?是的,只需创建一个自定义路由,看看这里好问题,我通常会在索引响应上为这些场景创建一个操作,因此,像GET/bank\u account/
这样的东西有一些逻辑,可以从会话中获取用户ID,并且只返回“bank accounts”的子集。有兴趣看看是否有其他解决方案,但我不这么认为。@Glen这是否意味着您覆盖了默认的blueprint操作?是的,只需创建一个自定义路线,请看这里