1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.security.users;
18
19 import java.io.IOException;
20 import java.security.Principal;
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Locale;
26
27 import javax.portlet.ActionRequest;
28 import javax.portlet.ActionResponse;
29 import javax.portlet.PortletConfig;
30 import javax.portlet.PortletException;
31 import javax.portlet.PortletSession;
32 import javax.portlet.RenderRequest;
33 import javax.portlet.RenderResponse;
34 import javax.security.auth.Subject;
35
36 import org.apache.jetspeed.CommonPortletServices;
37 import org.apache.jetspeed.portlets.security.SecurityResources;
38 import org.apache.jetspeed.profiler.Profiler;
39 import org.apache.jetspeed.security.Role;
40 import org.apache.jetspeed.security.RoleManager;
41 import org.apache.jetspeed.security.SecurityException;
42 import org.apache.jetspeed.security.User;
43 import org.apache.jetspeed.security.UserManager;
44 import org.apache.jetspeed.security.UserPrincipal;
45 import org.apache.portals.bridges.common.GenericServletPortlet;
46 import org.apache.portals.messaging.PortletMessaging;
47 import org.apache.webapp.admin.TreeControl;
48 import org.apache.webapp.admin.TreeControlNode;
49
50 /***
51 * This portlet is a browser over all the portlet applications in the system.
52 *
53 * @deprecated
54 * @see UserBrowser.java (new implementation)
55 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
56 * @version $Id: UserBrowserPortlet.java 348264 2005-11-22 22:06:45Z taylor $
57 */
58 public class UserBrowserPortlet extends GenericServletPortlet
59 {
60 private UserManager userManager;
61 private RoleManager roleManager;
62 private Profiler profiler;
63
64 /*** the id of the tree control */
65 private static final String TREE_CONTROL = "j2_tree";
66
67 /*** the id of the roles control */
68 private static final String ROLES_CONTROL = "jetspeedRoles";
69
70 /*** the id of the rules control */
71 private static final String RULES_CONTROL = "jetspeedRules";
72
73 /*** query filter for selecting users */
74 private static final String USER_FILTER = "";
75
76 /*** the id of the root node of the tree control */
77 private static final String SECURITY_NODE_ID = "SECURITY-NODE";
78
79 /*** the domain of the security sub-tree */
80 private static final String SECURITY_DOMAIN = "SECURITY_DOMAIN";
81
82 /*** the id of the user node of the tree control */
83 private static final String USER_NODE_ID = "USER-NODE";
84
85 /*** the domain of the user sub-tree */
86 private static final String USER_DOMAIN = "USER_DOMAIN";
87
88 /*** the domain of the users leaf nodes */
89 private static final String USER_DETAIL_DOMAIN = "USER_DETAIL_DOMAIN";
90
91 public void init(PortletConfig config) throws PortletException
92 {
93 super.init(config);
94 userManager = (UserManager) getPortletContext()
95 .getAttribute(CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
96 if (null == userManager)
97 {
98 throw new PortletException("Failed to find the User Manager on portlet initialization");
99 }
100 roleManager = (RoleManager) getPortletContext()
101 .getAttribute(CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
102 if (null == roleManager)
103 {
104 throw new PortletException("Failed to find the Role Manager on portlet initialization");
105 }
106 profiler = (Profiler)getPortletContext().getAttribute(CommonPortletServices.CPS_PROFILER_COMPONENT);
107 if (null == profiler)
108 {
109 throw new PortletException("Failed to find the Profiler on portlet initialization");
110 }
111 }
112
113 public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
114 {
115 response.setContentType("text/html");
116
117 String errorMessage = (String)PortletMessaging.consume(request, "user.error");
118 if (errorMessage != null)
119 {
120 request.setAttribute("errorMessage", errorMessage);
121 }
122
123
124 TreeControl control = null;
125 String refresh = (String)PortletMessaging.consume(request, "users", "refresh");
126 if (refresh == null)
127 {
128 control = (TreeControl) request.getPortletSession().getAttribute(TREE_CONTROL);
129 }
130
131
132 try
133 {
134 if (control == null)
135 {
136 Iterator users = userManager.getUsers(USER_FILTER);
137 control = buildTree(users, request.getLocale());
138 request.getPortletSession().setAttribute(TREE_CONTROL, control);
139 }
140 }
141 catch (SecurityException se)
142 {
143 throw new PortletException(se);
144 }
145 request.setAttribute(TREE_CONTROL, control);
146
147
148 String refreshRoles = (String)PortletMessaging.consume(request, "roles", "refresh");
149 List roles = null;
150 if (refreshRoles == null)
151 {
152 roles = (List) request.getPortletSession().getAttribute(ROLES_CONTROL);
153 }
154
155
156 try
157 {
158 if (roles == null)
159 {
160 roles = new LinkedList();
161 Iterator fullRoles = roleManager.getRoles("");
162 while (fullRoles.hasNext())
163 {
164 Role role = (Role)fullRoles.next();
165 roles.add(role.getPrincipal().getName());
166 }
167 request.getPortletSession().setAttribute(ROLES_CONTROL, roles);
168 }
169 }
170 catch (SecurityException se)
171 {
172 throw new PortletException(se);
173 }
174 request.setAttribute(ROLES_CONTROL, roles);
175
176
177 String refreshProfiles = (String)PortletMessaging.consume(request, "profiles", "refresh");
178 Collection rules = null;
179 if (refreshProfiles == null)
180 {
181 rules = (Collection) request.getPortletSession().getAttribute(RULES_CONTROL);
182 }
183
184
185 if (rules == null)
186 {
187 rules = profiler.getRules();
188 request.getPortletSession().setAttribute(RULES_CONTROL, rules);
189 }
190 request.setAttribute(RULES_CONTROL, rules);
191
192 super.doView(request, response);
193 }
194
195 private boolean isEmpty(String s)
196 {
197 if (s == null) return true;
198
199 if (s.trim().equals("")) return true;
200
201 return false;
202 }
203
204 public void processAction(ActionRequest actionRequest, ActionResponse actionResponse)
205 throws PortletException,
206 IOException
207 {
208 String browserAction = actionRequest.getParameter("browser.action");
209 if (browserAction != null)
210 {
211 String userName = actionRequest.getParameter("jetspeed.user");
212 String password = actionRequest.getParameter("jetspeed.password");
213 if (!isEmpty(userName) && !isEmpty(password))
214 {
215 try
216 {
217 userManager.addUser(userName, password);
218 TreeControl control = (TreeControl) actionRequest.getPortletSession().getAttribute(TREE_CONTROL);
219 Iterator users = userManager.getUsers(USER_FILTER);
220 control = buildTree(users, actionRequest.getLocale());
221 actionRequest.getPortletSession().setAttribute(TREE_CONTROL, control);
222 selectNode(actionRequest, control, userName);
223
224 User user = userManager.getUser(userName);
225 String role = actionRequest.getParameter(ROLES_CONTROL);
226 if (!isEmpty(role) && user != null)
227 {
228 roleManager.addRoleToUser(userName, role);
229 }
230
231 String rule = actionRequest.getParameter(RULES_CONTROL);
232 if (!isEmpty(rule) && user != null)
233 {
234 Principal principal = getPrincipal(user.getSubject(), UserPrincipal.class);
235 profiler.setRuleForPrincipal(principal, profiler.getRule(rule), "page");
236 }
237
238 }
239 catch (SecurityException se)
240 {
241 PortletMessaging.publish(actionRequest, "user.error", se.getMessage());
242 }
243
244 }
245
246
247 return;
248 }
249 TreeControl control = (TreeControl) actionRequest.getPortletSession().getAttribute(TREE_CONTROL);
250
251 if (control != null)
252 {
253
254 String node = actionRequest.getParameter(SecurityResources.REQUEST_NODE);
255 if (node != null)
256 {
257 TreeControlNode controlNode = control.findNode(node);
258 if (controlNode != null)
259 {
260 controlNode.setExpanded(!controlNode.isExpanded());
261 }
262 }
263
264
265 String selectedNode = actionRequest.getParameter(SecurityResources.REQUEST_SELECT_NODE);
266 if (selectedNode != null)
267 {
268 selectNode(actionRequest, control, selectedNode);
269 }
270 }
271 }
272
273 private void selectNode(ActionRequest actionRequest, TreeControl control, String selectedNode)
274 {
275 control.selectNode(selectedNode);
276 TreeControlNode child = control.findNode(selectedNode);
277 if (child != null)
278 {
279 String domain = child.getDomain();
280 if (domain.equals(USER_DETAIL_DOMAIN))
281 {
282 if (selectedNode != null)
283 {
284 actionRequest.getPortletSession().setAttribute(
285 SecurityResources.PAM_CURRENT_USER, selectedNode,
286 PortletSession.APPLICATION_SCOPE);
287 }
288 }
289 }
290 }
291
292 private TreeControl buildTree(Iterator users, Locale locale)
293 {
294
295 TreeControlNode root = new TreeControlNode(SECURITY_NODE_ID,
296 null,
297 getMessage(MSG_SECURITY_ROOT, locale),
298 SecurityResources.PORTLET_URL, null,
299 true,
300 SECURITY_DOMAIN);
301
302 TreeControl control = new TreeControl(root);
303
304 TreeControlNode userTree = new TreeControlNode(USER_NODE_ID,
305 null,
306 getMessage(MSG_USER_ROOT, locale),
307 SecurityResources.PORTLET_URL, null,
308 false,
309 USER_DOMAIN);
310 root.addChild(userTree);
311
312 while (users.hasNext())
313 {
314 User user = (User) users.next();
315 Principal principal = getPrincipal(user.getSubject(), UserPrincipal.class);
316
317 TreeControlNode userNode = new TreeControlNode(principal.getName(), null, principal.getName(),
318 SecurityResources.PORTLET_URL, null, false, USER_DETAIL_DOMAIN);
319 userTree.addChild(userNode);
320 }
321
322 return control;
323 }
324
325 private Principal getPrincipal(Subject subject, Class classe)
326 {
327 Principal principal = null;
328 Iterator principals = subject.getPrincipals().iterator();
329 while (principals.hasNext())
330 {
331 Principal p = (Principal) principals.next();
332 if (classe.isInstance(p))
333 {
334 principal = p;
335 break;
336 }
337 }
338 return principal;
339 }
340
341 /*** Messages */
342 private static final String MSG_SECURITY_ROOT = "tree.security.root";
343
344 private static final String MSG_USER_ROOT = "tree.user.root";
345
346 private String getMessage(String key, Locale locale)
347 {
348 return getResourceBundle(locale).getString(key);
349 }
350
351
352
353 }