001: /**
002: * Copyright 2003 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: */package com.sun.portal.wsrp.producer;
013:
014: import java.util.Map;
015: import java.util.HashMap;
016: import java.util.Set;
017: import java.util.Iterator;
018: import java.util.HashSet;
019: import java.util.logging.Level;
020: import java.util.logging.Logger;
021:
022: import com.iplanet.sso.SSOToken;
023: import com.iplanet.sso.SSOException;
024:
025: import com.sun.portal.providers.context.ProviderContext;
026: import com.sun.portal.providers.context.ContainerProviderContext;
027:
028: import com.sun.portal.desktop.context.PSDesktopContextFactoryManager;
029: import com.sun.portal.desktop.context.DesktopContextFactory;
030: import com.sun.portal.desktop.context.DesktopContext;
031: import com.sun.portal.desktop.context.DSAMEMultiPortalConstants;
032:
033: import javax.servlet.ServletContext;
034: import javax.servlet.ServletRequest;
035: import javax.servlet.http.HttpServletRequest;
036:
037: import com.sun.portal.wsrp.producer.context.WSRPSessionContext;
038:
039: import com.sun.portal.wsrp.producer.ISConnection;
040: import com.sun.portal.wsrp.producer.ProducerException;
041: import com.sun.portal.wsrp.producer.ProducerRegistryManager;
042: import com.sun.portal.wsrp.producer.ProducerDN;
043:
044: import com.sun.portal.wsrp.common.WSRPSpecKeys;
045: import com.sun.portal.wsrp.common.Timer;
046:
047: import com.sun.portal.desktop.util.Base64;
048:
049: import com.sun.portal.desktop.context.DSAMEConstants;
050: import com.sun.portal.log.common.PortalLogger;
051:
052: //
053: // TBD: this component must clean up and remove
054: // non-used, stale, or timed out PC objects.
055: // how this will be done is unclear at this point,
056: // and needs to be researched.
057: //
058:
059: //
060: // this component will assume that the producer org
061: // and instance OUs are created externally (admin console).
062: //
063:
064: public class UserManager {
065: private static final String WSRP_GUEST_USER = WSRPSpecKeys.WSRP_GUEST_KEY;
066:
067: //
068: // these are derived from the producerKey and are used to build the
069: // correct user DN
070: //
071: private String organization = null;
072: private String instance = null;
073:
074: //
075: // the registration handle identifies a particular consumer registration
076: // within a producer instance. we need this to build the user DN
077: // since users will live in an ou named as the registration handle
078: //
079: private String registrationHandle = null;
080:
081: private ServletContext servletContext = null;
082: private HttpServletRequest req = null;
083: private SSOToken token = null;
084: private ISConnection isConnection = null;
085: private static Logger logger = PortalLogger
086: .getLogger(UserManager.class);
087:
088: public UserManager(ServletContext servletContext,
089: HttpServletRequest req, SSOToken token, String producerKey)
090: throws ProducerException {
091: this (servletContext, req, token, producerKey, null);
092: }
093:
094: public UserManager(ServletContext servletContext,
095: HttpServletRequest req, SSOToken token, String producerKey,
096: String registrationHandle) throws ProducerException {
097: this .req = req;
098: this .token = token;
099:
100: ProducerRegistryManager reg = ProducerRegistryManager
101: .getRegistryManager();
102: isConnection = new ISConnection(token);
103:
104: this .organization = reg.getOrganization(producerKey);
105: this .instance = reg.getInstance(producerKey);
106:
107: this .registrationHandle = registrationHandle;
108: this .servletContext = servletContext;
109: }
110:
111: public void releaseProviderContexts(String[] sessionIDs) {
112: Timer t = new Timer();
113:
114: Set encodedSessionIDs = new HashSet();
115: for (int i = 0; i < sessionIDs.length; i++) {
116: String uid = Base64.encode(sessionIDs[i]);
117: String userDN = ProducerDN.getUserDN(organization,
118: instance, registrationHandle, uid, null);
119: encodedSessionIDs.add(userDN);
120: }
121:
122: DesktopContextFactory dcf = PSDesktopContextFactoryManager
123: .getFactory(servletContext);
124: dcf.releaseDesktopContexts(encodedSessionIDs);
125:
126: }
127:
128: private String getUID(String userContextKey) {
129: String uid = Base64.encode(userContextKey);
130: return uid;
131: }
132:
133: public ProviderContext getProviderContext(String userContextKey,
134: int expires) throws ProducerException {
135: return getProviderContext(userContextKey, expires, true);
136: }
137:
138: public ProviderContext getProviderContext(SSOToken ssoToken,
139: int expires) throws ProducerException {
140: return getProviderContext(ssoToken, expires, true);
141: }
142:
143: private ProviderContext getProviderContext(String userContextKey,
144: int expires, boolean create) throws ProducerException {
145: Timer t = new Timer();
146:
147: DesktopContext dc = getDesktopContext(userContextKey, expires,
148: create);
149: dc.refresh();
150: ContainerProviderContext cpc = dc.getContainerProviderContext();
151:
152: return (ProviderContext) cpc;
153: }
154:
155: private ProviderContext getProviderContext(SSOToken ssoToken,
156: int expires, boolean create) throws ProducerException {
157: Timer t = new Timer();
158:
159: DesktopContext dc = getDesktopContext(ssoToken, expires, create);
160: dc.refresh();
161: ContainerProviderContext cpc = dc.getContainerProviderContext();
162:
163: return (ProviderContext) cpc;
164: }
165:
166: public DesktopContext getDesktopContext(String userContextKey,
167: int expires) throws ProducerException {
168: return getDesktopContext(userContextKey, expires, true);
169: }
170:
171: private DesktopContext getDesktopContext(String userContextKey,
172: int expires, boolean create) throws ProducerException {
173: DesktopContextFactory dcf = PSDesktopContextFactoryManager
174: .getFactory(servletContext);
175: DesktopContext dc = null;
176:
177: String userDN = getUserDN(userContextKey, create);
178: assignToWSRPRole(userDN);
179: req.setAttribute(WSRPSessionContext.WSRP_UID_KEY, userDN);
180: dc = dcf.getDesktopContext(req, expires, create);
181: dc.refresh();
182: return dc;
183: }
184:
185: public DesktopContext getDesktopContext(SSOToken ssoToken,
186: int expires) throws ProducerException {
187: return getDesktopContext(ssoToken, expires, true);
188: }
189:
190: private DesktopContext getDesktopContext(SSOToken ssoToken,
191: int expires, boolean create) throws ProducerException {
192: try {
193: DesktopContextFactory dcf = PSDesktopContextFactoryManager
194: .getFactory(servletContext);
195: DesktopContext dc = null;
196: String userDN = ssoToken.getPrincipal().getName();
197: assignToWSRPRole(userDN);
198: req.setAttribute(WSRPSessionContext.WSRP_UID_KEY, userDN);
199: req
200: .setAttribute(WSRPSessionContext.WSRP_SSOTOKEN,
201: ssoToken);
202: dc = dcf.getDesktopContext(req, expires, create);
203: dc.refresh();
204: return dc;
205: } catch (SSOException se) {
206: throw new ProducerException(se.getMessage());
207: }
208:
209: }
210:
211: private String getUserDN(String userContextKey, boolean create)
212: throws ProducerException {
213:
214: String userDN = null;
215: //
216: // uck or reg handle is null, or the uck
217: // is the well-known wsrp "anonymous"
218: // string.
219: //
220: // target the well-defined "default"
221: // user for this producer
222: //
223: if (userContextKey == null) {
224: userDN = ProducerDN.getDefaultUserDN(organization,
225: instance, null);
226: if (logger.isLoggable(Level.WARNING))
227: logger.log(Level.WARNING, "PSWS_CSPWSP0003");
228: } else if (registrationHandle == null) {
229: userDN = ProducerDN.getDefaultUserDN(organization,
230: instance, null);
231: if (logger.isLoggable(Level.WARNING))
232: logger.log(Level.WARNING, "PSWS_CSPWSP0004");
233: } else if (userContextKey.equals(WSRP_GUEST_USER)) {
234: userDN = ProducerDN.getDefaultUserDN(organization,
235: instance, null);
236: if (logger.isLoggable(Level.WARNING))
237: logger.log(Level.WARNING, "PSWS_CSPWSP0005");
238: } else {
239: //
240: // we have a uck and a reg handle, find the
241: // associated user entry and get a provider
242: // context for it
243: //
244: String uid = getUID(userContextKey);
245: if (logger.isLoggable(Level.FINEST)) {
246: String[] param = { "uid", uid };
247: logger.log(Level.FINEST, "PSWS_CSPWSP0006", param);
248: }
249:
250: userDN = ProducerDN.getUserDN(organization, instance,
251: registrationHandle, uid, null);
252: if (!isConnection.isValidEntry(userDN) && create) {
253: synchronized (this .getClass()) {
254: if (!isConnection.isValidEntry(userDN)) {
255: String consumerPeopleDN = ProducerDN
256: .getConsumerPeopleDN(organization,
257: instance, registrationHandle,
258: null);
259:
260: if (logger.isLoggable(Level.FINEST)) {
261: String[] param = { "consumerPeopleDN",
262: consumerPeopleDN, uid };
263: logger.log(Level.FINEST, "PSWS_CSPWSP0007",
264: param);
265: }
266:
267: Map attrs = new HashMap();
268: Set passwordVal = new HashSet();
269: // add user's password equal to the user dn
270: passwordVal.add(userDN);
271: attrs.put("userPassword", passwordVal);
272:
273: Set objectClasses = new HashSet();
274: // add desktop object class
275: DSAMEMultiPortalConstants dmc = DSAMEMultiPortalConstants
276: .getInstance();
277: objectClasses
278: .add(dmc.MP_SUN_DESKTOP_OBJECT_CLASS);
279:
280: isConnection.createUser(consumerPeopleDN, uid,
281: objectClasses, attrs, instance);
282:
283: }
284: }
285: }
286: }
287:
288: if (logger.isLoggable(Level.FINEST)) {
289: String[] param = { "userDN", userDN };
290: logger.log(Level.FINEST, "PSWS_CSPWSP0006", param);
291: }
292:
293: return userDN;
294: }
295:
296: public void store(String userContextKey) throws ProducerException {
297: DesktopContext dc = getDesktopContext(userContextKey, -1, false);
298: if (dc != null) {
299: dc.store();
300: }
301: }
302:
303: public void store(SSOToken token) throws ProducerException {
304: DesktopContext dc = getDesktopContext(token, -1, false);
305: if (dc != null) {
306: dc.store();
307: }
308: }
309:
310: /*
311: * Does on-the-fly migration of WSRP user from portal 6.x to 7.0. _WSRPRole_
312: * is added in 7.0 to share cloned channel information to all the users under
313: * that consumer registration
314: */
315: private void assignToWSRPRole(String userDN)
316: throws ProducerException {
317: String roleDN = getRoleDN();
318: if (!checkRoleExists(roleDN)) {
319: createRole();
320: }
321: if (!checkUserInRole(userDN, roleDN)) {
322: addUserToRole(userDN, roleDN);
323: }
324: }
325:
326: private boolean checkRoleExists(String roleDN)
327: throws ProducerException {
328: return isConnection.isValidEntry(roleDN);
329: }
330:
331: private boolean checkUserInRole(String userDN, String roleDN)
332: throws ProducerException {
333: return isConnection.checkUserInRole(userDN, roleDN);
334: }
335:
336: private void addUserToRole(String userDN, String roleDN)
337: throws ProducerException {
338: isConnection.addUserToWSRPRole(token, userDN, roleDN);
339: }
340:
341: private void createRole() throws ProducerException {
342: isConnection.createRole(organization, registrationHandle,
343: instance, null, null);
344: }
345:
346: private String getRoleDN() {
347: return ProducerDN.getProducersRoleDN(organization, instance,
348: registrationHandle, null);
349: }
350: }
|