UMB_CURRENT_USER_CONTEXT & User Groups
# package-development
w
UMB_CURRENT_USER_CONTEXT & User Groups
I am trying to get the current user who is logged in and their user groups they are a part of. Such as Admin or Editor etc or any other custom user group. From what I can tell on UMB_CURRENT_USER_CONTEXT there is nothing that seems to contain this info. Is there a context where I can find this information as struggling where to find it?
I can obviously write my own API Controller and use the newer `IBackOfficeSecurityAccessor`in C# to get the current signed in user and look up the groups that way. But with the info in the back-office already, I had wondered if it exists on a different context and save me writing an API call.
If it does not exist in a context, can I suggest that it does as it can make for a useful condition. As a user may have access to have a section such as content but you may only want users in the Admin group to see a power user dashboard.
Any thoughts @Jacob Overgaard can I get this info in a context somewhere already??
j
maybe this can help you:
Copy code
cs
var cookieAuthenticatedUserAttempt =
            await HttpContext.AuthenticateAsync(Constants.Security.BackOfficeAuthenticationType);

        if (cookieAuthenticatedUserAttempt.Succeeded == false)
        {
            return Unauthorized();
        }

        BackOfficeIdentityUser? user = await _backOfficeUserManager.GetUserAsync(cookieAuthenticatedUserAttempt.Principal);
hmm, but that uses the cookie to check, which you dont necessarily have available
ahh, I think you can get the current user like this:
Copy code
cs
IUser user = CurrentUser(_backOfficeSecurityAccessor);
Copy code
cs
    protected static IUser CurrentUser(IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
        => backOfficeSecurityAccessor.BackOfficeSecurity?.CurrentUser ?? throw new InvalidOperationException("No backoffice user found");
w
Yeh I know how to do it with C# no problem in an API
But I would imagine other extension points depend on the user type. Only show this context action for super admin etc…
j
Oh, you meant frontend!
sorry
there's the current user context
UMB_CURRENT_USER_CONTEXT
it even has an
isCurrentUserAdmin()
function
w
Yep but nothing for the exact user groups
So admin, and writer perhaps
From what I could find
j
sure, you can use the id from the current user on the User group repository
w
How do I get the user group repo?
Is it a context?
j
sorry, UmbUserDetailRepository
repositories are never contexts, they can just be new'ed up
Copy code
ts
const userDetailRepository = new UmbUserDetailRepository(this);
const {data: user} = await userDetailRepository.requestByUnique(currentUser.unique);

console.log('the groups are', user.userGroupUniques);
w
Are uniques guids and not the name/aliases?!
j
they are guids, yes
take those to the UmbUserGroupItemRepository to translate into names if you wish
Copy code
ts
const { data: userGroups } = await (new UmbUserGroupItemRepository(this)).requestItems(user.userGroupUniques);

console.log('user groups', userGroups.items)
w
Ok thanks.
So a bit of gymnastics but can be done
Shame not just on the current user context
But glad i can get the info 🙂
Do you think it makes sense though it be in the context and save other people doing this as for other people to know what repo to new up etc
j
it's an optimization in the backend since they can avoid iterating the user groups table every time you request a user
that makes the api very fast, since you only query for the data you need
so take the GUIDs of the user groups and query for the user group items or details instead depending on your needs
w
Ok fair enough
Didn’t know with your stores and observables etc it could be optimised that way?
Fetch it once and only change it when it’s needed to
j
yeah, if something else has fetched it already, it wont fetch it again (ideally)
but thats not entirely true right now
2 Views