1
我在迴環中遇到了acl問題。 我能夠正確地在數據庫中創建Role和RoleMapping集合。 問題是,當管理員嘗試創建一個新的產品,它說未經授權。有人能指導我做錯了什麼,或者指導我做正確的做法。如何在環回中正確設置ACL
project.json
{
"name": "product",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"admin": {
"type": "belongsTo",
"model": "admin",
"foreignKey": ""
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "READ",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "admin",
"permission": "ALLOW",
"property": "create"
}
],
"methods": {}
}
admin.json
{
"name": "admin",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"products": {
"type": "hasMany",
"model": "product",
"foreignKey": ""
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
}
],
"methods": {}
}
admin.js
'use strict';
module.exports = function(Admin) {
Admin.afterRemote('create', function(context, user, next) {
Admin.app.models.Role.upsertWithWhere({
name: 'admin'
},{name:'admin'}, function(err, role) {
if (err) throw err;
console.log('Created role:', role);
role.principals.create({
principalType: Admin.app.models.RoleMapping.ROLE,
principalId: user.id
}, function(err, principal) {
if (err) throw err;
console.log('Created principal:', principal);
});
});
next();
});
};