View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.portlets.security.users;
18  
19  import java.security.Principal;
20  import java.util.Iterator;
21  
22  import javax.faces.context.FacesContext;
23  import javax.portlet.PortletConfig;
24  import javax.portlet.PortletException;
25  import javax.security.auth.Subject;
26  
27  import org.apache.jetspeed.CommonPortletServices;
28  import org.apache.jetspeed.security.SecurityException;
29  import org.apache.jetspeed.security.User;
30  import org.apache.jetspeed.security.UserManager;
31  import org.apache.jetspeed.security.UserPrincipal;
32  import org.apache.portals.bridges.jsf.FacesPortlet;
33  
34  /***
35   * Provides maintenance capabilities for User Administration.
36   * 
37   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
38   * @version $Id: UserManagerPortlet.java 348264 2005-11-22 22:06:45Z taylor $
39   */
40  public class UserManagerPortlet extends FacesPortlet {
41  	private UserManager userManager;
42  
43  	public void init(PortletConfig config) throws PortletException {
44  		super.init(config);
45  		userManager = (UserManager) getPortletContext().getAttribute(
46                  CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
47  		if (null == userManager) {
48  			throw new PortletException(
49  					"Failed to find the User Manager on portlet initialization");
50  		}
51  //		System.out.println("user manager = " + userManager);
52  		try {
53  			Iterator users = userManager.getUsers("");
54  			while (users.hasNext()) {
55  				User user = (User) users.next();
56  //				System.out.println("++++ User = " + user);
57  				getPrincipal(user.getSubject(), UserPrincipal.class);
58  //				System.out.println("principal = " + principal.getName());
59  			}
60  		} catch (SecurityException se) {
61  			throw new PortletException(se);
62  		}
63  	}
64  
65  	protected void preProcessFaces(FacesContext context) {
66  //		System.out.println("*** pre processing faces for user manager: " + context);
67  	}
68  
69  	public Principal getPrincipal(Subject subject, Class classe) {
70  		Principal principal = null;
71  		Iterator principals = subject.getPrincipals().iterator();
72  		while (principals.hasNext()) {
73  			Principal p = (Principal) principals.next();
74  			if (classe.isInstance(p)) {
75  				principal = p;
76  				break;
77  			}
78  		}
79  		return principal;
80  	}
81  
82  }