Get role of user

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BigBoss
    Member
    • Nov 2023
    • 94

    Get role of user

    Hello,
    How can I get if the authenticated user in the JS files and also the role?​​
  • abhilash.kumar.niit
    Member
    • Sep 2024
    • 86

    #2
    hi, can any one help in this ?? i also have same requirement.

    Comment

    • yuri
      Member
      • Mar 2014
      • 8845

      #3
      Within a view:

      Code:
      const hasRole = (this.getUser().attributes.rolesIds || []).includes(targetRoleId);
      Within any other class, need to inject the user: https://docs.espocrm.com/development...ncy-injection/
      If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

      Comment

      • onepoint0
        Member
        • Jul 2023
        • 45

        #4
        Hi Yuri,

        I'm trying to do this too. When I create a custom view for my entity I find that this.getUser().attributes.rolesIds is undefined and when I console log it I get the attributes object (correct username, email etc) but it doesn't contain the roleIds property:

        Code:
        "recordViews": {
            "detail": "custom:views/record/camera-detail"
        },
        
        ....................
        
        define('custom:views/record/camera-detail', ['views/record/detail'], function (Dep) {
            return Dep.extend({
          
                setup: function () {
                    Dep.prototype.setup.call(this);
                    console.log(`camera detail custom view `,this);
                    // const hasRole = (this.getUser().attributes.rolesIds || []).includes(targetRoleId);
                    console.log('role = ',this.getUser().attributes.rolesIds); // <-- undefined
                },
        
            });
        });
        My user definitely has a role:

        Click image for larger version  Name:	user-role.png Views:	0 Size:	36.7 KB ID:	116159

        Am I doing this wrong?
        Last edited by onepoint0; 03-24-2025, 02:32 AM.

        Comment

        • yuri
          Member
          • Mar 2014
          • 8845

          #5
          Indeed, roles are not loaded for the current user. Maybe consider using a custom field instead? It can be enum, multi-enum or link. Cannot be link-multiple as they are not loaded there.
          If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

          Comment

          Working...