WoVoo
WoVoo

Reputation: 99

How to copy an existing organisation role in Liferay and add it to Users that already had the original role?

So i am working with Liferay 7/Oracle 11g and i try to copy existing organization roles of a specified subtype, rename them and then add those new roles to users that had the original Roles.

So getting roles by subtype works pretty straightforward,

the first Problem arises after i add the new role, i receive an error message when trying to view the new Role:

Someone may be trying to circumvent the permission checker: {companyId=20115, name=com.liferay.portal.kernel.model.Role, primKey=31701, scope=4}

the second Problem is getting the Users that have the original Organization Role as i can't find an existing Liferay class that delivers me that Information. From what i know its saved withing the USERGROUPROLE table, so i could read it from there with my own SQL select, but i would prefer if there was a Liferay class that provided that information.

List<Role> lRole = RoleLocalServiceUtil.getSubtypeRoles(String.valueOf(adoptFromYear));
for(Role role : lRole) {
    long roleId = CounterLocalServiceUtil.increment();
    Role newRole = RoleLocalServiceUtil.createRole(roleId);
    newRole.setClassPK(roleId);
    newRole.setCompanyId(role.getCompanyId());
    newRole.setUserId(role.getUserId());
    newRole.setUserName(role.getUserName());
    newRole.setClassName(role.getClassName());
    newRole.setTitle(role.getTitle());
    newRole.setDescription(role.getDescription());
    newRole.setCreateDate(new Date());
    newRole.setType(role.getType());
    newRole.setName(replaceOrAppendYear(role.getName(), newYear));
    newRole.setSubtype(String.valueOf(newYear));

    RoleLocalServiceUtil.addRole(newRole);

    //assign Users that have base Role, the new Role
    long[] aUserId = UserLocalServiceUtil.getRoleUserIds(role.getRoleId());

    for(long userId : aUserId) {
        RoleLocalServiceUtil.addUserRole(userId, newRole.getRoleId());
    }
}

UPDATE:

I fixed the first problem by using another addRole method of UserLocalServiceUtil, code is now as following:

List<Role> lRole = RoleLocalServiceUtil.getSubtypeRoles(String.valueOf(adoptFromYear));
for(Role role : lRole) {
    Role newRole = RoleLocalServiceUtil.addRole(role.getUserId(), Role.class.getName(), 
        CounterLocalServiceUtil.increment(), replaceOrAppendYear(role.getName(), newYear), 
        role.getTitleMap(), role.getDescriptionMap(), role.getType(), 
        String.valueOf(newYear), null);

    //add User from base Role to new Role
    long[] aUserId = UserLocalServiceUtil.getRoleUserIds(role.getRoleId());
    //aUserId still empty

    for(long userId : aUserId) {
        RoleLocalServiceUtil.addUserRole(userId, newRole.getRoleId());
    }
}

So the Problem of getting all User that have a certain Organization Role still exists.

Upvotes: 0

Views: 213

Answers (1)

WoVoo
WoVoo

Reputation: 99

So my solution to my problems is as following, i used the other addRole method to add a new Role and that also creates the additional entry in the db so there is no error message anymore.

On my second problem, i just use the liferay API to get all entries from UserGroupRole Table and then i filter all the entries via the roleId that i need. Not a nice solution, but in my case even with tens of thousands of entries (and thats generous for what i work on) the function will be called once a year so ¯\_(ツ)_/¯

So here is the solution:

List<Role> lRole = RoleLocalServiceUtil.getSubtypeRoles(String.valueOf(adoptFromYear));
for(Role role : lRole) {
    Role newRole = RoleLocalServiceUtil.addRole(role.getUserId(), Role.class.getName(), 
        CounterLocalServiceUtil.increment(), replaceOrAppendYear(role.getName(), newYear), 
        role.getTitleMap(), role.getDescriptionMap(), role.getType(), String.valueOf(newYear), null);

    //get all User - Group (Organization) - Role Objects and filter by Role
    List<UserGroupRole> lUserGroupRole = UserGroupRoleLocalServiceUtil
        .getUserGroupRoles(QueryUtil.ALL_POS, QueryUtil.ALL_POS).stream()
        .filter(ugr -> ugr.getRoleId() == role.getRoleId()).collect(Collectors.toList());

    //add new Role to all existing Users that had the adopted role
    long[] roleIds = {newRole.getRoleId()};
    for(UserGroupRole ugr : lUserGroupRole) {
        UserGroupRoleLocalServiceUtil.addUserGroupRoles(ugr.getUserId(), ugr.getGroupId(), roleIds);
    }
}

Upvotes: 0

Related Questions