简体   繁体   English

以编程方式在Liferay中为用户添加角色

[英]Add role to User in Liferay programmatically

I need to assign roles to my Liferay's users when they log in the application. 我需要在登录应用程序时为Liferay的用户分配角色。

I have implemented all the logic in the 'authenticateByScreenName' method of a custom class that implements 'Authenticator' . 我已经实现了实现“Authenticator”的自定义类的“authenticateByScreenName”方法中的所有逻辑。

Example code: 示例代码:

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-in, apparently it works, I check liferay database's tables and they are updated, my user has "Administrator" role assigned. 当我登录时,显然它可以工作,我检查了liferay数据库的表并且它们已经更新,我的用户已经分配了“管理员”角色。 However, the portal in front-end doesn't show the "Admin" option. 但是,前端的门户网站不显示“Admin”选项。

在此输入图像描述

But If I go to 'My Account', press the 'save' button, log-out and log-in again I have the Admin options availables. 但如果我转到“我的帐户”,请按“保存”按钮,注销并再次登录我可以使用管理员选项。

Anyone know why this happens?, I am calling 'updateUser()' after assign the role, It is not the same as the 'save' button? 有人知道为什么会这样吗?我在分配角色后调用'updateUser()',它与'save'按钮不一样?

Possible solution: I have found that If I clear the content cached across 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 https://www.liferay.com/es/web/kamesh.sampath1/blog/-/blogs/how-to-clear-liferay-cache

Add the following line: 添加以下行:

MultiVMPoolUtil.clear();

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. 任何人都知道这是否是正确的解决方案?,当按下“my_account”页面中的“保存”按钮时,我无法找到liferay的内容。 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 :(. 似乎是如果一个列被更新,如果它被缓存,liferay不会使用它:(。

I can give you a cheap hack. 我可以给你一个廉价的黑客。

Step 1: Make sure you have the hold of user credential. 第1步:确保您拥有用户凭据。

Step 2: Do as necessary to change user roles etc 第2步:根据需要更改用户角色等

Step 3: Flush all kinds of cache (client or server) related to the flow 步骤3:刷新与流相关的各种缓存(客户端或服务器)

Step 4: redirect the user to a temp view where the user credential you held will be applied automatically and then redirected to the portal. 步骤4:将用户重定向到临时视图,您将自动应用您持有的用户凭据,然后重定向到门户。

Lemme know whether it works Lemme知道它是否有效

I think that the solution of clear cache works because you need to remove all cached portlets repsones. 我认为清除缓存的解决方案是有效的,因为您需要删除所有缓存的portlet repsones。 This is the way my_account do it. 这是my_account这样做的方式。

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

The problem is that InvokerPortletImpl is not visible externally. 问题是InvokerPortletImpl在外部不可见。 to replicate this functionally you can try a login.events.post and get the responses cache from the HttpSession e clear the map; 要在功能上进行复制,您可以尝试登录.eff。来获取HttpSession的响应缓存并清除地图;

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

but it is like a hack its better in this case MultiVMPoolUtil.clear(); 但在这种情况下,它更像是一个黑客攻击MultiVMPoolUtil.clear();

When you call the method you are passing screenName 当您调用方法时,您将传递screenName

 setProfile(companyId, screenname);

But in ur setProfileMethod, you are using UserId 但是在你的setProfileMethod中,你正在使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何以编程方式在liferay中添加站点? - How to add sites in liferay programmatically? 在 Liferay 7.2 中以编程方式在 Web 内容中添加类别 - Add the category in the web content programmatically in Liferay 7.2 如何按名称为 liferay 用户拥有的每个角色获取权限 - How to get permissions by name for each role a liferay user has Liferay 6.1-如何以编程方式向用户分配角色(在挂钩中) - Liferay 6.1 - How to programatically assign a role to a user (in a hook) Liferay kaleo工作流程通知,将电子邮件发送给特定用户而不是角色 - Liferay kaleo workflow notifications, send email to specific user not to role 为超级管理员以外的角色更新Liferay中的用户个人资料图片 - Updating user profile picture in liferay for role other than super admin 如何在 Liferay 中为自定义角色的布局添加 VIEW 权限 - How to Add VIEW permission for layout for custom role in Liferay JDA - 如果添加了某个角色,则向用户添加角色 - JDA - Add Role to User if a certain Role is added 如何复制Liferay中的现有组织角色并将其添加到已经具有原始角色的用户中? - How to copy an existing organisation role in Liferay and add it to Users that already had the original role? 在JSF网站上登录时以编程方式为用户分配安全角色 - Programmatically assigning security role to user on login in JSF website
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM