Copied to clipboard

Flag this post as spam?

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


  • Ross Ekberg 124 posts 365 karma points
    Jan 31, 2019 @ 22:29
    Ross Ekberg
    0

    Get User Groups

    Umbraco 7.13.1

    I have a .NET macro that I am migrating from an older version of Umbraco (7.4.2) and I need to determine what groups the current user is a part of.

    I am coding in Visual Basic. Every hint and tip I have found thus far seems to reference methods that don't exist.

    Here is the code I was previously using that no longer works:

    If umbraco.BusinessLogic.User.GetCurrent IsNot Nothing Then
           Dim currentUser As String = umbraco.BusinessLogic.User.GetCurrent.UserType.Name
           If currentUser = "Administrators" Then
               showAdmin = True
           End If
     End If
    
  • Rhys Hamilton 140 posts 942 karma points
    Feb 01, 2019 @ 10:01
    Rhys Hamilton
    0

    The umbraco.BusinessLogic.User namespace is marked as obsolete in the later versions of Umbraco 7, so you'll need to use the UserService instead to update your macro.

    Here's a basic C# example that gets the current user and lists the groups they belong too. Sorry it's not in VB, but hopefully it points you in the right direction.

        var username = UmbracoContext.Security.CurrentUser.Username;
        var user = ApplicationContext.Current.Services.UserService.GetByUsername(username);
    
        var groups = user.Groups;
    
        @foreach (var group in groups)
        {
            <p>@group.Name</p>
        }
    
  • Ross Ekberg 124 posts 365 karma points
    Feb 01, 2019 @ 16:00
    Ross Ekberg
    0

    Thank you for the quick response. This does help. My problem now is that I need to create an object reference to get access to the 'userservices' methods. Below is the requirements. I don't know what any of these types are. Is there an easier way?

    Public Sub New(provider As Umbraco.Core.Persistence.UnitOfWork.IDatabaseUnitOfWorkProvider, repositoryFactory As Umbraco.Core.Persistence.RepositoryFactory, logger As Umbraco.Core.Logging.ILogger, eventMessagesFactory As Umbraco.Core.Events.IEventMessagesFactory)
    
  • Rhys Hamilton 140 posts 942 karma points
    Feb 01, 2019 @ 16:41
    Rhys Hamilton
    1

    Hi Ross, I decided to create a quick test myself.

    Right enough, there's an issue retrieving the user - since there's no authentication at all (more here if you're interested), the UmbracoContext.Security.CurrentUserwon't work. This works fine for back-office stuff, but not on the front-end (that ones on me!)

    I've updated things, and the following solution below seems to work (again in C#).

    @using Umbraco.Core.Security;
    
    var userTicket = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket();
    if (userTicket != null)
    {
        var currentUser = ApplicationContext.Services.UserService.GetByUsername(userTicket.Name);
        var groups = currentUser.Groups;
    
        foreach (var group in groups)
        {
            <p>@group.Name</p>
        }
    }
    
  • Ross Ekberg 124 posts 365 karma points
    Feb 01, 2019 @ 16:51
    Ross Ekberg
    0

    Again, thank you for the response. However, I don't have any issue with getting the current user. My issue is accessing the 'UserServices' methods. Visual Studio is claiming that I need an object reference to do this since the methods are not shared. To create an object, I need to declare a new one, using the 'New' method I posted above.

    Again, it just seems like there is an easier way. What you are suggesting seems quite simple, but I don't know what your "UmbracoContext" or "ApplicationContext" are.

    Here is the code I typed up. It gets by the compiler but I get a runtime error saying I can't put 'nothing' in for the User Service.

            Dim username = umbraco.helper.GetCurrentUmbracoUser.LoginName
            Dim g As New umbraco.Core.Services.UserService(Nothing, Nothing, Nothing, Nothing)
            Dim user = g.GetByUsername(username)
    
            For Each group In user.Groups
                lblNote.Text += group.Name + "<br />"
            Next
    
  • Ross Ekberg 124 posts 365 karma points
    Feb 01, 2019 @ 17:16
    Ross Ekberg
    100

    I was able to figure it out thanks to your post and from this one:

    https://our.umbraco.com/forum/developers/api-questions/50075-how-to-get-the-current-user-with-UserService

    'ApplicationContext' is a member of 'Core'. I wasn't able to find the 'UmbracoContext' you referenced, but below is the finished solution:

    import umbraco.Core.Security 
    Dim ticket = New System.Web.HttpContextWrapper(System.Web.HttpContext.Current).GetUmbracoAuthTicket()
        If ticket IsNot Nothing Then
                Dim user = umbraco.Core.ApplicationContext.Current.Services.UserService.GetByUsername(ticket.Name)
                For Each group In user.Groups
                    If group.Name = "Administrators" Then showAdmin = True
                Next
        End If
    
Please Sign in or register to post replies

Write your reply to:

Draft