<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>role creation in groovy script for a virtual instance?</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=122270164" />
  <subtitle>role creation in groovy script for a virtual instance?</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=122270164</id>
  <updated>2026-04-04T03:12:36Z</updated>
  <dc:date>2026-04-04T03:12:36Z</dc:date>
  <entry>
    <title>RE: RE: role creation in groovy script for a virtual instance?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122270467" />
    <author>
      <name>Gunnar Brinkmann</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122270467</id>
    <updated>2023-12-06T13:21:54Z</updated>
    <published>2023-12-06T13:21:53Z</published>
    <summary type="html">&lt;p&gt;Hi Dominic,&lt;/p&gt;
&lt;p&gt;thank you very much, it worked like a charm.&lt;/p&gt;
&lt;p&gt;Note: I explicitly used a specific admin user by using&lt;/p&gt;
&lt;pre&gt;
&lt;code class="language-java"&gt;UserLocalServiceUtil.getUserByScreenName(virtualInstanceId, &amp;quot;sn.of.the.admin.user&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Regards, have a nice day :-)&lt;br&gt; ​​​​​​​Gunnar&lt;/p&gt;</summary>
    <dc:creator>Gunnar Brinkmann</dc:creator>
    <dc:date>2023-12-06T13:21:53Z</dc:date>
  </entry>
  <entry>
    <title>RE: role creation in groovy script for a virtual instance?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122270211" />
    <author>
      <name>Dominic Blank - USU</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122270211</id>
    <updated>2024-06-05T14:40:26Z</updated>
    <published>2023-12-06T12:01:22Z</published>
    <summary type="html">&lt;p&gt;Hi Gunnar,&lt;/p&gt;
&lt;p&gt;RoleServiceUtil uses CompanyThreadLocal deep inside and reads the
  DefaultUser from it, then reads the CompanyId from this DefaultUser to
  create roles accordingly.&lt;br&gt; Now we could overwrite
  CompanyThreadLocal, but we would have to make sure to reset the
  CompanyId afterwards.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;pre&gt;
&lt;code class="language-java"&gt;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 -&amp;gt; Control-Panel -&amp;gt; Virtual-Instances
    long companyId = 123212

    // we need the admin user of the virtual instance defined above
    Role role = RoleLocalServiceUtil.getRole(companyId, RoleConstants.ADMINISTRATOR);
    List&amp;lt;User&amp;gt; users = UserLocalServiceUtil.getRoleUsers(role.getRoleId());
    User user = users.get(0);


    [&amp;quot;ROLE_NAME_1&amp;quot;,
     &amp;quot;ROLE_NAME_2&amp;quot;].each
            {
                if (roleExists(companyId, it)) {
                    out.println(&amp;quot;role &amp;quot; + it + &amp;quot; already exists for companyId &amp;quot; + companyId)
                } else {
                    try {
                        out.println(&amp;quot;creating regular role &amp;quot; + it + &amp;quot; for companyId &amp;quot; + 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(&amp;quot;check role exists: &amp;quot; + e.getMessage())
        return false
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I hope I have been able to help you with this.&lt;/p&gt;
&lt;p&gt;Regards,&lt;br&gt; Dominic&lt;/p&gt;</summary>
    <dc:creator>Dominic Blank - USU</dc:creator>
    <dc:date>2023-12-06T12:01:22Z</dc:date>
  </entry>
  <entry>
    <title>role creation in groovy script for a virtual instance?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122270163" />
    <author>
      <name>Gunnar Brinkmann</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=122270163</id>
    <updated>2023-12-06T09:45:29Z</updated>
    <published>2023-12-06T09:45:28Z</published>
    <summary type="html">&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;But I can only run it in the root site server administration script
  console, not in a virtual instance&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;root instance  &lt;ul&gt;
      &lt;li&gt;virtual instance 1, bunch of roles&lt;/li&gt;
      &lt;li&gt;virtual instance 2, bunch of other roles&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Simplified (just two dummy role names) script is&lt;/p&gt;
&lt;pre&gt;
&lt;code class="language-java"&gt;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(&amp;quot;liferay.user.id&amp;quot;)
    Long liferayUserId = Long.parseLong(tmp)

    User shouldBeAdminUserRunningTheScript = UserLocalServiceUtil.getUser(liferayUserId)
    long companyId = Long.valueOf(shouldBeAdminUserRunningTheScript.getCompanyId())

    [&amp;quot;ROLE_NAME_1&amp;quot;,
     &amp;quot;ROLE_NAME_2&amp;quot;].each
            {
                if (roleExists(companyId, it)) {
                    out.println(&amp;quot;role &amp;quot; + it + &amp;quot; already exists for companyId &amp;quot; + companyId)
                } else {
                    try {
                        out.println(&amp;quot;creating regular role &amp;quot; + it + &amp;quot; for companyId &amp;quot; + 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(&amp;quot;check role exists: &amp;quot; + e.getMessage())
        return false
    }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Any chance to achieve scripted role creation in a virtual instance?&lt;/p&gt;
&lt;p&gt;Or must I (again) create them manually in a virtual instance's roles configuration?&lt;/p&gt;
&lt;p&gt;Regards,&lt;br&gt; ​​​​​​​gun&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;</summary>
    <dc:creator>Gunnar Brinkmann</dc:creator>
    <dc:date>2023-12-06T09:45:28Z</dc:date>
  </entry>
</feed>
