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
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>
}
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)
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>
}
}
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
'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
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:
The
umbraco.BusinessLogic.User
namespace is marked as obsolete in the later versions of Umbraco 7, so you'll need to use theUserService
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.
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?
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.CurrentUser
won'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#).
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.
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:
is working on a reply...