fixes #4074 Descargar ACL del usuario actual #1255

Open
pau wants to merge 40 commits from 4074-download-user-ACL into dev
3 changed files with 37 additions and 24 deletions
Showing only changes of commit 01daa253db - Show all commits

View File

@ -35,18 +35,32 @@ module.exports = Self => {
}); });
Self.acls = async function(ctx) { Self.acls = async function(ctx) {
const models = Self.app.models;
const acls = []; const acls = [];
const userId = ctx.req.accessToken.userId; const userId = ctx.req.accessToken.userId;
if (userId) { if (userId) {
const dynamicAcls = await Self.rawSql(` const roleMapping = await models.RoleMapping.find({
SELECT * where: {
FROM salix.ACL a principalId: userId
WHERE a.principalId IN ( },
SELECT r.name COLLATE utf8mb3_general_ci include: [
FROM salix.RoleMapping rm {
JOIN account.role r ON r.id = rm.roleId relation: 'role',
WHERE rm.principalId = ? scope: {
alexandre marked this conversation as resolved Outdated
Outdated
Review

Esta consulta pot ferse sense gastar SQL, sempre que es puga, fer-ho en funcions de loopback.

Esta consulta pot ferse sense gastar SQL, sempre que es puga, fer-ho en funcions de loopback.
)`, [userId]); fields: [
'name'
]
}
}
]
});
const dynamicAcls = await models.ACL.find({
where: {
principalId: {
inq: roleMapping.map(rm => rm.role().name)
}
}
});
dynamicAcls.forEach(acl => acls.push(acl)); dynamicAcls.forEach(acl => acls.push(acl));
staticAcls.get('$authenticated').forEach(acl => acls.push(acl)); staticAcls.get('$authenticated').forEach(acl => acls.push(acl));
} else } else

View File

@ -32,20 +32,12 @@ class AclService {
} }
hasAnyACL(model, property, accessType) { hasAnyACL(model, property, accessType) {
if (this.acls) { const acls = this.acls[model];
if (this.acls[model]) { if (acls) {
if (this.acls[model]['*']) { for (const prop of ['*', property]) {
if (this.acls[model]['*']['*']) const acl = acls[prop];
return true; if (acl && (acl['*'] || acl[accessType]))
if (this.acls[model]['*'][accessType]) return true;
return true;
}
if (this.acls[model][property]) {
if (this.acls[model][property]['*'])
return true;
if (this.acls[model][property][accessType])
return true;
}
} }
} }
return false; return false;

View File

@ -39,6 +39,13 @@
"mysql": { "mysql": {
"table": "salix.RoleMapping" "table": "salix.RoleMapping"
} }
},
"relations": {
"role": {
"type": "belongsTo",
"model": "Role",
"foreignKey": "roleId"
}
} }
}, },
"Schema": { "Schema": {