Foros de discusión

How to check if a user has a role within current community/site (Liferay 6.

Morad Ahmad, modificado hace 12 años.

How to check if a user has a role within current community/site (Liferay 6.

Junior Member Mensajes: 71 Fecha de incorporación: 16/06/10 Mensajes recientes
I defined a community role "SiteContentManager" which allows users to add specific content to a site, where they have this role. I need to check if the user has this role in a portlet. I tried different ways without success:

1. List<Role> userGroupRoles = RoleServiceUtil.getUserGroupRoles(user.getUserId(), communityId);
if (userGroupRoles != null) {
for (Role role : userGroupRoles) {
System.out.println(role.getName());
if (role.getName().equals(SITE_ADMIN_ROLE))
return true;
}
}

2. groupRoles = RoleLocalServiceUtil.getGroupRoles(communityId);
System.out.println("group roles: ");
if (groupRoles != null) {...}

3. RoleServiceUtil.hasUserRole(user.getUserId(), communityId, SITE_ADMIN_ROLE, true);

???

Thanks.
thumbnail
Jitendra Rajput, modificado hace 12 años.

RE: How to check if a user has a role within current community/site (Lifera

Liferay Master Mensajes: 875 Fecha de incorporación: 7/01/11 Mensajes recientes
Have you tried by passing false in second approach .?

RoleServiceUtil.hasUserRole(user.getUserId(), communityId, SITE_ADMIN_ROLE, false);


Similar kind of problem encountered in below thread ..look into it ..May help you.

http://www.liferay.com/community/forums/-/message_boards/message/4002510
Ricardo Piccoli, modificado hace 11 años.

RE: How to check if a user has a role within current community/site (Lifera

New Member Mensajes: 6 Fecha de incorporación: 14/02/13 Mensajes recientes
Any success?
Ricardo Piccoli, modificado hace 11 años.

RE: How to check if a user has a role within current community/site (Lifera

New Member Mensajes: 6 Fecha de incorporación: 14/02/13 Mensajes recientes
I was able to do that using:

UserGroupRoleLocalServiceUtil.hasUserGroupRole(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), "my-custom-role", true);

or

UserGroupRoleLocalServiceUtil.hasUserGroupRole(themeDisplay.getUserId(), themeDisplay.getScopeGroupId(), RoleConstants.SITE_ADMINISTRATOR, true);

This last "true", is because I don't add users but groups to sites.
Lee Jordan, modificado hace 4 años.

RE: How to check if a user has a role within current community/site (Lifera

Expert Mensajes: 449 Fecha de incorporación: 26/05/15 Mensajes recientes
Trying to detect user site roles also here in freemarker portal_normal theme file, which is an absolute nightmare. These things need to be much easier.

Can't find service in 7.2


freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==&gt; serviceLocator.findService("com.liferay.portal.service.UserGroupRoleLocalService")
thumbnail
Christoph Rabel, modificado hace 4 años.

RE: How to check if a user has a role within current community/site (Lifera

Liferay Legend Mensajes: 1554 Fecha de incorporación: 24/09/09 Mensajes recientes
Lee Jordan, modificado hace 4 años.

RE: How to check if a user has a role within current community/site (Lifera

Expert Mensajes: 449 Fecha de incorporación: 26/05/15 Mensajes recientes
Sigh, that's what I'm using in 7.2 and the service is now found. That documentation isn't helpful though. Java Docs to a front end developer is total nonsense. FED life in Liferay is abysmal.

I have this is in init_custom.ftl

user_id and group_id are already defined from the init.ftl

<#assign UserLocalRoleService = serviceLocator.findService("com.liferay.portal.kernel.service.UserGroupRoleLocalService") />
<#assign user_site_roles = UserLocalRoleService.getUserGroupRoles(user_id, group_id) />

and this in portal normal

console.log('${user_site_roles}');


I'm stuck trying to even see what's in there as it's a sequence and won't echo out to the console, the error now is ...Caused by: freemarker.core.NonStringException: For "${...}" content: Expected a string or something automatically convertible to string (number, date or boolean), but this has evaluated to a sequence (Collections$UnmodifiableRandomAccessList wrapped into f.t.DefaultListAdapter):_==> user_site_roles  [in template "mercury-theme_SERVLET_CONTEXT_/templates/portal_normal.ftl" at line 91, column 20]__----_FTL stack trace ("~" means nesting-related):_   - Failed at: ${user_site_roles}  [in template "mercury-theme_SERVLET_CONTEXT_/templates/portal_normal.ftl" at line 91, column 18]_---- [Sanitized]


All I need right now is something like "is_site_admin" "is_site_member" "is_site_guest", that's really what I'm trying to build in the theme.
Lee Jordan, modificado hace 4 años.

RE: How to check if a user has a role within current community/site (Lifera

Expert Mensajes: 449 Fecha de incorporación: 26/05/15 Mensajes recientes
One use case is to display an information box at the bottom of a web content template that's only visible to site admins. Giving information on how to use the template, links to resources like where to find images for the template etc etc. We also get hammered with people not knowing if they are a site member or not, so adding a simple icon to the dockbar would help them quit bugging us and help us figure out faster if that user is a site member or not.

// WRITE USER ROLE TO CONSOLE
    <#if user_site_roles?is_sequence>
      console.log('is sequence');
      <#list user_site_roles as role>
        <#assign role_name = role.getName()>
        ${role_name}
      </#list>
    </#if>

Caused by: freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:_==> role.getName
thumbnail
David H Nebinger, modificado hace 4 años.

RE: How to check if a user has a role within current community/site (Lifera

Liferay Legend Mensajes: 14915 Fecha de incorporación: 2/09/06 Mensajes recientes
So this is why Liferay recommends testing for permissions, not roles.

Roles themselves are just containers of permissions, and your actual complete set of permissions can come from many source including but not limited to direct role assignment.

Your test will fail the first time someone decides they don't want to manage 50k site member role assignments (or some other role) and instead want to create a group that has the role(s) and all users become member of the group.


From Liferay's perspective, everything works exactly the same, but your test is now going to fail. Why? Because Liferay does permission checks, not role checks, to determine if a user has access to something.
Lee Jordan, modificado hace 4 años.

RE: How to check if a user has a role within current community/site (Lifera

Expert Mensajes: 449 Fecha de incorporación: 26/05/15 Mensajes recientes
So how would we detect if the logged in user was a member vs an admin. I have another use case for this to direct IE usage discouragement to admins using IE. And also would like to report monthly on our page views by admins only.
thumbnail
David H Nebinger, modificado hace 4 años.

RE: How to check if a user has a role within current community/site (Lifera

Liferay Legend Mensajes: 14915 Fecha de incorporación: 2/09/06 Mensajes recientes
Ah, so you can do this pretty easily emoticon

For example, only a site admin can enable staging. So how do you figure out if a user has permission to enable staging? You look at the GroupServiceImpl to see how the portal verifies that the user can enable staging.


In 7.2 for example, the permission check is:

GroupPermissionUtil.check(
   getPermissionChecker(), groupLocalService.getGroup(groupId),
   ActionKeys.UPDATE);

So if this is good enough for the portal to determine that the user can modify the group to enable staging, I think it is safe to say that these same users would be admins (either real admins or site admins).

Liferay doesn't track page views by users, that would be something you'd have to build out yourself. The same permission check can be used to see if you're a site admin though.
Lee Jordan, modificado hace 4 años.

RE: How to check if a user has a role within current community/site (Lifera

Expert Mensajes: 449 Fecha de incorporación: 26/05/15 Mensajes recientes
Is that freemarker code?

That's really the end game for figuring out what the user is on a site by site basis. I will need to track how many site admins are still using IE for example to provide education to change behaviour. I can't currently see how Liferay analytics tracks that (it should). But as for figuring out how to do it ... It's a desert out there. There's a few threads with snippets but no such documentation on how to use services in freemarker.

I'm at this stage to be honest ...
"output a sequence in freemarker" just so I can see what's in there to condition on. The chances of success for anyone particularly someone new to Liferay is close to zero.

I also only get two pages of results in Google and one result is this thread. The thing is if I can get something to work it opens the floodgates for other stuff, there's just very little chance of being able to scrape enough information together for a freemarker developer here (not java)??

I have the ?size of the array/sequence whatever data Liferay is giving. The size is zero.
thumbnail
David H Nebinger, modificado hace 4 años.

RE: How to check if a user has a role within current community/site (Lifera

Liferay Legend Mensajes: 14915 Fecha de incorporación: 2/09/06 Mensajes recientes
It is pure java in the form I gave, but you can easily wrap that into a context contributor for direct freemarker access.

I try to avoid doing things in FM scripts because it is all interpreted. Instead I prefer to build all of my logic in Java and then use a context contributor to inject it into the FM context. Then my FM scripts can use simple ${} syntax to inject the value, but the heavy lifting is still done in java.