porlet的权限控制示例

来源:互联网 发布:js 图片放大缩小 编辑:程序博客网 时间:2024/05/21 09:47

1.permission.xml 该文件最终在运行放置在classes/resource-actions目录,因此需要在工程的资源文件添加文件夹resource-actions

<?xml version="1.0"?>
<resource-action-mapping>
    <portlet-resource>
        <portlet-name>serverInfoPortlet</portlet-name>
        <supports>
            <action-key>YOUR_DELETE</action-key>
            <action-key>YOUR_ADD</action-key>
            <action-key>YOUR_EDIT</action-key>
            <action-key>CONFIGURATION</action-key>
            <action-key>VIEW</action-key>
        </supports>
        <community-defaults>
            <action-key>VIEW</action-key>
        </community-defaults>
        <guest-defaults>
            <action-key>VIEW</action-key>
        </guest-defaults>
        <guest-unsupported>
            <action-key>YOUR_DELETE</action-key>
            <action-key>YOUR__ADD</action-key>
            <action-key>YOUR_EDIT</action-key>
        </guest-unsupported>
    </portlet-resource>
</resource-action-mapping>

2.添加portlet.properties,该文件在运行放置在classes目录下 内容如下

resource.actions.configs=resource-actions/permission.xml
include-and-override=portlet-ext.properties
language.bundle=your.company.path.Language

3.国际化文件Language.properties/Language_zh_CN.properteis/Language_en_US.properties

action.YOUR__DELETE=Server Information Delete
action.YOUR__ADD=Server Information Add
action.YOUR__EDIT=Server Information Edit

4.jsp的应用

a.head.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%@ page import="javax.portlet.*"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>
<%@ taglib uri="http://liferay.com/tld/theme" prefix="liferay-theme" %>

<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

<%@ page import="com.liferay.portal.theme.ThemeDisplay" %>
<portlet:defineObjects />
<liferay-theme:defineObjects />

<%
 PortletPreferences prefs = renderRequest.getPreferences();
String rootPath = request.getContextPath();
 %>
b.view.jsp

 <%@ include file="/common/header.jsp" %>
 <%
        long groupId = scopeGroupId;
        String name = portletDisplay.getRootPortletId();
        String primKey = portletDisplay.getResourcePK();
        String actionId1 = "YOUR_DELETE";
        String actionId2 = "YOUR_ADD";
        String actionId3 = "YOUR_EDIT";
%>
   <c:choose>
             <c:when test="<%= permissionChecker.hasPermission(groupId, name, primKey, actionId1/2/3) %>">

                   your control button or message
             </c:when>
   </c:choose>