Issue
-
We are trying to use the scripting console to create roles. Either we are not doing this correctly, or something is not working correctly.
When creating the roles we use RoleLocalServiceUtil.addRole to which we pass a ServiceContext object on which companyId of the instance has been set. Roles, however, are being created on the main instance, and not the instance corresponding to the companyId set on serviceContext.
-
Steps to reproduce:
1. Unzip Liferay-2023.Q3.2
2. Startup
3. Create a new instance with (for example) virtual host foo.localhost.com
4. Check what companyId your new instance got - for example 12345
5. Add foo.localhost.com to your hosts file
6. In your main instance, go to control panel -> server administration -> script
7. Execute the attached Groovy script (exchange the Config.companyId to the instance's company id)Actual result:
Roles "Foo1" and "Foo2" are created on the main instance, but not in the instance with companyId 12345Expected result:
Roles "Foo1" and "Foo2" are create on the instance with companyId 12345
Environment
- 7.4
- 2023.q3.2
Resolution
-
the culprit is the userId that you pass to the addRoles() method, and which eventually gets passed to the RoleLocalServiceUtil.addRole() API call.
You obtain the userId like this:
long currentUserId = Long.parseLong(userInfo.get("liferay.user.id"))
and since you are running the script while you are connected to the main instance (with a user from that instance), this will return a userId belonging to the main instance, and you call the role creation method with such a userId. - So there is a certain inconsistency here: you want to create role in a certain instance, with a user ID from another instance, and probably this is what leads to this behaviour.
- We suggest to somehow try to pass a proper userID, one belonging to a user from the instance where you want the role to be created. This has been verified to yield the expected results.