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

isInRole, fix #56 #390

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions definitions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,17 @@ declare namespace Roles {
options?: string | { scope?: string; anyScope?: boolean }
): Promise<boolean>

/**
* 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
*/
function isInRole(role: string, scope: string[]): boolean
function isInRoleAsync(role: string, scope: string[]): Promise<boolean>

// The schema for the roles collection
interface Role {
_id: string
Expand Down
36 changes: 36 additions & 0 deletions roles/roles_client.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* global Roles */
import { Meteor } from 'meteor/meteor'
import { Match } from 'meteor/check'

/**
* Provides functions related to user authorization. Compatible with built-in Meteor accounts packages.
Expand Down Expand Up @@ -1041,6 +1042,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)
},

/**
* Normalize options.
*
Expand Down
36 changes: 36 additions & 0 deletions roles/roles_common_async.js
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.
Expand Down Expand Up @@ -1265,6 +1266,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)
Copy link
Member

Choose a reason for hiding this comment

The 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.
*
Expand Down
Loading