Add Role for User in Liferay Programmatically

I need to assign roles to my Liferay users on login.

I implemented all the logic in the authenticateByScreenName method of the user class that implements the Authenticator.

Code example:

public class ESBAuthenticator implements Authenticator{ public int authenticateByScreenName(long companyId, String screenName, String password, Map<String, String[]> headerMap, Map<String, String[]> 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); } } 

When I log into the system, apparently this works, I check the liferay database tables and they are updated, my user has the "Administrator" role assigned. However, the "Admin" option is not displayed on the portal in the interface.

enter image description here

But if I go to "My Account", click on the "Save" button, log out and log in. I have admin access options available.

Does anyone know why this is happening? I call "updateUser ()" after assigning the role. Is this not the same as the save button?

Possible solution: I found that if I clear the contents cached through the cluster, it works fine. I found it in this post:

https://www.liferay.com/es/web/kamesh.sampath1/blog/-/blogs/how-to-clear-liferay-cache

Add the following line:

 MultiVMPoolUtil.clear(); 

Does anyone know if this solution is correct? I can’t find what liferay does when the "Save" button on the "my_account" page is clicked. Maybe this will clear this cache? I was looking for synchronization with the database function, but could not find anything. It seems that if the column is updated, liferay does not use it if it is cached: (.

+5
source share
3 answers

I can give you a cheap hack.

Step 1. Make sure you have user credentials.

Step 2: Do as needed to change user roles, etc.

Step 3: Clear all cache types (client or server) associated with the stream

Step 4: redirect the user to a temporary view in which user credentials will be saved automatically and then redirected to the portal.

Lemme knows if it works

0
source

I think the clear cache solution works because you need to delete all cached repsones portlets. This is what my_account does.

  // Clear cached portlet responses PortletSession portletSession = actionRequest.getPortletSession(); InvokerPortletImpl.clearResponses(portletSession); 

The problem is that InvokerPortletImpl is not displayed externally. to replicate this functionally, you can try login.events.post and get the response cache with HttpSession e clear the map;

  httprequest.getSession().getAttribute("CACHE_PORTLET_RESPONSES").clear() 

but this is how to crack it better in this case MultiVMPoolUtil.clear ();

0
source

When you call the method by which you pass the screen name

  setProfile(companyId, screenname); 

But in ur setProfileMethod you use UserId

  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); } 

Use method

 UserLocalServiceUtil.fetchUserByScreenName(companyId, screenName) 
-1
source

Source: https://habr.com/ru/post/1238857/


All Articles