Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Louis Ferreira 69 posts 266 karma points
    Feb 21, 2016 @ 12:33
    Louis Ferreira
    0

    How to get user roles in Angular Controller?

    Hi, I have an angular controller controlling a dasboard in the back office. I would like to show/hide a button if the currently logged in user is in the administrator role. Is there an umbraco js service that does this already?

    I'm looking to do something like this...

    $scope.isAdmin = someService.userHasRole('administrator');
    

    then in the view I can...

    <div ng-hide="!isAdmin">
        <button ...
    

    Anyone done something like this before?

    Thnx

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 21, 2016 @ 14:30
    Jan Skovgaard
    100

    Hi Louis

    There is a userService, which you can use. It has a method called getCurrentUser()...

    So inject the userService into your controller and then call it like this

    var getCurrentUser = userService.getCurrentUser().then(function(userObj){
        var user = userObj;
        $scope.currentUser = user.id;
    });
    

    I Can't remember if this actually returns the user role but if it's only the root admin account you need to check for then you can use id as shown in the example and check whether the user id is 0 or not.

    Hope this helps a bit.

    /Jan

  • Louis Ferreira 69 posts 266 karma points
    Feb 21, 2016 @ 17:34
    Louis Ferreira
    0

    Awesome!

    That did the trick! I did search the Belle docs but did not find anything that looks like a user service tho'... anyway, it works.

    Thank you Jan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Feb 21, 2016 @ 18:14
    Jan Skovgaard
    0

    Hi Louis

    Happy to hear that :)

    Yeah, not everything is documented at the old belle site and it does not seem like it has been moved into the documentation section of our yet - So sometimes the way forward is to have a look at the Umbraco source code to get some ideas.

    Happy coding!

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft