9 Replies Latest reply on May 19, 2006 7:51 PM by 702619

    Portal角色权限管理API例子

    702619
      要运行该例子,请仔细阅读如下步骤.

      1. 作为CONTROL PROJECT导入PortalRoleManagementControl目录到你的PORTAL应用.
      2. 创建一个新的(或使用已存在的)一个Portal Web Project inside your portal application
      3. 作为 WEB PROJECT导入testRoleControl directory到你的portal web project
      4. 编译PortalRoleManagementControl project
      5. Start up portal server
      6. 进入Portal Admin Tool创建一个testRole的角色.
      7. 进入workshop测试testRoleControl.

      文件说明
      RoleManager.java 封装的角色查询,创建,给角色增加用户等API
      RoleManagerImpl.jcs JAVA控件
      Entitlements.jsp 对具体PORTLET进行角色能力的操作.
        • 1. Re: Portal角色权限管理API例子
          702619
          TestRoleControlController.jpf
          package testRoleControl;
          import com.bea.p13n.security.Authentication;
          import com.bea.wlw.netui.pageflow.FormData;
          import com.bea.wlw.netui.pageflow.Forward;
          import com.bea.wlw.netui.pageflow.PageFlowController;
          import java.util.ArrayList;
          import java.util.Collection;
          import java.util.List;

          /**
          * @jpf:controller
          * @jpf:view-properties view-properties::
          * <!-- This data is auto-generated. Hand-editing this section is not recommended. -->
          * <view-properties>
          * <pageflow-object id="pageflow:/testRoleControl/TestRoleControlController.jpf"/>
          * <pageflow-object id="action:begin.do">
          * <property value="200" name="x"/>
          * <property value="240" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="action:addUsers.do">
          * <property value="400" name="x"/>
          * <property value="320" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="action:getRoles.do">
          * <property value="400" name="x"/>
          * <property value="100" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="action:getAllUsers.do">
          * <property value="720" name="x"/>
          * <property value="100" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="action:removeUsers.do">
          * <property value="340" name="x"/>
          * <property value="420" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="action-call:@page:index.jsp@#@action:getRoles.do@">
          * <property value="276,320,320,364" name="elbowsX"/>
          * <property value="92,92,92,92" name="elbowsY"/>
          * <property value="East_1" name="fromPort"/>
          * <property value="West_1" name="toPort"/>
          * </pageflow-object>
          * <pageflow-object id="page:index.jsp">
          * <property value="240" name="x"/>
          * <property value="100" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="action-call:@page:displayRoles.jsp@#@action:getAllUsers.do@">
          * <property value="596,640,640,684" name="elbowsX"/>
          * <property value="92,92,92,92" name="elbowsY"/>
          * <property value="East_1" name="fromPort"/>
          * <property value="West_1" name="toPort"/>
          * </pageflow-object>
          * <pageflow-object id="page:displayRoles.jsp">
          * <property value="560" name="x"/>
          * <property value="100" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="action-call:@page:displayUsers.jsp@#@action:begin.do@">
          * <property value="684,460,460,236" name="elbowsX"/>
          * <property value="232,232,232,232" name="elbowsY"/>
          * <property value="West_1" name="fromPort"/>
          * <property value="East_1" name="toPort"/>
          * </pageflow-object>
          * <pageflow-object id="page:displayUsers.jsp">
          * <property value="720" name="x"/>
          * <property value="240" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="forward:path#success#index.jsp#@action:begin.do@">
          * <property value="200,200,202,204" name="elbowsX"/>
          * <property value="196,92,92,92" name="elbowsY"/>
          * <property value="North_1" name="fromPort"/>
          * <property value="West_1" name="toPort"/>
          * <property value="success" name="label"/>
          * </pageflow-object>
          * <pageflow-object id="forward:path#success#displayRoles.jsp#@action:getRoles.do@">
          * <property value="436,480,480,524" name="elbowsX"/>
          * <property value="92,92,92,92" name="elbowsY"/>
          * <property value="East_1" name="fromPort"/>
          * <property value="West_1" name="toPort"/>
          * <property value="success" name="label"/>
          * </pageflow-object>
          * <pageflow-object id="forward:path#success#displayUsers.jsp#@action:getAllUsers.do@">
          * <property value="720,720,720,720" name="elbowsX"/>
          * <property value="144,170,170,196" name="elbowsY"/>
          * <property value="South_1" name="fromPort"/>
          * <property value="North_1" name="toPort"/>
          * <property value="success" name="label"/>
          * </pageflow-object>
          * <pageflow-object id="control:rolemanagement.RoleManager#roleManager">
          * <property value="38" name="x"/>
          * <property value="34" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="action-call:@page:index.jsp@#@action:addUsers.do@">
          * <property value="276,320,320,364" name="elbowsX"/>
          * <property value="103,103,312,312" name="elbowsY"/>
          * <property value="East_2" name="fromPort"/>
          * <property value="West_1" name="toPort"/>
          * </pageflow-object>
          * <pageflow-object id="action-call:@page:index.jsp@#@action:removeUsers.do@">
          * <property value="276,290,290,304" name="elbowsX"/>
          * <property value="103,103,412,412" name="elbowsY"/>
          * <property value="East_2" name="fromPort"/>
          * <property value="West_1" name="toPort"/>
          * </pageflow-object>
          * <pageflow-object id="page:confirmation.jsp">
          * <property value="540" name="x"/>
          * <property value="360" name="y"/>
          * </pageflow-object>
          * <pageflow-object id="forward:path#success#confirmation.jsp#@action:addUsers.do@">
          * <property value="436,470,470,504" name="elbowsX"/>
          * <property value="312,312,341,341" name="elbowsY"/>
          * <property value="East_1" name="fromPort"/>
          * <property value="West_0" name="toPort"/>
          * <property value="success" name="label"/>
          * </pageflow-object>
          * <pageflow-object id="forward:path#success#confirmation.jsp#@action:removeUsers.do@">
          * <property value="376,440,440,504" name="elbowsX"/>
          * <property value="412,412,363,363" name="elbowsY"/>
          * <property value="East_1" name="fromPort"/>
          * <property value="West_2" name="toPort"/>
          * <property value="success" name="label"/>
          * </pageflow-object>
          * <pageflow-object id="action-call:@page:confirmation.jsp@#@action:getAllUsers.do@">
          * <property value="576,630,630,684" name="elbowsX"/>
          * <property value="352,352,103,103" name="elbowsY"/>
          * <property value="East_1" name="fromPort"/>
          * <property value="West_2" name="toPort"/>
          * </pageflow-object>
          * </view-properties>
          * ::
          */
          public class TestRoleControlController extends PageFlowController
          {
          /**
          * @common:control
          */
          private rolemanagement.RoleManager roleManager;



          // Uncomment this declaration to access Global.app.
          //
          // protected global.Global globalApp;
          //

          // For an example of page flow exception handling see the example "catch" and "exception-handler"
          // annotations in {project}/WEB-INF/src/global/Global.app

          /**
          * This method represents the point of entry into the pageflow
          * @jpf:action
          * @jpf:forward name="success" path="index.jsp"
          */
          protected Forward begin()
          {
          return new Forward("success");
          }

          /**
          * @jpf:action
          * @jpf:forward name="success" path="confirmation.jsp"
          */
          protected Forward addUsers()
          {
          try
          {
          ArrayList users = new ArrayList();
          users.add( getRequest().getParameter("username") );

          login();
          roleManager.addUsersToRole( getRequest().getParameter("appname"),
          getRequest().getParameter("webname"),
          getRequest().getParameter("rolename"),
          users );


          }
          catch ( Exception ex )
          {
          ex.printStackTrace();
          users = new String[0];
          }
          return new Forward("success");
          }

          /**
          * @jpf:action
          * @jpf:forward name="success" path="displayRoles.jsp"
          */
          protected Forward getRoles()
          {
          login();
          roles = roleManager.getAllRoles( getRequest().getParameter("appname"), getRequest().getParameter("webname") );
          return new Forward("success");
          }

          /**
          * @jpf:action
          * @jpf:forward name="success" path="displayUsers.jsp"
          */
          protected Forward getAllUsers()
          {
          try
          {
          login();
          users = roleManager.getAllUserForRole( getRequest().getParameter("appname"),
          getRequest().getParameter("webname"),
          getRequest().getParameter("rolename") );

          }
          catch ( Exception ex )
          {
          ex.printStackTrace();
          users = new String[0];
          }

          return new Forward("success");
          }

          /**
          * @jpf:action
          * @jpf:forward name="success" path="confirmation.jsp"
          */
          protected Forward removeUsers()
          {
          try
          {
          ArrayList users = new ArrayList();
          users.add( getRequest().getParameter("username") );

          login();
          roleManager.removeUsersFromRole( getRequest().getParameter("appname"),
          getRequest().getParameter("webname"),
          getRequest().getParameter("rolename"),
          users );


          }
          catch ( Exception ex )
          {
          ex.printStackTrace();
          users = new String[0];
          }
          return new Forward("success");
          }

          private void login ()
          {
          try
          {
          if ( null != getRequest().getRemoteUser() )
          return;

          Authentication.login("weblogic", "weblogic", getRequest());
          System.out.print ("---> Login successfully");
          }
          catch (Exception ex)
          {
          ex.printStackTrace();
          }
          }

          public String[] roles;
          public String[] users;

          }
          • 2. Re: Portal角色权限管理API例子
            702619
            <%@ page language="java" contentType="text/html;charset=UTF-8"%>
            <%@ page import="com.bea.p13n.management.ApplicationHelper"%>
            <%@ taglib uri="netui-tags-databinding.tld" prefix="netui-data"%>
            <%@ taglib uri="netui-tags-html.tld" prefix="netui"%>
            <%@ taglib uri="netui-tags-template.tld" prefix="netui-template"%>
            <netui:html>
            <head>
            <title>
            Web Application Page
            </title>
            </head>
            <body>

            <p>
            <%
            String appname = "portalApp";//ApplicationHelper.getApplicationName();
            String webname = "sampleportal";//ApplicationHelper.getWebAppName(request);
            %>

            <netui:anchor action="getRoles">
            Get all roles
            <netui:parameter name="appname" value="<%=appname%>"/>
            <netui:parameter name="webname" value="<%=webname%>"/>
            </netui:anchor>

            for application "<%=appname%>" and portal web application "<%=appname%>"
            </p>

            <p>
            <netui:anchor action="addUsers">
            Add weblogic user to testRole
            <netui:parameter name="appname" value="<%=appname%>"/>
            <netui:parameter name="webname" value="<%=webname%>"/>
            <netui:parameter name="rolename" value="testRole"/>
            <netui:parameter name="username" value="weblogic"/>
            </netui:anchor>

            for application "<%=appname%>" and portal web application "<%=appname%>"
            </p>

            <p>
            <netui:anchor action="removeUsers">
            Remove weblogic user from testRole
            <netui:parameter name="appname" value="<%=appname%>"/>
            <netui:parameter name="webname" value="<%=webname%>"/>
            <netui:parameter name="rolename" value="testRole"/>
            <netui:parameter name="username" value="weblogic"/>
            </netui:anchor>

            for application "<%=appname%>" and portal web application "<%=appname%>"
            </p>
            </body>
            </netui:html>
            • 3. Re: Portal角色权限管理API例子
              702619
              <%@ page language="java" contentType="text/html;charset=UTF-8"%>
              <%@ page
              import="com.bea.netuix.application.manager.entitlements.PortalEntitlementResource"%>
              <%@ page import="com.bea.netuix.util.PortalEntitlementConstants"%>
              <%@ page import="com.bea.p13n.entitlements.common.EntitlementConstants"%>
              <%@ page import="com.bea.p13n.entitlements.common.EntitlementHelper"%>
              <%@ page import="com.bea.p13n.entitlements.common.InvalidArgsException"%>
              <%@ page import="com.bea.p13n.entitlements.management.RolePolicyManager"%>
              <%@ page
              import="com.bea.p13n.entitlements.management.SecurityPolicyManager"%>
              <%@ page import="com.bea.p13n.entitlements.policy.RolePolicyItem"%>
              <%@ page import="com.bea.p13n.entitlements.policy.SecurityPolicyItem"%>
              <%@ page import="com.bea.p13n.entitlements.resource.SecurityResource"%>
              <%@ page import="com.bea.p13n.management.ApplicationHelper"%>
              <%@ page import="java.security.Principal"%>


              <%@ taglib uri="netui-tags-databinding.tld" prefix="netui-data"%>
              <%@ taglib uri="netui-tags-html.tld" prefix="netui"%>
              <%@ taglib uri="netui-tags-template.tld" prefix="netui-template"%>
              <netui:html>
              <%

              Principal principals = request.getUserPrincipal();
              System.out.println(principals.toString());


              String
              ResourceID="com_bea_p13n"+EntitlementConstants.RESOURCE_ID_DELIMITER+"Portlet"+EntitlementConstants.RESOURCE_ID_DELIMITER+"portlet_10";-------com_bea_p13n
              and Portlet are fixed, portlet_10 is portlet label.
              System.out.println(ResourceID);
              String cap = PortalEntitlementConstants.VIEW;
              ArrayList roleList1 = new ArrayList();
              roleList1.add("AuthenticatedVisitor");

              SecurityPolicyItem securityPolicyItem1 = new
              SecurityPolicyItem(ApplicationHelper.getApplicationName(),
              ApplicationHelper.getWebAppName(request),
              ResourceID,
              (List)null,
              (List)null,
              (List)roleList1,
              "view",------or use "cap" variable
              EntitlementHelper.createP13nContextHandler(request));


              try{
              if (SecurityPolicyManager.policyExists(securityPolicyItem1))
              {
              %><B>canRead Security Policy has already been created.<BR><%
              }
              else
              {
              SecurityPolicyManager.createSecurityPolicy(securityPolicyItem1);
              %><B>canRead Security Policy successfully created.<BR><%
              }
              }
              catch (Exception e)
              {
              %><B>Invalid argument exception caught! Incorrect input for role
              policy.<BR><%
              e.printStackTrace();
              }
              finally
              {
              System.out.println(securityPolicyItem1.getCapability().toString());
              System.out.println(securityPolicyItem1.getEntAppName().toString());
              System.out.println(securityPolicyItem1.getWebAppName().toString());
              System.out.println(securityPolicyItem1.getResourceId().toString());
              System.out.println(securityPolicyItem1.getRoleList().toString());
              //System.out.println(securityPolicyItem1.getUserList().toString());
              }
              %>
              </netui:html>
              • 4. Re: Portal角色权限管理API例子
                702619
                多谢!收藏!!!!!
                • 6. Re: Portal角色权限管理API例子
                  702619
                  ddddddd
                  • 7. Re: Portal角色权限管理API例子
                    702619
                    private rolemanagement.RoleManager roleManager
                    这个控件去那找啊,楼主,我刚接触PORTAL还不是很熟悉
                    • 8. Re: Portal角色权限管理API例子
                      702619
                      非常支持!!解我燃眉之急!
                      • 9. Re: Portal角色权限管理API例子
                        702619
                        别外请问一下:要创建角色的API是什么?
                        如果要对创建的桌面中书页、书页、portlet与角色之间的关系也能通过API的方式来实现读、写该如何来做?