Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes after removing underscore #292

Merged
merged 1 commit into from
Nov 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 32 additions & 47 deletions roles/roles_common.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
;(function () {

// borrowed from underscore
function isObject (obj) {
var type = typeof obj;
return type === 'function' || type === 'object' && !!obj;
}

const difference = (...arrays) =>
arrays.reduce((a, b) =>
a.filter((value) => !b.includes(value) )
)

/**
* Provides functions related to user authorization. Compatible with built-in Meteor accounts packages.
*
Expand Down Expand Up @@ -75,9 +64,9 @@ Object.assign(Roles, {

Roles._checkRoleName(roleName);

if (typeof options.unlessExists === 'undefined') {
options.unlessExists = false;
}
options = Object.assign({
unlessExists: false
}, options);

var result = Meteor.roles.upsert({_id: roleName}, {$setOnInsert: {children: []}});

Expand Down Expand Up @@ -224,7 +213,7 @@ Object.assign(Roles, {
* @param {String} roleName Name of role.
* @param {String} parentName Name of parent role.
* @private
* @static_.
* @static
*/
_addRoleToParent: function (roleName, parentName) {
var role,
Expand Down Expand Up @@ -478,7 +467,7 @@ Object.assign(Roles, {
}, options);

users.forEach(function (user) {
if (isObject(user)) {
if (typeof user === 'object') {
id = user._id;
}
else {
Expand Down Expand Up @@ -520,7 +509,7 @@ Object.assign(Roles, {
Roles._checkRoleName(roleName);
Roles._checkScopeName(options.scope);

if (isObject(user)) {
if (typeof user === 'object') {
id = user._id;
}
else {
Expand Down Expand Up @@ -643,20 +632,17 @@ Object.assign(Roles, {

Roles._checkScopeName(options.scope);

if (typeof options.ifExists === 'undefined') {
options.ifExists = false
}
if (typeof options._assigned === 'undefined') {

options = Object.assign({
// internal option, should not be used publicly because it will break assumptions
// in te code; publicly, you can only remove users from an assigned role
// when should the role be removed, default is `true` which means only when it is assigned,
// `false` means when it is not assigned, and `null` means always

options._assigned = true
}
_assigned: true
}, options);

users.forEach(function (user) {
if (!user) return;

roles.forEach(function (role) {
Roles._removeUserFromRole(user, role, options);
});
Expand Down Expand Up @@ -695,7 +681,7 @@ Object.assign(Roles, {
Roles._checkRoleName(roleName);
Roles._checkScopeName(options.scope);

if (isObject(user)) {
if (typeof user === 'object') {
id = user._id;
}
else {
Expand Down Expand Up @@ -772,7 +758,7 @@ Object.assign(Roles, {
Meteor.users.update(user._id, {
$pull: {
roles: {
$nor: setRoles.map((role) => ({ _id: role._id, scope: role.scope }))
$nor: setRoles.map(function (role) { return {_id: role._id, scope: role.scope } })
}
}
});
Expand Down Expand Up @@ -824,20 +810,21 @@ Object.assign(Roles, {

Roles._checkScopeName(options.scope);

if (typeof options.anyScope === 'undefined') {
options.anyScope = false;
}
options = Object.assign({
anyScope: false
}, options);

if (!user) return false;

if (isObject(user)) {
if (user.hasOwnProperty('roles')) {
if (typeof user === 'object') {
if ('roles' in user) {
return roles.some(function (role) {
const roles = user.roles || [];
if (options.anyScope) {
return (user.roles || []).some(Roles._roleMatcher(role));
return roles.some(Roles._roleMatcher(role));
}
else {
return (user.roles || []).some(Roles._roleAndScopeMatcher(role, options.scope));
return roles.some(Roles._roleAndScopeMatcher(role, options.scope));
}
})
} else {
Expand Down Expand Up @@ -903,11 +890,9 @@ Object.assign(Roles, {

if (!user) return [];

if (options.anyScope) {
roles = user.roles || [];
}
else {
roles = (user.roles || []).filter(Roles._scopeMatcher(options.scope));
roles = user.roles || [];
if (!options.anyScope) {
roles = roles.filter(Roles._scopeMatcher(options.scope));
}

if (options.onlyAssigned) {
Expand Down Expand Up @@ -1139,12 +1124,13 @@ Object.assign(Roles, {
*/
_resolveUser: function (user, force) {
// TODO: We could use $elemMatch to limit returned fields here.

if (!isObject(user)) {
if (!user) {
return null;
} else if (typeof user !== 'object') {
user = Meteor.users.findOne(
{_id: user},
{fields: {roles: 1}});
} else if (force || !user.hasOwnProperty('roles')) {
} else if (force || !('roles' in user)) {
user = Meteor.users.findOne(
{_id: user._id},
{fields: {roles: 1}});
Expand Down Expand Up @@ -1180,7 +1166,7 @@ Object.assign(Roles, {
return function (userRole) {
// == used on purpose in "userRole.scope == null"
return (userRole._id === roleName && userRole.scope === scope) ||
(userRole._id === roleName && (!userRole.hasOwnProperty('scope') || userRole.scope == null));
(userRole._id === roleName && (!('scope' in userRole) || userRole.scope == null));
};
},

Expand All @@ -1196,7 +1182,7 @@ Object.assign(Roles, {
return function (userRole) {
// == used on purpose in "userRole.scope == null"
return (userRole.scope === scope) ||
(!userRole.hasOwnProperty('scope') || userRole.scope == null);
(!('scope' in userRole) || userRole.scope == null);
};
},

Expand Down Expand Up @@ -1237,7 +1223,7 @@ Object.assign(Roles, {
* @static
*/
_normalizeOptions: function (options) {
options = typeof options === 'undefined' ? {} : options;
options = options === undefined ? {} : options;

if (options === null || typeof options === 'string') {
options = {scope: options};
Expand Down Expand Up @@ -1297,7 +1283,6 @@ Object.assign(Roles, {
return string.replace(/^\s+|\s+$/g, '');
}
}

}); // end Object.assign(Roles ...)
});

}());