Foros de discusión

How to know the logged in user is admin or not?

chandra sekhar, modificado hace 13 años.

How to know the logged in user is admin or not?

Regular Member Mensajes: 149 Fecha de incorporación: 7/07/09 Mensajes recientes
Hi All,

How can know the logged in user is admin or not ? I have a requirement where I have to check whether the logged in user is admin or not? If he is admin I have to execute some logic and if he is not admin I have to execute some other logic. how to check in liferay logged in user role ?

Thanks in advance.

Regards,
Chandrasekhar
Dariusz Sawicki, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Regular Member Mensajes: 165 Fecha de incorporación: 27/03/09 Mensajes recientes
hello
You must retrive user which is already looged and for exapmle


 List<role> userRoles = user.getRoles();
            for(Role r : userRoles){
                if("Administrator".equalsIgnoreCase(r.getName())){
                    admistraton
                }else{
                    !administrator
                }
</role>


It's one from multiple option.

Regards
Darek
thumbnail
jelmer kuperus, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 1191 Fecha de incorporación: 10/03/10 Mensajes recientes
permissionChecker.isOmniadmin()
thumbnail
Hitoshi Ozawa, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/03/10 Mensajes recientes
Agree with jelmer. I think .getName will get the name instead of a role. The difference is a name can be change by an admin. For example, I'm translating the name to Japanese for the Japanese version of Liferay.
chandra sekhar, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Regular Member Mensajes: 149 Fecha de incorporación: 7/07/09 Mensajes recientes
Hi All,

Thanks to all for your responses.

Shall I derectly use the following code plugins portlet jsp page or I need to import any packages?

if(permissionChecker.isOmniadmin())
	   out.println("Admin");
      else 
	   out.println("Not Admin");



Thanks in adavance.

Regards,
Chandrasekhar
thumbnail
jelmer kuperus, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 1191 Fecha de incorporación: 10/03/10 Mensajes recientes
It's exposed if you use the <liferay-theme:defineObjects /> tag
thumbnail
Thiago Leão Moreira, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 1449 Fecha de incorporación: 10/10/07 Mensajes recientes
You can also use

request.isUserInRole(String)


Remember to use the Role's name not the title.
chandra sekhar, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Regular Member Mensajes: 149 Fecha de incorporación: 7/07/09 Mensajes recientes
Hi,

Thanks for your response.

I tested that one and Its working fine for my requirement. But I have one more doubt. If I use the code request.isUserInRole("Administrator") like this, here I am hard-coding the role name as a Administrator. But if I change role name "Administrator" to "admin" then I have to change in code also for validating. Is there any other way for the same scenario, so that I need not change any coding part if I made any changes in the spelling of role name also.

Thanks in advance.

Regards,
Chandrasekhar
thumbnail
Raja Nagendra Kumar, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Expert Mensajes: 484 Fecha de incorporación: 2/03/06 Mensajes recientes
Use additonal properties file which can map..

in the code you could always use

"admin"

and let the properties file contain

admin=Administrator etc..

then your code can be read the value for the key admin and compare.

Not sure if Liferay has standard constants or API to get all the admin rights accounts etc..

Regards,
Raja Nagendra Kumar,
C.T.O
www.tejasoft.com
thumbnail
Thiago Leão Moreira, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 1449 Fecha de incorporación: 10/10/07 Mensajes recientes
Hi Chandrasekhar,

You cannot change the name of a role only its title.
thumbnail
Hitoshi Ozawa, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 7942 Fecha de incorporación: 24/03/10 Mensajes recientes
You cannot change the name of a role only its title.


You are right! My mistake. I was changing from the source code but later realized that this wasn't the right approach. emoticon
thumbnail
Olaf Kock, modificado hace 13 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 6403 Fecha de incorporación: 23/09/08 Mensajes recientes
chandra sekhar:
If I use the code request.isUserInRole("Administrator") like this, here I am hard-coding the role name as a Administrator. But if I change role name "Administrator" to "admin" then I have to change in code also for validating. Is there any other way for the same scenario, so that I need not change any coding part if I made any changes in the spelling of role name also.


Yes, definitely, and it'll make your code much more maintainable: You probably don't want to know if somebody is "Administrator", but if they have the permission to do something. permissionChecker has many other methods where you can check if a user has permission to do a certain operation in the portal - no matter what role name this permission comes through.

Naturally this is a lot more granular, which in turn might result in a lot more work as you have to determine what permission you're actually looking for. And the interface takes a few more parameters that you have to determine. As you don't give a hint what you're actually wrapping in this conditional, I don't know if this is feasible for you, but I'd recommend to go this route if it's remotely possible.
Mark Smith, modificado hace 10 años.

RE: How to know the logged in user is admin or not?

Junior Member Mensajes: 54 Fecha de incorporación: 18/01/14 Mensajes recientes
In liferay version 6.2 you should use:

	
PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();
permissionChecker.isOmniadmin();


regards
Hanna Åstrand, modificado hace 9 años.

RE: How to know the logged in user is admin or not?

New Member Mensaje: 1 Fecha de incorporación: 18/04/11 Mensajes recientes
Mark Smith:
In liferay version 6.2 you should use:

	
PermissionChecker permissionChecker = PermissionThreadLocal.getPermissionChecker();
permissionChecker.isOmniadmin();




Thanks! You saved my day emoticon
Traolly Xiong, modificado hace 9 años.

RE: How to know the logged in user is admin or not?

Regular Member Mensajes: 195 Fecha de incorporación: 30/12/11 Mensajes recientes
Permission Checker seems to only check for "Regular Roles". Any ideas on how to check for "Site Roles"?

Thanks.
thumbnail
Olaf Kock, modificado hace 9 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 6403 Fecha de incorporación: 23/09/08 Mensajes recientes
Traolly Xiong:
Permission Checker seems to only check for "Regular Roles". Any ideas on how to check for "Site Roles"?


PermissionChecker actually checks for permissions (plus a few extras). If you actually want role associations, especially site roles, you'll have to go through the regular API and potentially check indirections - e.g. if a user directly has a specific role, or any of their usergroups, organizations etc.

Thus my recommendation above: You want to check for permissions, not for role memberships. Don't hardcode the roles into your applications, rather offer permissions that can be configured at runtime. Then you even can use the very convenient PermissionChecker interface, inline permissions etc..
thumbnail
David H Nebinger, modificado hace 9 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 14916 Fecha de incorporación: 2/09/06 Mensajes recientes
+1 to Olaf.

You can actually give all permissions to the guest role, basically turning them into an administrator but without the admin role.

Permissions are the way to go, doesn't matter if you are an admin or a user with a permission, the permission checker will show that you have the perm vs someone who doesn't.
Traolly Xiong, modificado hace 9 años.

RE: How to know the logged in user is admin or not?

Regular Member Mensajes: 195 Fecha de incorporación: 30/12/11 Mensajes recientes
Yes I thought of that when I was looking at one of the custom site roles I created, but was hesitant to go that path because all I really wanted to do was just show a html button (use js to perform a certain function) to an end user if that end user is a site admin or had a specific site role. When adding permissions to a site role, it's broken down into 5 categories in 6.1 LR (i.e. Page, Page Variation, Site, Site Pages Variation, and Team), and I'm not sure which permissions would make sense to define. I was thinking it was a bit much for the goal, but I could be wrong.

Olaf / David, with that said should I still stick with the permission check or go with the role association check?

If I do go with the permissionchecker, what's the best way to get the "groupID", "primKey", and "actionID"?

hhm... I'm thinking if I decide to check if a user is part of a usergroup, I'm assuming the site role associated to user group can would help me use the
"isGroupMember()" method to do so.

Let me know our thoughts as I attempt to try out your suggestions.

Much thanks.

Regards,
Traolly Xiong
thumbnail
David H Nebinger, modificado hace 9 años.

RE: How to know the logged in user is admin or not?

Liferay Legend Mensajes: 14916 Fecha de incorporación: 2/09/06 Mensajes recientes
Group id is going to be the scopeGroupId from the current ThemeDisplay instance. primKey is your primary key value; usually this will be the entity that you are checking a perm on, i.e. a journal article id or something. Action id is the action string that you're tied to, would be the action you want to perform such as UPDATE or something.

Usually when you're looking at permissions, you have to identify the resource (what you are wanting to act upon) and the action to take (what you want to do to the resource). Sometimes you can leverage existing Liferay perms, but you could always create your own and test for it in your page.
Traolly Xiong, modificado hace 9 años.

RE: How to know the logged in user is admin or not?

Regular Member Mensajes: 195 Fecha de incorporación: 30/12/11 Mensajes recientes
Very insightful. Thanks!