title: Add role to User in Liferay programmatically
body: <p>I need to assign roles to my Liferay's users when they log in the application.</p>
  
  <p>I have implemented all the logic in the <em>'authenticateByScreenName'</em> method of a custom class that implements <em>'Authenticator'</em>. </p>
  
  <p>Example code:</p>
  
  <pre><code>public class ESBAuthenticator implements Authenticator{
  
        public int authenticateByScreenName(long companyId, String screenName, String password,
              Map&lt;String, String[]&gt; headerMap, Map&lt;String, String[]&gt; parameterMap)
  
                setProfile(companyId, screenname);
                return 1;
       }
  
       public static void setProfile(long companyId, long userId){
             User user = UserLocalServiceUtil.getUser(userId);
             Role liferayRole = RoleLocalServiceUtil.fetchRole(companyId, "Administrator");
             RoleLocalServiceUtil.addUserRole(user.getUserId(), liferayRole.getRoleId());
             UserLocalServiceUtil.updateUser(user);
       }
   }
  </code></pre>
  
  <p>When I log-in, apparently it works, I check liferay database's tables and they are updated, my user has "Administrator" role assigned. However, the portal in front-end doesn't show the "Admin" option.</p>
  
  <p><a href="https://i.stack.imgur.com/U9lWr.png"><img src="https://i.stack.imgur.com/U9lWr.png" alt="enter image description here"></a></p>
  
  <p>But If I go to 'My Account', press the 'save' button, log-out and log-in again I have the Admin options availables.</p>
  
  <p>Anyone know why this happens?, I am calling 'updateUser()' after assign the role, It is not the same as the 'save' button?</p>
  
  <p><strong>Possible solution:</strong>
  I have found that If I clear the content cached across the cluster it works fine. I found it in this post:</p>
  
  <p><a href="https://www.liferay.com/es/web/kamesh.sampath1/blog/-/blogs/how-to-clear-liferay-cache">https://www.liferay.com/es/web/kamesh.sampath1/blog/-/blogs/how-to-clear-liferay-cache</a></p>
  
  <p>Add the following line:</p>
  
  <pre><code>MultiVMPoolUtil.clear();
  </code></pre>
  
  <p>Anyone know if is this the right solution?, I can't find what does liferay when the "save" button from the "my_account" page is pressed. Maybe it clear this cache?. I was searching for a synchronize with database function but couldn't find anything. It seems to be that if a column is updated, liferay doesn't use it if it's cached :(.</p>