<?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>Is is possible to use permissionChecker in a background service?</title>
  <link rel="self" href="https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=112148624" />
  <subtitle>Is is possible to use permissionChecker in a background service?</subtitle>
  <id>https://liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=112148624</id>
  <updated>2026-04-05T04:19:48Z</updated>
  <dc:date>2026-04-05T04:19:48Z</dc:date>
  <entry>
    <title>RE: Is is possible to use permissionChecker in a background service?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112252432" />
    <author>
      <name>旻 吴</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112252432</id>
    <updated>2025-04-15T00:43:36Z</updated>
    <published>2019-02-06T16:00:07Z</published>
    <summary type="html">It works! Thank you so much!</summary>
    <dc:creator>旻 吴</dc:creator>
    <dc:date>2019-02-06T16:00:07Z</dc:date>
  </entry>
  <entry>
    <title>RE: Is is possible to use permissionChecker in a background service?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112189130" />
    <author>
      <name>Ketan Savaliya</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112189130</id>
    <updated>2019-01-31T08:54:02Z</updated>
    <published>2019-01-31T08:54:02Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;blockquote&gt;吴 旻Thank you for your response. We change use roles in panel UI and tried clear server cache in control panel-server admin but it didn't work.&lt;br&gt;&lt;br&gt;We tracked into permissionChecker's source code and found out that the reason is &lt;br&gt;&lt;pre&gt;&lt;code&gt;UserGroupRoleLocalService.hasUserGroupRole(userId, groupId, SITE_ADMIN, true)&lt;/code&gt;&lt;/pre&gt;does not work. It can not return true even if the userId-groupId-roleId record exists in UserGroupRole table, due to cache issue. But unfortunatelly we haven't found the method to flush that cache yet...&lt;/blockquote&gt;&lt;br&gt;&lt;br&gt;Hi,&lt;br&gt;​​​​​​​&lt;br&gt;&lt;br&gt;I think you can use below code line to clear cache for any entiry by giving entity impl full package path. in your case&amp;nbsp;UserGroupRoleImpl.&amp;nbsp;&lt;br&gt;&lt;strong&gt;&lt;br&gt;EntityCacheUtil.clearCache("com.liferay.portal.db.model.impl.Payment_TransactionImpl");&lt;/strong&gt;&lt;br&gt;&lt;br&gt;Where&amp;nbsp;&lt;strong&gt;Payment_Transaction &lt;/strong&gt;is your service builder entity.&amp;nbsp;&lt;strong&gt;Payment_TransactionImpl &lt;/strong&gt;is generated by service builder.&amp;nbsp;&lt;br&gt;&lt;br&gt;&lt;br&gt;HTH!!&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Ketan Savaliya</dc:creator>
    <dc:date>2019-01-31T08:54:02Z</dc:date>
  </entry>
  <entry>
    <title>RE: Is is possible to use permissionChecker in a background service?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112186483" />
    <author>
      <name>旻 吴</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112186483</id>
    <updated>2025-04-15T00:43:36Z</updated>
    <published>2019-01-31T05:25:46Z</published>
    <summary type="html">Thank you so much for the hints. Because we are testing on a local pc without any user login, I&amp;#39;m afraid neither is the cause. &lt;br /&gt;&lt;br /&gt;1. Did anyone write to the database manually, without using the API? &lt;br /&gt;&lt;br /&gt;  NO&lt;br /&gt;&lt;br /&gt;2. Are you running a cluster? &lt;br /&gt;  NO&lt;br /&gt;&lt;br /&gt;As the source code above, we are testing permission checker via gogo-shell with some commands registerd by an osgi service. Would that cause the problem? Such as different database connection with that used by liferay-ui-called-service ?</summary>
    <dc:creator>旻 吴</dc:creator>
    <dc:date>2019-01-31T05:25:46Z</dc:date>
  </entry>
  <entry>
    <title>RE: Is is possible to use permissionChecker in a background service?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112177911" />
    <author>
      <name>Olaf Kock</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112177911</id>
    <updated>2019-01-30T11:20:54Z</updated>
    <published>2019-01-30T11:20:54Z</published>
    <summary type="html">&lt;blockquote&gt;吴 旻...It can not return true even if the userId-groupId-roleId record exists in UserGroupRole table, due to cache issue. But unfortunatelly we haven&amp;#39;t found the method to flush that cache yet...&lt;/blockquote&gt;Flushing the cache would only be good as a quick check to get to the root of the problem. It should be enough to use the controls in the Control Panel&amp;#39;s &amp;#34;System Administration&amp;#34; section and then try again.&lt;br /&gt;If it turns out to really be a cache issue, I&amp;#39;d look after two causes first: &lt;ul style="list-style: disc outside;"&gt;&lt;li&gt;Did anyone write to the database manually, without using the API? (If yes: Stop doing so)&lt;/li&gt;&lt;li&gt;Are you running a cluster? (if yes: Make sure that the machines are properly communicating and double check the proper cluster setup)&lt;/li&gt;&lt;/ul&gt;If it&amp;#39;s neither of the two, it makes sense to check if there&amp;#39;s an invalidation bug in cache handling. The probability is low though.</summary>
    <dc:creator>Olaf Kock</dc:creator>
    <dc:date>2019-01-30T11:20:54Z</dc:date>
  </entry>
  <entry>
    <title>RE: Is is possible to use permissionChecker in a background service?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112174714" />
    <author>
      <name>旻 吴</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112174714</id>
    <updated>2025-04-15T00:43:36Z</updated>
    <published>2019-01-30T09:05:43Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Thank you for your response. We change use roles in panel UI and tried clear server cache in control panel-server admin but it didn't work.&lt;br&gt;&lt;br&gt;We tracked into permissionChecker's source code and found out that the reason is &lt;br&gt;&lt;pre&gt;&lt;code&gt;UserGroupRoleLocalService.hasUserGroupRole(userId, groupId, SITE_ADMIN, true)&lt;/code&gt;&lt;/pre&gt;does not work. It can not return true even if the userId-groupId-roleId record exists in UserGroupRole table, due to cache issue. But unfortunatelly we haven't found the method to flush that cache yet...&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>旻 吴</dc:creator>
    <dc:date>2019-01-30T09:05:43Z</dc:date>
  </entry>
  <entry>
    <title>RE: Is is possible to use permissionChecker in a background service?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112167207" />
    <author>
      <name>Amos Fong</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112167207</id>
    <updated>2019-01-29T18:01:32Z</updated>
    <published>2019-01-29T18:01:32Z</published>
    <summary type="html">&lt;blockquote&gt;if we change the role of a user to site admin&lt;/blockquote&gt;Are you doing this with LocalServiceUtils or via the UI in control panel? It should clear the proper cache if you are which I think is the entityCache.&lt;br /&gt;&lt;br /&gt;If you hit any of the clear cache buttons in control panel &amp;gt; server admin, does that fix the issue?</summary>
    <dc:creator>Amos Fong</dc:creator>
    <dc:date>2019-01-29T18:01:32Z</dc:date>
  </entry>
  <entry>
    <title>Is is possible to use permissionChecker in a background service?</title>
    <link rel="alternate" href="https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112148623" />
    <author>
      <name>旻 吴</name>
    </author>
    <id>https://liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=112148623</id>
    <updated>2025-04-15T00:43:36Z</updated>
    <published>2019-01-28T02:24:14Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;p&gt;Hello everyone. I am wondering if it is possible to use
  permissionChecker in a background service without user login.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;We know that with a login user and in the thread of that user, one
  can call &lt;code&gt;PermissionThreadLocal.getPermissionChecker()&lt;/code&gt; to
  get permissionChecker instance of that user and do checks such as
    &lt;code&gt;permissionChecker.isGroupAdmin(long groupId)&lt;/code&gt;.
  Furthermore, the permissionChecker's return &lt;strong&gt;is flushed as soon
  as&lt;/strong&gt; a user is assgined /unassgined&amp;nbsp;as a site admin.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;However, if we create a background service and
    use&amp;nbsp;&lt;code&gt;PermissionChecker permissionChecker =
  PermissionCheckerFactoryUtil.create(user)&lt;/code&gt; , the returning
  permissionChecker is not flushed when the role of the user is changed.
  That is, if we change the role of a user to site admin, the
  &lt;code&gt;permissionChecker.isGroupAdmin&lt;/code&gt; will return false until we
  restart the liferay server.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Here is the code:&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;pre&gt;
@Component(
    immediate = true,
    property = {
        "osgi.command.scope=liferay",
        "osgi.command.function=checkPermission"
    },
    service = PermissionCheckerTest.class
)
public class PermissionCheckerTest {
    
    private void print(String s) { System.out.println(s);}    

    public void checkPermission(long userId, long groupId) {
        
        try {
          long oldId = PrincipalThreadLocal.getUserId();    

          PrincipalThreadLocal.setName(userId);
          User user = _userLocalService.getUser(userId);
            
          PermissionCacheUtil.clearCache();
          PermissionCacheUtil.clearPrimaryKeyRoleCache();
          PermissionChecker permissionChecker = PermissionCheckerFactoryUtil.create(user);    

          PermissionChecker opc = PermissionThreadLocal.getPermissionChecker();        
          PermissionThreadLocal.setPermissionChecker(permissionChecker);

          try {
            print("User has site admin role: " + permissionChecker.isGroupAdmin(groupId));
          } finally {
            PermissionThreadLocal.setPermissionChecker(opc);
            PrincipalThreadLocal.setName(oldId);
          }
        } catch (Exception ex) {
            print(ex.getMessage());
        }
    }

    @Reference
    private UserLocalService _userLocalService;    

}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I think the problem is the role cache is not flushed. But how could I
  do it? Thank you very much.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>旻 吴</dc:creator>
    <dc:date>2019-01-28T02:24:14Z</dc:date>
  </entry>
</feed>
