Liferay Portal的权限框架采用声明式配置,类似Service Builder。您可以在XML文件中定义所有的权限,这些权限被称为default.xml。(叫别的名字也行)。然后,在代码中的以下位置实现权限检查:
- 视图层,当显示链接或按钮以保护功能时
- Action,在执行受保护的Action之前
- Service,在调用local service之前
首先要定义好权限。考虑应用程序的用例,以及如何控制对该功能的访问:
- Add Guestbook button 仅允许管理员使用。
- Guestbook选项卡应该通过权限进行过滤,这样管理员就可以控制谁可以看到它们。
- 为了防止匿名用户发送垃圾留言,添加条目按钮只允许站点成员可用。
- 用户可对自己的条目设置权限。。
现在可以创建权限配置了。应用程序中的对象(如Guestbook
和Entry
)被定义为resource,resource actions管理用户如何与这些资源进行交互。因此,有两种权限:portlet权限和资源(或模型)权限。Portlet权限保护 global functions,例如Add Entry。如果用户没有访问该 global functions的权限,他们就失去了一个portlet权限。资源权限保护对象访问,如Guestbook和Entry。用户可能拥有查看一个条目、查看和编辑另一个条目的权限,并且可能无法访问第三个条目。这取决于资源权限。

图1:Portlet权限和资源权限涵盖了应用程序的不同部分。
第一件事是告诉框架权限在哪里定义:
- In
guestbook-service
’s src/main/resources
folder, create a file called portlet.properties
. - In this file, place the following property:
resource.actions.configs=META-INF/resource-actions/default.xml
该属性定设置权限定义文件的名称和位置。
接下来,创建权限文件:
- In the
META-INF
folder, create a subfolder called resource-actions
. - Create a new file in this folder called
default.xml
. - Click the Source tab. Add the following
DOCTYPE
declaration to the top of the file:
<?xml version="1.0"?>
<!DOCTYPE resource-action-mapping PUBLIC "-//Liferay//DTD Resource Action
Mapping 7.0.0//EN" "http://www.liferay.com/dtd/liferay-resource-action-mapping_7_0_0.dtd">
4. Place the following wrapper tags into your default.xml
file, below the DOCTYPE
declaration:
<resource-action-mapping>
</resource-action-mapping>
您将在这些标签中定义您的资源和模型权限。
5. Next, place the permissions for your com.liferay.docs.guestbook
package between the <resource-action-mapping>
tags:
<model-resource>
<model-name>com.liferay.docs.guestbook</model-name>
<portlet-ref>
<portlet-name>com_liferay_docs_guestbook_portlet_GuestbookPortlet</portlet-name>
<portlet-name>com_liferay_docs_guestbook_portlet_GuestbookAdminPortlet</portlet-name>
</portlet-ref>
<root>true</root>
<permissions>
<supports>
<action-key>ADD_GUESTBOOK</action-key>
<action-key>ADD_ENTRY</action-key>
<action-key>VIEW</action-key>
</supports>
<site-member-defaults>
<action-key>ADD_ENTRY</action-key>
</site-member-defaults>
<guest-defaults>
<action-key>VIEW</action-key>
</guest-defaults>
<guest-unsupported>
<action-key>ADD_GUESTBOOK</action-key>
<action-key>ADD_ENTRY</action-key>
</guest-unsupported>
</permissions>
</model-resource>
这定义了Guestbook和条目实体的基准配置。支持的操作是ADD_GUESTBOOK和ADD_ENTRY。默认情况下,站点成员可以ADD_ENTRY,而来宾不能执行任何操作(但他们可以查看)。
- Below that, but above the closing
</resource-action-mapping>
, place the Guestbook
model permissions:<model-resource>
<model-name>com.liferay.docs.guestbook.model.Guestbook</model-name>
<portlet-ref>
<portlet-name>com_liferay_docs_guestbook_portlet_GuestbookPortlet</portlet-name>
<portlet-name>com_liferay_docs_guestbook_portlet_GuestbookAdminPortlet</portlet-name>
</portlet-ref>
<permissions>
<supports>
<action-key>ADD_ENTRY</action-key>
<action-key>DELETE</action-key>
<action-key>PERMISSIONS</action-key>
<action-key>UPDATE</action-key>
<action-key>VIEW</action-key>
</supports>
<site-member-defaults>
<action-key>ADD_ENTRY</action-key>
<action-key>VIEW</action-key>
</site-member-defaults>
<guest-defaults>
<action-key>VIEW</action-key>
</guest-defaults>
<guest-unsupported>
<action-key>UPDATE</action-key>
</guest-unsupported>
</permissions>
</model-resource>
这定义了Guestbook
特定的操作,包括添加,删除,更新和查看。默认情况下,网站成员和访客可以查看留言簿,但访客无法更新它们。
- Below the
Guestbook
model permissions, but still above the closing </resource-action-mapping>
, place the Entry
model permissions:<model-resource>
<model-name>com.liferay.docs.guestbook.model.Entry</model-name>
<portlet-ref>
<portlet-name>com_liferay_docs_guestbook_portlet_GuestbookPortlet</portlet-name>
</portlet-ref>
<permissions>
<supports>
<action-key>DELETE</action-key>
<action-key>PERMISSIONS</action-key>
<action-key>UPDATE</action-key>
<action-key>VIEW</action-key>
</supports>
<site-member-defaults>
<action-key>VIEW</action-key>
</site-member-defaults>
<guest-defaults>
<action-key>VIEW</action-key>
</guest-defaults>
<guest-unsupported>
<action-key>UPDATE</action-key>
</guest-unsupported>
</permissions>
</model-resource>
这定义了Entry
特定的操作。默认情况下,网站成员可以添加或查看条目,而访客只能查看条目。
- 保存文件。
这里定义好了模型级别的权限,但还必须定义Portlet权限。这些是在guestbook-web模块中管理的,其中包含了portlet类。按照以下步骤在guestbook-web模块中添加portlet权限:
- In
guestbook-web
’s src/main/resources
folder, create a file called portlet.properties
. - In this file, place the following property:
resource.actions.configs=META-INF/resource-actions/default.xml
- Create a subfolder called
resource-actions
in the src/main/resources/META-INF
folder. - Create a new file in this folder called
default.xml
. - Add the following
DOCTYPE
declaration to the top of the file:<?xml version="1.0"?>
<!DOCTYPE resource-action-mapping PUBLIC "-//Liferay//DTD Resource Action
Mapping 7.0.0//EN" "http://www.liferay.com/dtd/liferay-resource-action-mapping_7_0_0.dtd">
- Below the
DOCTYPE
declaration, add the following resource-action-mapping
tags:<resource-action-mapping>
</resource-action-mapping>
You’ll define your portlet permissions inside these tags.
- Insert this block of code inside the
resource-action-mapping
tags:<portlet-resource>
<portlet-name>com_liferay_docs_guestbook_portlet_GuestbookAdminPortlet</portlet-name>
<permissions>
<supports>
<action-key>ACCESS_IN_CONTROL_PANEL</action-key>
<action-key>CONFIGURATION</action-key>
<action-key>VIEW</action-key>
</supports>
<site-member-defaults>
<action-key>VIEW</action-key>
</site-member-defaults>
<guest-defaults>
<action-key>VIEW</action-key>
</guest-defaults>
<guest-unsupported>
<action-key>ACCESS_IN_CONTROL_PANEL</action-key>
<action-key>CONFIGURATION</action-key>
</guest-unsupported>
</permissions>
</portlet-resource>
这定义了Guestbook Admin portlet的默认权限。支持的Actions有ACCESS_IN_CONTROL_PANEL
,CONFIGURATION
和VIEW
。虽然任何人都可以查看该应用,但访客和网站成员无法在控制面板中配置或访问该应用。由于它是一个控制面板portlet,这意味着只有管理员才能访问。
- Below the Guestbook Admin permissions, insert this block of code:
<portlet-resource>
<portlet-name>com_liferay_docs_guestbook_portlet_GuestbookPortlet</portlet-name>
<permissions>
<supports>
<action-key>ADD_TO_PAGE</action-key>
<action-key>CONFIGURATION</action-key>
<action-key>VIEW</action-key>
</supports>
<site-member-defaults>
<action-key>VIEW</action-key>
</site-member-defaults>
<guest-defaults>
<action-key>VIEW</action-key>
</guest-defaults>
<guest-unsupported />
</permissions>
</portlet-resource>
这定义了Guestbook portlet的权限。它支持ADD_TO_PAGE、配置和视图的操作。站点成员和来宾在默认情况下获得VIEW权限。
- 保存文件。
现在已经成功为应用程序设计和实施了权限方案。接下来,将创建Java代码以支持服务层中的权限。