Fórum

How to add a role to a user programatically

Chris Halverson, modificado 15 Anos atrás.

How to add a role to a user programatically

New Member Postagens: 18 Data de Entrada: 14/11/06 Postagens Recentes
I want to automatically add a roll to somebody if they don't already have it based on an external table (that part isn't important). I'm assuming I can get the role id by:

Role companyAdminRole = RoleLocalServiceUtil.getRole(user.getCompanyId(), "RoleNameHere");


Now I need to assign that to a user if it doesn't exist. How can I query to see if it exists for them and how do I add it to them?

I see a RoleLocalServiceUtil.addRole but I'm not sure if that's the right one.

Any insight would be appreciated. Thanks!
thumbnail
Alex Wallace, modificado 15 Anos atrás.

RE: How to add a role to a user programatically

Liferay Master Postagens: 640 Data de Entrada: 05/11/07 Postagens Recentes
You are in the right track...

Here is how you can get the roles for a user:


 RoleLocalServiceUtil.getUserRoles(userId);


And to add roles, as you've correctly observed, use


RoleLocalServiceUtil.addRole(...);


Thanks!
thumbnail
Tobias Amon, modificado 15 Anos atrás.

RE: How to add a role to a user programatically

Liferay Master Postagens: 546 Data de Entrada: 08/08/07 Postagens Recentes
Hi,

you can have a look at these two methods
        UserLocalServiceUtil.addRoleUsers(roleId, userIds)
        RoleLocalServiceUtil.addUserRoles(userId, roleIds)


The first adds an "array" of users (or just one) to one group. The second adds an "array" of groups (or just one) to one user.

kind regards
Tobias
Chris Halverson, modificado 15 Anos atrás.

RE: How to add a role to a user programatically

New Member Postagens: 18 Data de Entrada: 14/11/06 Postagens Recentes
Awesome, thanks to both of you. Good to confirm that I wasn't missing something.

Thanks again for the quick response.
thumbnail
Alex Wallace, modificado 15 Anos atrás.

RE: How to add a role to a user programatically

Liferay Master Postagens: 640 Data de Entrada: 05/11/07 Postagens Recentes
Indeed, the RoleLocalServiceUtil.addRole is to create new roles in the db, not to assign it to a user... sorry for not clarifying above...
Jay M Kraly, modificado 15 Anos atrás.

RE: How to add a role to a user programatically

New Member Postagens: 12 Data de Entrada: 09/10/08 Postagens Recentes
I have a simple communitylandingpageaction class that is a duplicate of defaultlandingpageaction, in it I'm trying to get the current list of roles for a user.

The problem is that the classes such as RoleLocalServiceUtil are not available at compile time, and I can't figure out where they come from. Are these injected by Spring? Am I forgetting to include a jar in my classpath? Any help appreciated.
thumbnail
Wilson Man, modificado 15 Anos atrás.

RE: How to add a role to a user programatically

Liferay Master Postagens: 581 Data de Entrada: 21/06/06 Postagens Recentes
include the portal-service.jar for compilation