How to check if a user has a specific role in Meteor

I want to manage the users of my Meteor application, and for this I will need to find out their current roles. I have a page setup available only to admin users, and this page is subscribed to a collection of users.

In my template for this page, I have the following:

{{#each user}} <p> <a href="/@{{username}}">{{username}}</a> {{#if isInRole 'admin'}} Admin{{/if}} </p> {{/each}} 

Unfortunately, this leaves me with a problem when the user role in the log (which is the administrator) is mapped in the {{#if isInRole 'admin'}} block. This leads to the fact that all users have administrator status (which is not the case).

How to check if a user who is displayed in each block is in a specific role?

Edit Note: I am using the alanning / meteor-role package

The database has a list of all users, and I want to see their administrator status.

+5
source share
2 answers

I have the following solution for anyone who faces this problem in the future.

JavaScript:

 Template.registerHelper('isUserInRole', function(userId, role) { return Roles.userIsInRole(userId, role); }); 

Template:

 <p> Roles: {{#if isUserInRole _id 'webmaster'}}Webmaster {{/if}} {{#if isUserInRole _id 'admin'}}Admin {{/if}} </p> 
+3
source

You can create your own role verification functions as follows:

 isAdmin = function(){ var loggedInUser = Meteor.user(); var result = false; if(loggedInUser){ if (Roles.userIsInRole(loggedInUser, ['Admin'])){ result = true; } } return result; }; 

So save this in ./lib/roles.js , for example.

You will need to install the alanning: role package in order to use it.

+1
source

Source: https://habr.com/ru/post/1241440/


All Articles