-
-
Notifications
You must be signed in to change notification settings - Fork 169
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
isInRole, fix #56 #390
base: master
Are you sure you want to change the base?
isInRole, fix #56 #390
Changes from 5 commits
fb87df7
24efbb7
6f8b192
b7c17e3
c05bd92
2ef9b52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,7 @@ | ||
/* global Meteor, Roles, Mongo */ | ||
/* global Roles */ | ||
import { Meteor } from 'meteor/meteor' | ||
import { Mongo } from 'meteor/mongo' | ||
import { Match } from 'meteor/check' | ||
|
||
/** | ||
* Provides functions related to user authorization. Compatible with built-in Meteor accounts packages. | ||
|
@@ -1047,6 +1050,41 @@ Object.assign(Roles, { | |
return false | ||
}, | ||
|
||
/** | ||
* Check if current user is in at least one of the target roles. | ||
* @method isInRole | ||
* @param {String} role Name of role or comma-seperated list of roles. | ||
* @param {String} [scope] Optional, name of scope to check. | ||
* @return {Boolean} `true` if current user is in at least one of the target roles. | ||
* @static | ||
*/ | ||
isInRole: function (role, scope) { | ||
const userId = Meteor.userId() | ||
const comma = (role || '').indexOf(',') | ||
let roles | ||
|
||
if (!userId) return false | ||
if (!Match.test(role, String)) return false | ||
|
||
if (comma !== -1) { | ||
roles = role.split(',').reduce(function (memo, r) { | ||
if (!r) { | ||
return memo | ||
} | ||
memo.push(r) | ||
return memo | ||
}, []) | ||
} else { | ||
roles = [role] | ||
} | ||
|
||
if (Match.test(scope, String)) { | ||
return this.userIsInRole(userId, roles, scope) | ||
} | ||
|
||
return this.userIsInRole(userId, roles) | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a conditional return: return Match.test(scope, String)
? this.userIsInRole(userId, roles, scope)
: this.userIsInRole(userId, roles) |
||
|
||
/** | ||
* Normalize options. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/* global Roles */ | ||
import { Meteor } from 'meteor/meteor' | ||
import { Mongo } from 'meteor/mongo' | ||
import { Match } from 'meteor/check' | ||
|
||
/** | ||
* Provides functions related to user authorization. Compatible with built-in Meteor accounts packages. | ||
|
@@ -1263,6 +1264,41 @@ Object.assign(Roles, { | |
return false | ||
}, | ||
|
||
/** | ||
* Check if current user is in at least one of the target roles. | ||
* @method isInRoleAsync | ||
* @param {String} role Name of role or comma-seperated list of roles. | ||
* @param {String} [scope] Optional, name of scope to check. | ||
* @return {Promise} `true` if current user is in at least one of the target roles. | ||
* @static | ||
*/ | ||
isInRoleAsync: async function (role, scope) { | ||
const userId = Meteor.userId() | ||
const comma = (role || '').indexOf(',') | ||
let roles | ||
|
||
if (!userId) return false | ||
if (!Match.test(role, String)) return false | ||
|
||
if (comma !== -1) { | ||
roles = role.split(',').reduce(function (memo, r) { | ||
if (!r) { | ||
return memo | ||
} | ||
memo.push(r) | ||
return memo | ||
}, []) | ||
} else { | ||
roles = [role] | ||
} | ||
|
||
if (Match.test(scope, String)) { | ||
return await this.userIsInRoleAsync(userId, roles, scope) | ||
} | ||
|
||
return await this.userIsInRoleAsync(userId, roles) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Technically you don't need to await before returning and also don't need to make the function async, because it will return a promise anyway. If you resolve the promise before returning, there will be one more microtask execution than if returning the promise directly: isInRoleAsync: function (role, scope) {
// ...
return this.userIsInRoleAsync(userId, roles) // always returns Promise
} does not affect anything by itself but might be measurable when requests / operations do in the millions haha |
||
}, | ||
|
||
/** | ||
* Normalize options. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be simplified: