简体   繁体   English

Liferay API:通过权限获取角色

[英]Liferay API: Get Roles via Permissions

At the moment I'm trying to create an overview where I list all the Layouts and Portlets and Roles (Permissions) that are activated for every of them. 目前,我正在尝试创建一个概述,其中列出所有已激活的布局和Portlet以及角色(权限)。

I think I'm pretty near to the solution already but the last bit is missing. 我想我已经很接近解决方案了,但是最后一点丢失了。

I already got all kind of information I need. 我已经获得了所需的各种信息。 I got form Layout --> to Resource --> to Permission. 我得到了表格布局->资源->权限。 But now I don't know how to get from the Permissions (or permissionId) to the actual Roles via the Liferay API. 但是现在我不知道如何通过Liferay API从Permissions(或PermissionsId)获取实际的角色。

what I have: Layout, Resource, Permission what I need: Roles 我所拥有的:布局,资源,权限我所需要的:角色

Using Liferay 5.2.4 使用Liferay 5.2.4

you can use RoleLocalServiceUtil. 您可以使用RoleLocalServiceUtil。 companyId you can get over the Liferay user by calling user.getCompanyId. 您可以通过调用user.getCompanyId来超越Liferay用户的companyId。 and user you can retrieve by this way: user = UserLocalServiceUtil.getUserById(id); 和可以通过这种方式检索的用户:user = UserLocalServiceUtil.getUserById(id);

private static Role getLiferayRole(String roleName, long companyId) {
    Role role = null;
    try {
        role = RoleLocalServiceUtil.getRole(companyId, roleName);
    } catch (Exception e) {
        LOG.error(e.getMessage());
    } 
    return role;
}

Okay, I think I finally found it out myself. 好吧,我想我终于找到了答案。 All I'm doing now is: 我现在正在做的是:

List<Layout> tempPages = LayoutLocalServiceUtil.getLayouts( 0, LayoutLocalServiceUtil.getLayoutsCount() );
List<Role> allAvailRoles = RoleLocalServiceUtil.getRoles( 0, RoleLocalServiceUtil.getRolesCount() );
for ( Layout page : tempPages )
{
    List<Role> mappedRoles = new ArrayList<Role>();
    for ( Role role : allAvailRoles )
    {
        boolean hasRolePermission = PermissionLocalServiceUtil.hasRolePermission( role.getRoleId(),
                                                                                  page.getCompanyId(),
                                                                                  Layout.class.getName(),
                                                                                  ResourceConstants.SCOPE_INDIVIDUAL,
                                                                                  String.valueOf( page.getPrimaryKey() ),
                                                                                  "VIEW" );
        if ( hasRolePermission )
        {
           mappedRoles.add( role );
        }
     }
     pages.add( new LayoutRoleModel( page, mappedRoles ) );
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM