Forums de discussion

How to add a role to a user programatically

Chris Halverson, modifié il y a 15 années.

How to add a role to a user programatically

New Member Publications: 18 Date d'inscription: 14/11/06 Publications récentes
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, modifié il y a 15 années.

RE: How to add a role to a user programatically

Liferay Master Publications: 640 Date d'inscription: 05/11/07 Publications récentes
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, modifié il y a 15 années.

RE: How to add a role to a user programatically

Liferay Master Publications: 546 Date d'inscription: 08/08/07 Publications récentes
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, modifié il y a 15 années.

RE: How to add a role to a user programatically

New Member Publications: 18 Date d'inscription: 14/11/06 Publications récentes
Awesome, thanks to both of you. Good to confirm that I wasn't missing something.

Thanks again for the quick response.
thumbnail
Alex Wallace, modifié il y a 15 années.

RE: How to add a role to a user programatically

Liferay Master Publications: 640 Date d'inscription: 05/11/07 Publications récentes
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, modifié il y a 15 années.

RE: How to add a role to a user programatically

New Member Publications: 12 Date d'inscription: 09/10/08 Publications récentes
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, modifié il y a 15 années.

RE: How to add a role to a user programatically

Liferay Master Publications: 581 Date d'inscription: 21/06/06 Publications récentes
include the portal-service.jar for compilation