Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
role creation in groovy script for a virtual instance?
Hi,
I have created a groovy script to create a bunch of roles to use in a Docker DXP 7.4, role names are copied from the current production system DXP 7.1.
But I can only run it in the root site server administration script console, not in a virtual instance
- root instance
- virtual instance 1, bunch of roles
- virtual instance 2, bunch of other roles
Simplified (just two dummy role names) script is
import com.liferay.portal.kernel.model.Role
import com.liferay.portal.kernel.model.User
import com.liferay.portal.kernel.model.role.RoleConstants
import com.liferay.portal.kernel.service.ClassNameLocalServiceUtil
import com.liferay.portal.kernel.service.RoleServiceUtil
import com.liferay.portal.kernel.service.UserLocalServiceUtil
static void main(String[] args) {
String tmp = userInfo.get("liferay.user.id")
Long liferayUserId = Long.parseLong(tmp)
User shouldBeAdminUserRunningTheScript = UserLocalServiceUtil.getUser(liferayUserId)
long companyId = Long.valueOf(shouldBeAdminUserRunningTheScript.getCompanyId())
["ROLE_NAME_1",
"ROLE_NAME_2"].each
{
if (roleExists(companyId, it)) {
out.println("role " + it + " already exists for companyId " + companyId)
} else {
try {
out.println("creating regular role " + it + " for companyId " + companyId)
RoleServiceUtil.addRole(
ClassNameLocalServiceUtil.getClassName(Role.class.getName()).getClassName(),
0,
it,
null,
null,
RoleConstants.TYPE_REGULAR,
null,
null
)
} catch (e) {
out.println(e)
}
}
}
}
boolean roleExists(long companyId, String roleName)
{
try {
RoleServiceUtil.getRole(companyId, roleName)
return true
}
catch (e) {
out.println("check role exists: " + e.getMessage())
return false
}
}
Any chance to achieve scripted role creation in a virtual instance?
Or must I (again) create them manually in a virtual instance's roles configuration?
Regards,
gun
Hi Gunnar,
RoleServiceUtil uses CompanyThreadLocal deep inside and reads the
DefaultUser from it, then reads the CompanyId from this DefaultUser to
create roles accordingly.
Now we could overwrite
CompanyThreadLocal, but we would have to make sure to reset the
CompanyId afterwards.
As a simpler alternative, I suggest the use of RoleLocalServiceUtil, this service class also offers the option of specifying the user. So that we can independently read out the AdminUser of a virtual instance and pass it on accordingly.
import com.liferay.portal.kernel.model.Role
import com.liferay.portal.kernel.model.User
import com.liferay.portal.kernel.model.role.RoleConstants
import com.liferay.portal.kernel.service.ClassNameLocalServiceUtil
import com.liferay.portal.kernel.service.RoleLocalServiceUtil
import com.liferay.portal.kernel.service.UserLocalServiceUtil
static void main(String[] args) {
// companyId is the InstanceId of the VirtualInstance u want the role to be added -> Control-Panel -> Virtual-Instances
long companyId = 123212
// we need the admin user of the virtual instance defined above
Role role = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ADMINISTRATOR);
List<User> users = UserLocalServiceUtil.getRoleUsers(role.getRoleId());
User user = users.get(0);
["ROLE_NAME_1",
"ROLE_NAME_2"].each
{
if (roleExists(companyId, it)) {
out.println("role " + it + " already exists for companyId " + companyId)
} else {
try {
out.println("creating regular role " + it + " for companyId " + companyId)
RoleLocalServiceUtil.addRole(
user.getUserId(),
ClassNameLocalServiceUtil.getClassName(Role.class.getName()).getClassName(),
0,
it,
null,
null,
RoleConstants.TYPE_REGULAR,
null,
null
)
} catch (e) {
out.println(e)
}
}
}
}
boolean roleExists(long companyId, String roleName) {
try {
RoleLocalServiceUtil.getRole(companyId, roleName)
return true
}
catch (e) {
out.println("check role exists: " + e.getMessage())
return false
}
}
I hope I have been able to help you with this.
Regards,
Dominic
Hi Dominic,
thank you very much, it worked like a charm.
Note: I explicitly used a specific admin user by using
UserLocalServiceUtil.getUserByScreenName(virtualInstanceId, "sn.of.the.admin.user")
Regards, have a nice day :-)
Gunnar
Powered by Liferay™