0

I am using liferay 6.1.1 CE GA2 bundled with tomcat 7.0.27 and hsql.

I have created a hook on ExpandoValue where I want to assign a role to a user when I update his custom fields. Here is what I have tried :

public class UserTagValueListener implements ModelListener<ExpandoValue>{
    @Override
    public void onAfterUpdate(ExpandoValue arg0) throws ModelListenerException {       
        long userId = arg0.getClassPK();
        long roleId = (long)1; //could be any role

        try {
            System.out.println("user roles : " + UserUtil.getRoles(userId));

            /* I tried these 4 lines with the same result */
            //UserLocalServiceUtil.addRoleUsers(roleId, new long[]{userId});
            //RoleLocalServiceUtil.addUserRoles(userId, new long[]{roleId});
            //UserUtil.addRole(userId, roleId);
            RoleUtil.addUser(roleId, userId);
        } catch (PortalException e) {
            e.printStackTrace();
        } catch (SystemException e) {
            e.printStackTrace();
        }
        System.out.println("user roles : " + UserUtil.getRoles(userId));
    }
}

When I check the roles before and after the line where I add a UserRole, it shows that the role has been added to my current user. But once the execution is finished, the user does not have the role anymore.

What am I doing wrong?

Simulant
  • 19,190
  • 8
  • 63
  • 98
user2765347
  • 1
  • 1
  • 2
  • For me `RoleLocalServiceUtil.addUserRoles(userId, roleIds);` works without any problem. Is there some other hook as well which might be updating the Roles? – Prakash K Sep 11 '13 at 05:54
  • No there is no other hook updating the Roles. It looks like the change is not commited to the DB? – user2765347 Sep 11 '13 at 09:22
  • Hope the roleId and userId you are providing are proper i.e. they exist in the database. Try adding something in some other table, within the same code. Also is there any exception? – Prakash K Sep 11 '13 at 10:00
  • There is no exception. I tried to create a role with the method RoleLocalServiceUtil.addRole(...) and it worked, the role I created is in the DB. – user2765347 Sep 11 '13 at 12:34
  • So after that did you try this `RoleLocalServiceUtil.addUserRoles(userId, roleIds);` with the appropriate roleIds and userIds? – Prakash K Sep 11 '13 at 12:53
  • I used `RoleUtil.addUser(roleId, userId);` but it didn't work, the users_roles is not in the DB. – user2765347 Sep 11 '13 at 13:02
  • I would not recommend using `RoleUtil` i.e. classes of the persistence package from outside `*LocalService` implementation. instead try using `RoleLocalServiceUtil.addUserRoles(userId, roleIds);`, it should work. Though I have not tested with `RoleUtil`. – Prakash K Sep 12 '13 at 06:18
  • I just tried it but it gave me the same result. I am going to look into the DB. – user2765347 Sep 12 '13 at 07:51

2 Answers2

1

This worked for me. Type is the value which I am getting from the select box role in create account.jsp page

 String[] usery = (String[])user.getExpandoBridge().getAttribute("Type"); 

 Role role = RoleLocalServiceUtil.getRole(company.getCompanyId(), usery[0]);

 UserLocalServiceUtil.addRoleUser(role.getRoleId(), user.getUserId());

 UserLocalServiceUtil.updateUser(user);
bummi
  • 27,123
  • 14
  • 62
  • 101
Aravinth
  • 94
  • 5
0

this worked for me:

try {

    User newUser = .....
    long roleId = ....      

    Role role = RoleLocalServiceUtil.getRole(roleId);

    System.out.println("Found role : " + role.getRoleId());
    RoleUtil.addUser(role.getRoleId(), newUser.getUserId());
    UserLocalServiceUtil.updateUser(newUser);
    RoleLocalServiceUtil.updateRole(role);

} catch (Exception e) {
    e.printStackTrace();
}
yannicuLar
  • 3,083
  • 3
  • 32
  • 50
  • I tried updating the role and user as you did but that didn't work. The DB table I want to change here is users_role, I dont modify anything on the role or user. – user2765347 Sep 11 '13 at 09:24
  • then for some reason, your database is not updated. maybe you should concentrate on that matter, and the rest of the Role Assign - related code is irrelevant – yannicuLar Sep 11 '13 at 09:46