001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.taskadmin;
014:
015: import java.util.Collections;
016: import java.util.List;
017: import java.util.Arrays;
018: import java.util.ArrayList;
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.HashMap;
022: import java.util.Set;
023: import java.util.HashSet;
024: import java.util.TreeSet;
025: import java.util.StringTokenizer;
026: import java.util.regex.PatternSyntaxException;
027: import java.util.Locale;
028: import java.util.ResourceBundle;
029:
030: import java.net.URL;
031: import java.net.MalformedURLException;
032: import java.io.*;
033:
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036:
037: import com.sun.portal.taskadmin.TaskAdminException;
038: import com.sun.portal.taskadmin.TaskAdminConstants;
039: import com.sun.portal.taskadmin.context.UserTaskAdminContext;
040: import com.sun.portal.taskadmin.context.UserTaskAdminContextFactoryManager;
041:
042: import com.sun.portal.desktop.context.ContextError;
043:
044: import com.iplanet.am.sdk.AMUser;
045:
046: /**
047: * This class implements the APIs that can be used
048: * to achieve a set of administrative tasks associated
049: * with users.
050: */
051: public class UserTaskAdmin implements TaskAdminConstants {
052: private static final String WILD_CARD = "*";
053: protected UserTaskAdminContext context = null;
054:
055: public UserTaskAdmin(HttpServletRequest req)
056: throws TaskAdminException {
057: try {
058: context = UserTaskAdminContextFactoryManager.getFactory()
059: .getUserTaskAdminContext(req);
060: } catch (ContextError ce) {
061: throw new TaskAdminException(
062: "UserTaskAdmin():Unable to intialize taskadmincontext",
063: ce);
064: }
065: }
066:
067: /**
068: * This method returns the list of roleDNs that
069: * the user in session can assign users to.
070: */
071: public Set getBaseDNs() throws TaskAdminException {
072: try {
073: return context.getBaseDNs();
074: } catch (ContextError ce) {
075: Object[] tokens = { ce.toString() };
076: throw new TaskAdminException(ASSIGNABLE_ROLES_ERROR, tokens);
077: }
078: }
079:
080: /**
081: * This method returns the list of roleDNs that
082: * the user in session can assign users to.
083: */
084: public Set getAssignableRoles() throws TaskAdminException {
085: try {
086: return context.getAssignableRoles();
087: } catch (ContextError ce) {
088: Object[] tokens = { ce.toString() };
089: throw new TaskAdminException(ASSIGNABLE_ROLES_ERROR, tokens);
090: }
091: }
092:
093: public Set getUserRoleDNs(String userDN) throws TaskAdminException {
094: //Checks if valid user DN is passed, otherwise throws TaskAdminException.
095: boolean valid = context.isValidDN(userDN);
096: if (!valid) {
097: Object[] tokens = { userDN, " " };
098: throw new TaskAdminException(INVALID_USER_DN, tokens);
099: }
100: try {
101: return context.getUserRoleDNs(userDN);
102: } catch (ContextError ce) {
103: Object[] tokens = { getDNName(userDN), ce.toString() };
104: throw new TaskAdminException(USER_ROLES_ERROR, tokens);
105: }
106: }
107:
108: public Set getUserDNs() throws TaskAdminException {
109: return searchUsers("*");
110: }
111:
112: public boolean assignRole(String userDN, String roleDN)
113: throws TaskAdminException {
114: //Checks if valid user DN is passed, otherwise throws TaskAdminException.
115: boolean valid = context.isValidDN(userDN);
116: if (!valid) {
117: Object[] tokens = { userDN, " " };
118: throw new TaskAdminException(INVALID_USER_DN, tokens);
119: }
120: //Checks if valid role DN is passed, otherwise throws TaskAdminException.
121: checkRoleDN(roleDN);
122: try {
123: return context.assignRole(userDN, roleDN);
124: } catch (ContextError ce) {
125: Object[] tokens = { getDNName(roleDN), getDNName(userDN),
126: ce.toString() };
127: throw new TaskAdminException(ROLE_ASSIGN_ERROR, tokens);
128: }
129: }
130:
131: public String getDNName(String dn) throws TaskAdminException {
132: try {
133: return context.DNToName(dn);
134: } catch (ContextError ce) {
135: Object[] tokens = { ce.toString() };
136: throw new TaskAdminException(DNNAME_ERROR, tokens);
137: }
138: }
139:
140: public boolean removeRole(String userDN, String roleDN)
141: throws TaskAdminException {
142: //Checks if valid user DN is passed, otherwise throws TaskAdminException.
143: boolean valid = context.isValidDN(userDN);
144: if (!valid) {
145: Object[] tokens = { userDN, " " };
146: throw new TaskAdminException(INVALID_USER_DN, tokens);
147: }
148: //Checks if valid role DN is passed, otherwise throws TaskAdminException.
149: checkRoleDN(roleDN);
150: try {
151: return context.removeRole(userDN, roleDN);
152: } catch (ContextError ce) {
153: Object[] tokens = { getDNName(roleDN), getDNName(userDN),
154: ce.toString() };
155: throw new TaskAdminException(ROLE_REMOVE_ERROR, tokens);
156: }
157: }
158:
159: public ResourceBundle getResourceBundle() {
160: Locale locale = context.getLocale();
161: return ResourceBundle.getBundle(
162: TaskAdminConstants.RESOURCE_BUNDLE_FILE, locale,
163: getClass().getClassLoader());
164: }
165:
166: public boolean setUserStatus(String userDN, boolean activate)
167: throws TaskAdminException {
168: //Checks if valid user DN is passed, otherwise throws TaskAdminException.
169: boolean valid = context.isValidDN(userDN);
170: if (!valid) {
171: Object[] tokens = { userDN, " " };
172: throw new TaskAdminException(INVALID_USER_DN, tokens);
173: }
174: boolean status = !activate;
175: try {
176: status = context.setUserStatus(userDN, activate);
177: } catch (ContextError ce) {
178: Object[] tokens = { getDNName(userDN), ce.toString() };
179: throw new TaskAdminException(USER_STATUS_ERROR, tokens);
180: }
181: return status;
182: }
183:
184: public boolean resetPassword(String userDN, String newpasswd)
185: throws TaskAdminException {
186: //Checks if valid user DN is passed, otherwise throws TaskAdminException.
187: boolean valid = context.isValidDN(userDN);
188: if (!valid) {
189: Object[] tokens = { userDN, " " };
190: throw new TaskAdminException(INVALID_USER_DN, tokens);
191: }
192: boolean status = false;
193: if (newpasswd == null || newpasswd.trim().length() == 0) {
194: Object[] tokens = {};
195: throw new TaskAdminException(NULL_PASSWORD, tokens);
196: }
197: try {
198: status = context.resetPassword(userDN, newpasswd);
199: } catch (ContextError ce) {
200: Object[] tokens = { getDNName(userDN), ce.toString() };
201: throw new TaskAdminException(RESET_PASSWORD_ERROR, tokens);
202: }
203: return status;
204: }
205:
206: public Set searchUsers(String wildcard) throws TaskAdminException {
207: Set users = null;
208: try {
209: Map results = context.searchUsers(wildcard);
210: int errorcode = ((Integer) results.get("errorcode"))
211: .intValue();
212: if (errorcode == 0) {
213: users = (Set) results.get("users");
214: } else if (errorcode == 1) {
215: Object[] tokens = {};
216: throw new TaskAdminException(
217: SEARCHUSERS_TIMELIMIT_ERROR, tokens);
218: } else if (errorcode == 2) {
219: Object[] tokens = {};
220: throw new TaskAdminException(
221: SEARCHUSERS_SIZELIMIT_ERROR, tokens);
222: }
223: } catch (ContextError ce) {
224: Object[] tokens = { ce.toString() };
225: throw new TaskAdminException(GETUSERS_ERROR, tokens);
226: }
227: return users;
228: }
229:
230: public void deleteUsers(Set userDNs) throws TaskAdminException {
231: Iterator iterator = userDNs.iterator();
232: while (iterator.hasNext()) {
233: //Checks if valid user DN is passed, otherwise throws TaskAdminException.
234: String userDN = (String) iterator.next();
235: boolean valid = context.isValidDN(userDN);
236: if (!valid) {
237: Object[] tokens = { userDN, " " };
238: throw new TaskAdminException(INVALID_USER_DN, tokens);
239: }
240: }
241: try {
242: context.deleteUsers(userDNs);
243: } catch (ContextError ce) {
244: Object[] tokens = { ce.toString() };
245: throw new TaskAdminException(DELETEUSERS_ERROR, tokens);
246: }
247:
248: }
249:
250: public void createUser(String uid, String firstname,
251: String lastname, String fullname, String password)
252: throws TaskAdminException {
253: if ((uid == null || uid.trim().length() <= 0)
254: || (lastname == null || lastname.trim().length() <= 0)
255: || (fullname == null || fullname.trim().length() <= 0)
256: || (password == null || password.trim().length() <= 0)) {
257: throw new TaskAdminException(INVALID_USER_INFO,
258: new Object[] {});
259: }
260: try {
261: context.createUser(uid, firstname, lastname, fullname,
262: password);
263: } catch (ContextError ce) {
264: Object[] tokens = { uid, ce.toString() };
265: throw new TaskAdminException(CREATEUSERS_ERROR, tokens);
266: }
267:
268: }
269:
270: public void createRole(String rolename, String description)
271: throws TaskAdminException {
272: if ((rolename == null || rolename.trim().length() <= 0)) {
273: throw new TaskAdminException(INVALID_ROLE_INFO,
274: new Object[] {});
275: }
276: try {
277: context.createRole(rolename, description);
278: } catch (ContextError ce) {
279: Object[] tokens = { rolename, ce.toString() };
280: throw new TaskAdminException(CREATEROLE_ERROR, tokens);
281: }
282:
283: }
284:
285: public void createAdministrativeRole(String rolename,
286: String description, String managedRoleDN,
287: boolean contentAdmin) throws TaskAdminException {
288: if ((rolename == null || rolename.trim().length() <= 0)) {
289: throw new TaskAdminException(INVALID_ROLE_INFO,
290: new Object[] {});
291: }
292: try {
293: context.createAdministrativeRole(rolename, description,
294: managedRoleDN, contentAdmin);
295: } catch (ContextError ce) {
296: Object[] tokens = { rolename, ce.toString() };
297: throw new TaskAdminException(CREATEROLE_ERROR, tokens);
298: }
299:
300: }
301:
302: private void checkRoleDN(String roleDN) throws TaskAdminException {
303: boolean found = false;
304: try {
305: Iterator iterator = context.getBaseDNs().iterator();
306: while (iterator.hasNext()) {
307: if (iterator.next().toString().equalsIgnoreCase(roleDN)) {
308: found = true;
309: break;
310: }
311: }
312: } catch (ContextError e) {
313: Object[] tokens = { roleDN, e.toString() };
314: throw new TaskAdminException(INVALID_ROLE_DN, tokens);
315: }
316: if (!found) {
317: Object[] tokens = { roleDN, " " };
318: throw new TaskAdminException(INVALID_ROLE_DN, tokens);
319: }
320: }
321: }
|