Fórum

Creating Roles Programmatically for each User Group

thumbnail
Clay Banks, modificado 9 Anos atrás.

Creating Roles Programmatically for each User Group

Regular Member Postagens: 141 Data de Entrada: 11/12/13 Postagens Recentes
I want to add a Role for each User Group within my portal so that I can grant specific access to many users at once, depending on what groups they're belong to.

Using Liferay API, is there any way to programmatically add a Role for each User Group that exists within the Portal? This would be more efficient than adding each role individually through the UI.

Something like

    for(i=0;i<usergroups.size();i++){ roles.add(usergroups[i].getname()); } < code></usergroups.size();i++){>
<br><br><br>If possible, I'd like to then assign the same user group to that Role within the same method, otherwise the role would know nothing of the associated user group<br><pre><code>roles.assignUserGroup(userGroups[i]);</code></pre><br><br>Anyone accomplish a task similar to this?<br><br>UPDATE: Question answered on Stack Overflow here: http://stackoverflow.com/questions/27336702/creating-roles-programmatically-for-each-user-group/27375790#27375790
thumbnail
Suresh Nimmakayala, modificado 9 Anos atrás.

RE: Creating Roles Programmatically for each User Group

Liferay Master Postagens: 690 Data de Entrada: 18/08/04 Postagens Recentes
simple answer you can do programmatically , check RoleLocalServicUtil, UserGroupLocalServiceutil source code

not similar but creating,assigning users to sites ,usergroups programmatically kinda done
thumbnail
Tanweer Ahmed ., modificado 1 Ano atrás.

RE: Creating Roles Programmatically for each User Group (Resposta)

Expert Postagens: 322 Data de Entrada: 11/03/10 Postagens Recentes
Hi Clay,

You can have a look at the below method.

UserGroupGroupRoleLocalServiceUtil.addUserGroupGroupRoles(userGroupIds, groupId, roleId)


You have to pass the params as array of userGroupIds, groupId and the roleId which will be the id of the role which you want to associate.

Hope this helps.

Regards,
Tanweer.
thumbnail
Clay Banks, modificado 9 Anos atrás.

RE: Creating Roles Programmatically for each User Group

Regular Member Postagens: 141 Data de Entrada: 11/12/13 Postagens Recentes
Thanks Tanweer, just what I needed
thumbnail
Clay Banks, modificado 9 Anos atrás.

RE: Creating Roles Programmatically for each User Group

Regular Member Postagens: 141 Data de Entrada: 11/12/13 Postagens Recentes
A user from stackoverflow provided a useful solution as well

for (UserGroup userGroup : userGroups) {
    String userGroupName = userGroup.getName();
    // for locale specific title (optional, can be null)
    Map<locale, string> titleMap = new HashMap<locale, string>();
    titleMap.put(Locale.ENGLISH, userGroupName);

    // for locale specific description (optional, can be null)
    Map<locale, string> descriptionMap = new HashMap<locale, string>();
    titleMap.put(Locale.ENGLISH, "Role created for UserGroup - " + userGroupName);

    int type = RoleConstants.TYPE_REGULAR;

    // adding the role
    Role role = RoleLocalServiceUtil.addRole(userId, Role.class.getName(), 0, userGroupName, titleMap, descriptionMap, type, null, null);

    // assigning the UserGroup to the role
    GroupLocalServiceUtil.addRoleGroups(role.getRoleId(), new long[]{userGroup.getGroupId()}); // need to pass groupId and not userGroupId
}</locale,></locale,></locale,></locale,>


Original post: http://stackoverflow.com/questions/27336702/creating-roles-programmatically-for-each-user-group/27375790#27375790
thumbnail
Prakash Khanchandani, modificado 9 Anos atrás.

RE: Creating Roles Programmatically for each User Group

Expert Postagens: 329 Data de Entrada: 10/02/11 Postagens Recentes
Thanks for posting the stackoverflow link in the answer. Appreciated that you gave the due credit.

It would have been better to include the link in the question itself in both the places when you cross post the same thing on sites.

Helps the community know if it is already answered somewhere and would save much time.

Thanks,
Prakash
thumbnail
Clay Banks, modificado 9 Anos atrás.

RE: Creating Roles Programmatically for each User Group

Regular Member Postagens: 141 Data de Entrada: 11/12/13 Postagens Recentes
Thanks Prakash, took your advice and posted the SO link in the original question