diff --git a/definitions.d.ts b/definitions.d.ts index d8f4441..e7705f8 100644 --- a/definitions.d.ts +++ b/definitions.d.ts @@ -396,6 +396,17 @@ declare namespace Roles { options?: string | { scope?: string; anyScope?: boolean } ): Promise + /** + * 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 + // The schema for the roles collection interface Role { _id: string diff --git a/roles/roles_client.js b/roles/roles_client.js index e0ec9a7..f8d0955 100644 --- a/roles/roles_client.js +++ b/roles/roles_client.js @@ -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. @@ -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. * diff --git a/roles/roles_common_async.js b/roles/roles_common_async.js index 1107919..82e0cc3 100644 --- a/roles/roles_common_async.js +++ b/roles/roles_common_async.js @@ -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. @@ -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) + }, + /** * Normalize options. *