Fix inconsistency inside the ACL, #4310

This commit is contained in:
Klaus 2017-08-28 16:47:54 -03:00
parent 96add5d92e
commit ade47254d3
2 changed files with 19 additions and 14 deletions

View File

@ -56,7 +56,11 @@ export class Acl {
let permissions = {};
roles.forEach((role) => {
permissions = deepMerge(permissions, this.config[role]);
// There is a big issue here, if we just send the content from the this.config
// inside the deepMerge, we change both permissions and the config.
// Couldn't find a better way to prevent the changing.
// The problems occurs in the `sources.shift()`.
permissions = deepMerge(permissions, JSON.parse(JSON.stringify(this.config[role])));
});
return permissions;
@ -66,6 +70,6 @@ export class Acl {
static containsRole(user) {
return Match.test(user, Object) &&
Match.test(user.roles, Array);
Match.test(user.roles, Array);
}
}

View File

@ -1,30 +1,31 @@
import { Match } from 'meteor/check';
export default function deepMerge(origin, ...sources) {
if (!sources.length) return origin;
export default function deepMerge(merge, ...sources) {
if (!sources.length) return merge;
const source = sources.shift();
let modOrigin = origin;
if (Match.test(origin, Array)) {
let merged = merge;
if (Match.test(merged, Array)) {
if (Match.test(source, Array)) {
origin.push(...source);
merged.push(...source);
} else {
origin.push(source);
merged.push(source);
}
} else if (Match.test(origin, Object)) {
} else if (Match.test(merged, Object)) {
if (Match.test(source, Object)) {
Object.keys(source).forEach((key) => {
if (!origin[key]) {
modOrigin[key] = source[key];
if (!merged[key]) {
merged[key] = source[key];
} else {
deepMerge(modOrigin[key], source[key]);
deepMerge(merged[key], source[key]);
}
});
}
} else {
modOrigin = source;
merged = source;
}
return deepMerge(modOrigin, ...sources);
return deepMerge(merged, ...sources);
}