001: /* Copyright 2005 The JA-SIG Collaborative. All rights reserved.
002: * See license distributed with this file and
003: * available online at http://www.uportal.org/license.html
004: */
005: package org.jasig.portal.channels.cusermanager;
006:
007: import java.util.Enumeration;
008: import java.text.MessageFormat;
009:
010: import org.jasig.portal.IChannel;
011:
012: import org.jasig.portal.security.IPerson;
013: import org.jasig.portal.security.IPermission;
014: import org.jasig.portal.security.provider.PersonImpl; //import edu.cornell.uportal.channels.cusermanager.provider.PersonImpl;
015:
016: import org.jasig.portal.ChannelStaticData;
017: import org.jasig.portal.ChannelRuntimeData;
018: import org.jasig.portal.ChannelRuntimeProperties;
019: import org.jasig.portal.PortalEvent;
020: import org.jasig.portal.PortalException;
021: import org.jasig.portal.IPermissible;
022: import org.jasig.portal.AuthorizationException;
023:
024: import org.jasig.portal.utils.XSLT;
025: import org.jasig.portal.utils.DocumentFactory;
026:
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029:
030: import org.xml.sax.ContentHandler;
031:
032: /**
033: * @author smb1@cornell.edu
034: * @author apetro@unicon.net
035: * @version $Revision: 42096 $ $Date: 2007-06-14 12:09:46 -0700 (Thu, 14 Jun 2007) $
036: */
037: public class CUserManager extends CUserManagerPermissions implements
038: IChannel, IPermissible {
039:
040: private IDataHandler dataHandler;
041:
042: private String mode = Constants.MODEDISPLAY;
043:
044: private ChannelStaticData channelStaticData;
045: private ChannelRuntimeData channelRuntimeData;
046:
047: private boolean managerMode = false;
048: private boolean passwordChangeMode = true;
049:
050: private PortalEvent lastEvent;
051:
052: /** for pwd chng mode, we want to cache the user's info because
053: * there will be many more of these than admin channels
054: */
055: private Document personalDocument = null;
056:
057: private ChannelRuntimeDataToPersonConverter channelRuntimeDataToPersonConverter = new ChannelRuntimeDataToPersonConverter();
058:
059: /**
060: */
061: public CUserManager() {
062: }// CUserManager
063:
064: public ChannelRuntimeProperties getRuntimeProperties() {
065: return new ChannelRuntimeProperties();
066: }// getRuntimeProperties
067:
068: public void receiveEvent(PortalEvent ev) {
069:
070: this .lastEvent = ev;
071:
072: }// receiveEvent
073:
074: public void setStaticData(ChannelStaticData sd) {
075: channelStaticData = sd;
076:
077: String dataHandlerClassName = Constants.DEFAULTDATAHANDLER;
078:
079: String dataHandlerClassNameFromChannelStaticData = channelStaticData
080: .getParameter(Constants.CHNPARAMDATAHANDLER);
081:
082: if (dataHandlerClassNameFromChannelStaticData != null) {
083: dataHandlerClassName = dataHandlerClassNameFromChannelStaticData;
084: }
085:
086: try {
087: dataHandler = (IDataHandler) Class.forName(
088: dataHandlerClassName).newInstance();
089: } catch (Exception e) {
090: throw new PortalException(
091: "Unable to instantiate CUserManager IDataHandler implementation ["
092: + dataHandlerClassName + "]", e);
093: }
094:
095: // Ignore since 2.0 (2003.04.21)
096: // if( CSD.getParameter( Constants.CHNPARAMNOTMGR ) != null )
097: // ManagerMode = false;
098:
099: // let determine the user's rights
100: try {
101:
102: IPermission[] perms = channelStaticData
103: .getAuthorizationPrincipal().getAllPermissions(
104: Constants.PERMISSION_OWNERTOKEN, null,
105: Constants.PERMISSION_OWNERTARGET);
106:
107: for (int i = 0; i < perms.length; i++) {
108:
109: if (perms[i].getActivity().equals(
110: Constants.PERMISSION_MNGRRIGHT)
111: && perms[i].getType().equals(
112: IPermission.PERMISSION_TYPE_GRANT))
113: managerMode = true;
114:
115: if (perms[i].getActivity().equals(
116: Constants.PERMISSION_PWDCHNGRIGHT)
117: && perms[i].getType().equals(
118: IPermission.PERMISSION_TYPE_DENY))
119: passwordChangeMode = false;
120:
121: }// for
122: } catch (AuthorizationException ae) {
123: log.error(ae, ae);
124: }
125:
126: }// setStaticData
127:
128: public void setRuntimeData(ChannelRuntimeData rd) {
129: channelRuntimeData = rd;
130: }// setRuntimeData
131:
132: public void renderXML(ContentHandler out) throws PortalException {
133:
134: // first, be sure they are allowed to be here
135: if (!managerMode && !passwordChangeMode)
136: throw new AuthorizationException(MessageFormat
137: .format(Constants.ERRMSG_NORIGHTS,
138: new Object[] { (String) channelStaticData
139: .getPerson().getAttribute(
140: IPerson.USERNAME) }));
141:
142: try {
143:
144: String message_to_user_about_action = ""; // these always start blank
145: Document doc = null;
146: IPerson[] people = null;
147: mode = Constants.MODEDISPLAY;
148:
149: // now, b4 we get going, there may have been an event to deal with
150: if (channelRuntimeData.getParameter(Constants.FORMACTION) == null
151: && lastEvent != null) {
152:
153: if (lastEvent.getEventNumber() == PortalEvent.ABOUT_BUTTON_EVENT)
154: channelRuntimeData.setParameter(
155: Constants.FORMACTION, "10");
156:
157: if (lastEvent.getEventNumber() == PortalEvent.HELP_BUTTON_EVENT)
158: channelRuntimeData.setParameter(
159: Constants.FORMACTION, "11");
160:
161: lastEvent = null; // don't need that anymore
162: }// if, null & !null
163:
164: // see if we have form data to process
165: if (channelRuntimeData.getParameter(Constants.FORMACTION) != null) {
166:
167: log.debug("form.action="
168: + channelRuntimeData
169: .getParameter(Constants.FORMACTION));
170:
171: switch (Integer.parseInt(channelRuntimeData
172: .getParameter(Constants.FORMACTION))) {
173:
174: case 1: { // update
175:
176: this .dataHandler
177: .setUserInformation(channelRuntimeDataToPersonConverter
178: .channelRuntimeDataToPerson(channelRuntimeData));
179: message_to_user_about_action = Constants.MSG_SAVED;
180:
181: break;
182: }// 1
183:
184: case 2: { // choose
185:
186: mode = Constants.MODEDISPLAY;
187: break;
188: }// 2
189:
190: case 3: { // choose
191:
192: people = this .dataHandler.getAllUsers();
193: mode = Constants.MODECHOOSE;
194:
195: break;
196: }// 3
197:
198: case 4: { // search
199:
200: if (channelRuntimeData
201: .getParameter(Constants.FORMSRCHSTR) == null) {
202: people = new PersonImpl[0];
203: mode = Constants.MODESEARCH;
204: } else {
205:
206: // if they did not enter a src str, display mode
207: people = this .dataHandler
208: .getAllUsersLike(channelRuntimeData
209: .getParameter(Constants.FORMSRCHSTR));
210:
211: if (people.length == 1) {
212:
213: // this will cause a second lookup but the user experience will benifit
214: mode = Constants.MODEDISPLAY;
215:
216: channelRuntimeData
217: .setParameter(
218: Constants.UNFIELD,
219: (String) people[0]
220: .getAttribute(Constants.UNFIELD));
221:
222: } else
223: mode = Constants.MODECHOOSE; // they will need to select to narrow
224: }// if
225:
226: break;
227: }// 4
228:
229: case 5: { // prepare add new user
230:
231: channelRuntimeData.setParameter(Constants.UNFIELD,
232: (String) channelStaticData.getPerson()
233: .getAttribute(
234: Constants.ATTRUSERNAME));
235: mode = Constants.MODEADD;
236:
237: break;
238: }// 5
239:
240: case 6: { // add new user
241:
242: try {
243: this .dataHandler
244: .addUser(channelRuntimeDataToPersonConverter
245: .channelRuntimeDataToPerson(channelRuntimeData));
246: } catch (Exception adde) {
247:
248: if (adde.getMessage().indexOf(
249: Constants.ALREADY_EXISTS) > -1)
250: message_to_user_about_action = adde
251: .getMessage();
252: else
253: throw adde;
254:
255: }// catch
256:
257: mode = Constants.MODEDISPLAY;
258:
259: break;
260: }// 6
261:
262: case 7: { // prepare password chng
263:
264: people = new IPerson[] { this .dataHandler
265: .getUser(channelRuntimeData
266: .getParameter(Constants.UNFIELD)) };
267:
268: mode = Constants.MODEPWDCHNG;
269:
270: break;
271: }// 7
272:
273: case 8: { // password chng
274:
275: message_to_user_about_action = Constants.MSG_PWD_SAVED;
276:
277: try {
278: this .dataHandler
279: .setUserPassword(
280: channelRuntimeDataToPersonConverter
281: .channelRuntimeDataToPerson(channelRuntimeData),
282: (managerMode ? null
283: : channelRuntimeData
284: .getParameter(Constants.PWDFIELD)));
285: } catch (Exception pwdchng) {
286:
287: if (pwdchng.getMessage().equals(
288: Constants.ERRMSG_PWDNOTMATACHED))
289: message_to_user_about_action = pwdchng
290: .getMessage();
291: else
292: throw pwdchng;
293:
294: }// catch
295:
296: mode = Constants.MODEDISPLAY;
297:
298: personalDocument = null;
299:
300: break;
301: }// 8
302:
303: case 9: { // delete user
304:
305: this .dataHandler
306: .removeUser(channelRuntimeDataToPersonConverter
307: .channelRuntimeDataToPerson(channelRuntimeData));
308:
309: mode = Constants.MODEDISPLAY;
310:
311: personalDocument = null;
312:
313: break;
314: }// 9
315:
316: case 10: { // about
317:
318: mode = Constants.MODEABOUT;
319:
320: break;
321: }// 10
322:
323: case 11: { // help
324:
325: mode = Constants.MODEHELP;
326:
327: break;
328: }// 11
329:
330: default: {
331: mode = Constants.MODEDISPLAY;
332: channelRuntimeData.remove(Constants.UNFIELD);
333: }// default
334:
335: }// switch
336: }// if
337:
338: if (!managerMode && personalDocument == null
339: && !mode.equals(Constants.MODEABOUT)
340: && !mode.equals(Constants.MODEHELP)) // always override
341: mode = Constants.MODEDISPLAY; // force a read
342:
343: // look up the person we are supposed to display
344: if (mode.equals(Constants.MODEDISPLAY)
345: || mode.equals(Constants.MODEADD))
346: people = new IPerson[] { this .dataHandler
347: .getUser((channelRuntimeData
348: .getParameter(Constants.FORMCHOSEN) == null ? (channelRuntimeData
349: .getParameter(Constants.UNFIELD) == null ? (String) channelStaticData
350: .getPerson().getAttribute(
351: Constants.ATTRUSERNAME)
352: : channelRuntimeData
353: .getParameter(Constants.UNFIELD))
354: : channelRuntimeData
355: .getParameter(Constants.FORMCHOSEN))) };
356:
357: if (!managerMode && !mode.equals(Constants.MODEABOUT)
358: && !mode.equals(Constants.MODEHELP)) // always override
359: mode = Constants.MODEPWDCHNG;
360:
361: if ((managerMode || (!managerMode && personalDocument == null))
362: && !mode.equals(Constants.MODEABOUT)
363: && !mode.equals(Constants.MODEHELP)) {
364: doc = DocumentFactory.getNewDocument();
365:
366: // fill in info about the user
367: Element outtermost = doc.createElement("people");
368: Element person;
369:
370: for (int i = 0; i < people.length; i++) {
371:
372: person = doc.createElement("person");
373: Element attr = null;
374:
375: String worker = null;
376: Enumeration E = people[i].getAttributeNames();
377: while (E.hasMoreElements()) {
378:
379: worker = (String) E.nextElement();
380:
381: attr = doc.createElement(worker);
382: attr.appendChild(doc
383: .createTextNode((String) people[i]
384: .getAttribute(worker)));
385:
386: person.appendChild(attr);
387: }// while
388:
389: outtermost.appendChild(person);
390: }// for
391:
392: doc.appendChild(outtermost);
393: // end - fill in info about the user
394:
395: if (!managerMode)
396: personalDocument = doc;
397:
398: } else {
399:
400: doc = personalDocument;
401: }// if
402:
403: // Create a new XSLT styling engine
404: XSLT xslt = XSLT.getTransformer(this , channelRuntimeData
405: .getLocales());
406: // XSLT xslt = new XSLT( this );
407:
408: // we could have a blank document, help and about
409: if (doc == null) {
410: doc = DocumentFactory.getNewDocument();
411: doc.appendChild(doc.createElement(mode)); // null;
412: }// doc null
413:
414: // pass the result XML to the styling engine.
415: xslt.setXML(doc);
416:
417: // specify the stylesheet selector
418: xslt.setXSL(Constants.SSLFILE, mode, channelRuntimeData
419: .getBrowserInfo());
420:
421: // set parameters that the stylesheet needs.
422: xslt.setStylesheetParameter(Constants.BASEACTION,
423: channelRuntimeData.getBaseActionURL());
424: xslt.setStylesheetParameter(Constants.MODE, mode);
425: xslt.setStylesheetParameter(Constants.DISPLAYMESSAGE,
426: message_to_user_about_action);
427:
428: String MM = (!managerMode ? "yes" : "no");
429: xslt.setStylesheetParameter(Constants.MODEUSRPWDCHNG, MM);
430:
431: /** If I write the above as shown below it does not work. Wasted a .5hr on that! */
432: // xslt.setStylesheetParameter( Constants.MODEUSRPWDCHNG, (!ManagerMode?"yes":"no"));
433: // set the output Handler for the output.
434: xslt.setTarget(out);
435:
436: // do the deed
437: xslt.transform();
438:
439: } catch (Exception e) {
440: log.error(e, e);
441:
442: throw new PortalException((e.getMessage() != null ? e
443: .getMessage() : e.toString()));
444: }// catch
445: }// renderXML
446:
447: }// eoc
|