如何在Liferay中为自定义角色的布局添加VIEW权限

时间:2017-02-02 18:49:29

标签: java permissions liferay administration liferay-6.2

我一直在尝试传播自定义角色布局的“查看”权限,对于该布局和该自定义角色,resourcepermission表中不存在任何条目。如下面的代码所示,布局权限应该使用

进行理想设置
ResourcePermissionServiceUtil.setIndividualResourcePermissions(groupId, companyId, layout.getModelClassName(),
                "primkey", roleIdsToActionIds);

但是,这里第一次调用有效,但第二次调用设置当前布局的父布局权限,它会出现错误日志中显示的错误。有人可以指导我如何实现这个目标吗?

    // Propagete VIEW access to Page, on which Portlet is added - START
long plid = LayoutLocalServiceUtil.getDefaultPlid(groupId, false, portletId);
if(plid!=0){
Layout layout = LayoutLocalServiceUtil.getLayout(plid); 
if(layout!=null){
    _log.info("Adding permission for layout: "+layout+", layout model class name:"+layout.getModelClassName()+", scope:"+ResourceConstants.SCOPE_INDIVIDUAL
            +", primkey:"+String.valueOf(plid)+", roleid:"+customRoleId+", actionid:"+ ActionKeys.VIEW);

    Map<Long, String[]> roleIdsToActionIds = new HashMap<Long, String[]>();
    List<String> actionIds = new ArrayList<String>();
    actionIds.add(ActionKeys.VIEW);

    roleIdsToActionIds.put(customRoleId, actionIds.toArray(new String[actionIds.size()]));

    ResourcePermissionServiceUtil.setIndividualResourcePermissions(groupId, companyId, layout.getModelClassName(),
            String.valueOf(plid), roleIdsToActionIds);


    // TODO: give view permission to all its parent layouts as well
    while(layout.getParentLayoutId()!=0){
        layout = LayoutLocalServiceUtil.getParentLayout(layout);
        _log.info("Adding permission for parent layout: "+layout+", layout model class name:"+layout.getModelClassName()+", scope:"+ResourceConstants.SCOPE_INDIVIDUAL
                +", primkey:"+String.valueOf(layout.getPlid())+", roleid:"+customRoleId+", actionid:"+ ActionKeys.VIEW);

        ResourcePermissionServiceUtil.setIndividualResourcePermissions(groupId, companyId, layout.getModelClassName(),
                String.valueOf(layout.getPlid()), roleIdsToActionIds);
    }

    _log.debug("Added view permission for page of portlet:"+portletId);

    }   
} // Propagete VIEW access to Page, on which Portlet is added - END

错误日志:

    ...


Adding permission for layout: {uuid=e3d30768-8025-494b-b651-aa12e34baa45, plid=100207, groupId=AAA, companyId=XXX, userId=20199, userName=Liferay Admin, createDate=Sat Jan 14 00:56:55 GMT 2017, modifiedDate=Sat Jan 14 00:58:48 GMT 2017, privateLayout=false, layoutId=89, parentLayoutId=30, name=<?xml version='1.0' encoding='UTF-8'?><root available-locales="en_US" default-locale="en_US"><Name language-id="en_US">Notifications</Name></root>, title=, description=, keywords=, robots=, type=portlet, typeSettings=column-1=XXX_WAR_YYY,
    column-2=XXXZZZ_WAR_XXXWWW
    layout-template-id=my_custom_layout
    , hidden=false, friendlyURL=/notifications, iconImage=false, iconImageId=0, themeId=, colorSchemeId=, wapThemeId=, wapColorSchemeId=, css=, priority=4, layoutPrototypeUuid=, layoutPrototypeLinkEnabled=false, sourcePrototypeLayoutUuid=}, layout model class name:com.liferay.portal.model.Layout, scope:4, primkey:100207, roleid:73933, actionid:VIEW
    02:01:14,465 INFO  [http-bio-443-exec-46][setupServiceImpl:955] layout.getParentLayoutId():30
    02:01:14,466 INFO  [http-bio-443-exec-46][setupServiceImpl:958] Adding permission for parent layout: {uuid=f7202f94-3660-426f-bd18-7e083668d5ac, plid=26752, groupId=AAA, companyId=XXX, userId=XXX, userName=XXX Admin, createDate=Thu Apr 14 15:59:06 GMT 2016, modifiedDate=Wed Dec 07 20:33:22 GMT 2016, privateLayout=false, layoutId=30, parentLayoutId=3, name=<?xml version='1.0' encoding='UTF-8'?><root available-locales="en_US" default-locale="en_US"><Name language-id="en_US">Users</Name></root>, title=, description=, keywords=, robots=, type=link_to_layout, typeSettings=column-1-customizable=false
    column-2-customizable=false
    groupId=AAA
    layout-template-id=my_custom_layout
    layoutUpdateable=true
    linkToLayoutId=31
    privateLayout=false
    sitemap-changefreq=daily
    sitemap-include=1
    , hidden=false, friendlyURL=/users, iconImage=false, iconImageId=0, themeId=classic, colorSchemeId=, wapThemeId=, wapColorSchemeId=, css=, priority=9, layoutPrototypeUuid=, layoutPrototypeLinkEnabled=false, sourcePrototypeLayoutUuid=}, layout model class name:com.liferay.portal.model.Layout, scope:4, primkey:26752, roleid:73933, actionid:VIEW
    java.lang.NullPointerException
            at com.liferay.portal.service.impl.ResourcePermissionLocalServiceImpl.doUpdateResourcePermission(ResourcePermissionLocalServiceImpl.java:1200)
            at com.liferay.portal.service.impl.ResourcePermissionLocalServiceImpl.doUpdateResourcePermission(ResourcePermissionLocalServiceImpl.java:1253)
            at com.liferay.portal.service.impl.ResourcePermissionLocalServiceImpl.updateResourcePermission(ResourcePermissionLocalServiceImpl.java:1407)
            at com.liferay.portal.service.impl.ResourcePermissionLocalServiceImpl.setResourcePermissions(ResourcePermissionLocalServiceImpl.java:1134)
            at sun.reflect.GeneratedMethodAccessor1164.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:115)
            at com.liferay.portal.spring.transaction.DefaultTransactionExecutor.execute(DefaultTransactionExecutor.java:62)
            at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:51)
            at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:111)
            at com.liferay.portal.spring.aop.ServiceBeanAopProxy.invoke(ServiceBeanAopProxy.java:175)
            at com.sun.proxy.$Proxy152.setResourcePermissions(Unknown Source)
            at com.liferay.portal.service.impl.ResourcePermissionServiceImpl.setIndividualResourcePermissions(ResourcePermissionServiceImpl.java:238)
            at sun.reflect.GeneratedMethodAccessor1163.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:115)
            at com.liferay.portal.spring.transaction.DefaultTransactionExecutor.execute(DefaultTransactionExecutor.java:62)
            at com.liferay.portal.spring.transaction.TransactionInterceptor.invoke(TransactionInterceptor.java:51)
            at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:111)
            at com.liferay.portal.spring.aop.ChainableMethodAdvice.invoke(ChainableMethodAdvice.java:56)
            at com.liferay.portal.spring.aop.ServiceBeanMethodInvocation.proceed(ServiceBeanMethodInvocation.java:111)
            at com.liferay.portal.spring.aop.ServiceBeanAopProxy.invoke(ServiceBeanAopProxy.java:175)
            at com.sun.proxy.$Proxy153.setIndividualResourcePermissions(Unknown Source)
            at com.liferay.portal.service.ResourcePermissionServiceUtil.setIndividualResourcePermissions(ResourcePermissionServiceUtil.java:245)

1 个答案:

答案 0 :(得分:1)

所以最后看起来这个API调用修复了我的问题。

ResourcePermissionLocalServiceUtil.setResourcePermissions(
                                                companyId, Layout.class.getName(),
                                                ResourceConstants.SCOPE_INDIVIDUAL,
                                                String.valueOf(layout.getPlid()), customRoleId,
                                                new String[] {
                                                    ActionKeys.VIEW
                                                });

在尝试这个之前,我还尝试了其他两个Liferay API而没有成功。

ResourcePermissionLocalServiceUtil.setResourcePermissions(companyId, layout.getModelClassName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(layout.getPlid()), roleIdsToActionIds);

ResourcePermissionServiceUtil.addResourcePermission(groupId, companyId, layout.getModelClassName(), ResourceConstants.SCOPE_INDIVIDUAL, String.valueOf(layout.getPlid()), customRoleId, ActionKeys.VIEW);

两者都失败了。

java.lang.NullPointerException
    at com.liferay.portal.service.impl.ResourcePermissionLocalServiceImpl.doUpdateResourcePermission(ResourcePermissionLocalServiceImpl.java:1200)
    at com.liferay.portal.service.impl.ResourcePermissionLocalServiceImpl.doUpdateResourcePermission(ResourcePermissionLocalServiceImpl.java:1253)
    at com.liferay.portal.service.impl.ResourcePermissionLocalServiceImpl.updateResourcePermission(ResourcePermissionLocalServiceImpl.java:1407)
    at com.liferay.portal.service.impl.ResourcePermissionLocalServiceImpl.setResourcePermissions(ResourcePermissionLocalServiceImpl.java:1134)