From fb87df7c8249236c2d858a17593f847cf456197c Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Sat, 3 Feb 2024 20:13:12 +0100 Subject: [PATCH 1/4] Fix #56 --- roles/roles_common.js | 35 +++++++++++++++++++++++++++++++++++ roles/roles_common_async.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/roles/roles_common.js b/roles/roles_common.js index 3dfcefe..9c2baff 100644 --- a/roles/roles_common.js +++ b/roles/roles_common.js @@ -1047,6 +1047,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 6ca1a9f..f82498f 100644 --- a/roles/roles_common_async.js +++ b/roles/roles_common_async.js @@ -1263,6 +1263,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. * From 24efbb7d2e836d8278039a348bea876f5ec4fea3 Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Sat, 3 Feb 2024 20:15:07 +0100 Subject: [PATCH 2/4] Types for isInRole function --- definitions.d.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/definitions.d.ts b/definitions.d.ts index f783854..05c70b0 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 From 6f8b19240b419f88ce75af5e21ef82a3826e6194 Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Sat, 3 Feb 2024 20:30:55 +0100 Subject: [PATCH 3/4] Add missing imports and ddp package --- package.js | 3 ++- roles/roles_common.js | 5 ++++- roles/roles_common_async.js | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/package.js b/package.js index b7b66da..98ed552 100644 --- a/package.js +++ b/package.js @@ -17,7 +17,8 @@ Package.onUse(function (api) { 'accounts-base', 'tracker', 'mongo', - 'check' + 'check', + 'ddp' ], both) api.use('zodern:types@1.0.11') diff --git a/roles/roles_common.js b/roles/roles_common.js index 9c2baff..468b5e9 100644 --- a/roles/roles_common.js +++ b/roles/roles_common.js @@ -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. diff --git a/roles/roles_common_async.js b/roles/roles_common_async.js index f82498f..0918408 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. From c05bd92c1244b822e6cb78805003e7561c045d68 Mon Sep 17 00:00:00 2001 From: Jan Dvorak Date: Mon, 22 Jul 2024 15:36:02 +0200 Subject: [PATCH 4/4] Remove package lock --- package-lock.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 package-lock.json diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 48e341a..0000000 --- a/package-lock.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "lockfileVersion": 1 -}