1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.security.roles;
18
19 import java.io.IOException;
20 import java.security.Principal;
21 import java.sql.Types;
22 import java.util.ArrayList;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import javax.portlet.ActionRequest;
27 import javax.portlet.ActionResponse;
28 import javax.portlet.PortletConfig;
29 import javax.portlet.PortletException;
30 import javax.portlet.PortletMode;
31 import javax.portlet.RenderRequest;
32 import javax.portlet.RenderResponse;
33
34 import org.apache.jetspeed.CommonPortletServices;
35 import org.apache.jetspeed.portlets.security.SecurityResources;
36 import org.apache.jetspeed.portlets.security.SecurityUtil;
37 import org.apache.jetspeed.security.Role;
38 import org.apache.jetspeed.security.RoleManager;
39 import org.apache.jetspeed.security.SecurityException;
40 import org.apache.portals.gems.browser.BrowserIterator;
41 import org.apache.portals.gems.browser.DatabaseBrowserIterator;
42 import org.apache.portals.gems.browser.BrowserPortlet;
43 import org.apache.portals.gems.util.StatusMessage;
44 import org.apache.portals.messaging.PortletMessaging;
45 import org.apache.velocity.context.Context;
46
47 /***
48 * Role Browser - flat non-hierarchical view
49 *
50 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
51 * @version $Id: RoleBrowser.java 348264 2005-11-22 22:06:45Z taylor $
52 */
53 public class RoleBrowser extends BrowserPortlet
54 {
55 private RoleManager roleManager;
56
57 public void init(PortletConfig config)
58 throws PortletException
59 {
60 super.init(config);
61 roleManager = (RoleManager)
62 getPortletContext().getAttribute(CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
63 if (null == roleManager)
64 {
65 throw new PortletException("Failed to find the Role Manager on portlet initialization");
66 }
67 }
68
69 public void getRows(RenderRequest request, String sql, int windowSize)
70 {
71 getRows(request, sql, windowSize, "");
72 }
73
74 public void getRows(RenderRequest request, String sql, int windowSize, String filter)
75 {
76 List resultSetTitleList = new ArrayList();
77 List resultSetTypeList = new ArrayList();
78 resultSetTypeList.add(String.valueOf(Types.VARCHAR));
79 resultSetTitleList.add("role");
80
81 List list = new ArrayList();
82 try
83 {
84 Iterator roles = roleManager.getRoles(filter);
85
86 while (roles.hasNext())
87 {
88 Role role = (Role)roles.next();
89
90 Principal principal = role.getPrincipal();
91 list.add(principal.getName());
92 }
93 }
94 catch (SecurityException sex)
95 {
96 SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_ROLES, sex.getMessage());
97 }
98 BrowserIterator iterator = new DatabaseBrowserIterator(list, resultSetTitleList, resultSetTypeList,windowSize);
99 setBrowserIterator(request, iterator);
100 iterator.sort("role");
101 }
102
103 public void doView(RenderRequest request, RenderResponse response)
104 throws PortletException, IOException
105 {
106 String selected = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_SELECTED);
107 if (selected != null)
108 {
109 Context context = this.getContext(request);
110 context.put("selected", selected);
111 }
112 StatusMessage msg = (StatusMessage)PortletMessaging.consume(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_STATUS);
113 if (msg != null)
114 {
115 this.getContext(request).put("statusMsg", msg);
116 }
117
118 String filtered = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_FILTERED);
119 if (filtered != null)
120 {
121 this.getContext(request).put(FILTERED, "on");
122 }
123
124 String refresh = (String)PortletMessaging.consume(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_REFRESH);
125 if (refresh != null)
126 {
127 this.clearBrowserIterator(request);
128 }
129
130 ArrayList errorMessages = (ArrayList)PortletMessaging.consume(request, SecurityResources.TOPIC_ROLES, SecurityResources.ERROR_MESSAGES);
131 if (errorMessages != null )
132 {
133 this.getContext(request).put(SecurityResources.ERROR_MESSAGES, errorMessages);
134 }
135
136 super.doView(request, response);
137 }
138
139 public void processAction(ActionRequest request, ActionResponse response)
140 throws PortletException, IOException
141 {
142 if (request.getPortletMode() == PortletMode.VIEW)
143 {
144 String selected = request.getParameter("role");
145 if (selected != null)
146 {
147 Role role = lookupRole(request, selected);
148 if (role != null)
149 {
150 PortletMessaging.publish(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_SELECTED, selected);
151 PortletMessaging.publish(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_CHANGED, selected);
152 }
153 }
154 }
155
156 String filtered = request.getParameter(FILTERED);
157 if (filtered != null)
158 {
159 PortletMessaging.publish(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_FILTERED, "on");
160 }
161 else
162 {
163 PortletMessaging.cancel(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_FILTERED);
164 }
165
166 super.processAction(request, response);
167
168 }
169
170 private Role lookupRole(ActionRequest actionRequest, String roleName)
171 {
172 try
173 {
174 return roleManager.getRole(roleName);
175 }
176 catch (SecurityException sex)
177 {
178 SecurityUtil.publishErrorMessage(actionRequest, SecurityResources.TOPIC_ROLES, sex.getMessage());
179 return null;
180 }
181 }
182 }