KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > validation > ApproveRegistrationValidator


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64 package com.jcorporate.expresso.services.validation;
65
66 import com.jcorporate.expresso.core.controller.Controller;
67 import com.jcorporate.expresso.core.controller.ControllerException;
68 import com.jcorporate.expresso.core.controller.ControllerRequest;
69 import com.jcorporate.expresso.core.controller.ControllerResponse;
70 import com.jcorporate.expresso.core.controller.NonHandleableException;
71 import com.jcorporate.expresso.core.controller.Transition;
72 import com.jcorporate.expresso.core.db.DBException;
73 import com.jcorporate.expresso.core.i18n.Messages;
74 import com.jcorporate.expresso.core.security.User;
75 import com.jcorporate.expresso.services.dbobj.RegistrationDomain;
76 import com.jcorporate.expresso.services.dbobj.Setup;
77 import org.apache.log4j.Logger;
78
79 import java.util.Hashtable JavaDoc;
80 import java.util.StringTokenizer JavaDoc;
81 import java.util.Vector JavaDoc;
82
83
84 /**
85  * This class is a helper class that allows the LoginController controller to
86  * authorize or deny a registration request
87  *
88  * @author Shash Chatterjee
89  * @version $Revision: 1.19 $ $Date: 2004/11/17 20:48:22 $
90  * @see com.jcorporate.expresso.services.controller.LoginController
91  * @since Expresso 5.0
92  */

93 public class ApproveRegistrationValidator implements ValidationHandler {
94     /**
95      *
96      */

97     protected static Logger log = Logger.getLogger(ApproveRegistrationValidator.class);
98
99     /**
100      * Default constructor Creation date: (9/23/2001 2:23:06 PM) Author: Shash
101      * Chatterjee
102      */

103     public ApproveRegistrationValidator() {
104         super();
105     }
106
107     /**
108      * This method is used to send administrators of a registration domain an
109      * email notifying that a user registered and requires approval. This
110      * method is called by ValidationJob when a new validation request is
111      * submitted. Creation date: (9/23/2001 2:24:30 PM) Author: Shash
112      * Chatterjee
113      *
114      * @param params params All the application-specific parameters from
115      * LoginController
116      * @param URL URL The link that the user will click on to validate the
117      * request
118      * @throws AuthValidationException AuthValidationException
119      * @see com.jcorporate.expresso.services.job.ValidationJob
120      */

121     public void notify(Hashtable JavaDoc params, String JavaDoc URL)
122             throws AuthValidationException {
123         // The db context for the user (Note: this is different from the Validation entry context, which
124
// could very well be in a different DB context)
125
String JavaDoc dbName = (String JavaDoc) params.get("db");
126
127         // The login name of the user
128
String JavaDoc loginName = (String JavaDoc) params.get("UserName");
129         Vector JavaDoc addresses = new Vector JavaDoc(0);
130
131         try {
132             // Create the list of addresses to notify, in this case just the user
133
User myUser = new User();
134             myUser.setDataContext(dbName);
135             myUser.setLoginName(loginName);
136
137             if (!myUser.find()) {
138                 throw new AuthValidationException("User with login \"" +
139                         loginName + "\" not found");
140             }
141
142             RegistrationDomain rd = new RegistrationDomain();
143             rd.setDataContext(dbName);
144             rd.setField("Name", myUser.getRegistrationDomain());
145
146             if (!rd.find()) {
147                 throw new AuthValidationException("Registration domain \"" +
148                         myUser.getRegistrationDomain() + "\" not found");
149             }
150
151             String JavaDoc approvers = rd.getField("Approvers");
152             StringTokenizer JavaDoc stk = new StringTokenizer JavaDoc(approvers, ",");
153
154             while (stk.hasMoreTokens()) {
155                 String JavaDoc approverLoginName = stk.nextToken();
156                 User approver = new User();
157                 approver.setDataContext(dbName);
158                 approver.setLoginName(approverLoginName);
159
160                 if (!approver.find()) {
161                     throw new AuthValidationException("Approver (user) with login name \"" +
162                             approverLoginName + "\" cannot be found");
163                 }
164
165                 addresses.add(approver.getEmail());
166             }
167
168             StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
169             msg.append("User \"" + loginName +
170                     "\" requires approval for domain \"" + rd.getField("Name") +
171                     "\".\n");
172             msg.append("\nPlease approve or deny by clicking on link below:\n");
173             msg.append(URL);
174
175             // Send email notification
176
ValidationEntry.notifyByEmail(dbName,
177                     Setup.getValue(dbName, "MAILFrom"), addresses,
178                     "Registration Approval Requested", msg.toString());
179         } catch (DBException dbe) {
180             throw new AuthValidationException("DB error accessing user \"" +
181                     loginName + "\"", dbe);
182         }
183     }
184
185     /**
186      * This method is used after the approver is validated. This method simply
187      * transitions back to a state of the LoginController This method is
188      * called by ValidationController after a validation code matches that
189      * stored in the validation entry. Creation date: (9/23/2001 2:24:30 PM)
190      * Author: Shash Chatterjee
191      *
192      * @param params params All the application-specific parameters from
193      * LoginController
194      * @param request the ControllerRequest object
195      * @param response response The controller response from the
196      * runValidationState of ValidationController
197      * @param ctlr ctlr The ValidationController instance
198      * @return A ControllerResponse object
199      * @throws ControllerException ControllerException
200      * @see com.jcorporate.expresso.services.controller.ValidationController
201      */

202     public ControllerResponse validated(Hashtable JavaDoc params,
203                                         ControllerRequest request, ControllerResponse response, Controller ctlr)
204             throws ControllerException, NonHandleableException {
205         // The db context for the user (Note: this is different from the Validation entry context, which
206
// could very well be in a different DB context)
207
String JavaDoc dbname = (String JavaDoc) params.get("db");
208
209         // The login name of the user
210
String JavaDoc loginName = (String JavaDoc) params.get("UserName");
211
212         Transition t = new Transition();
213         t.setControllerObject((String JavaDoc) params.get("RegistrationController"));
214         t.setState("promptApproval");
215         t.setName("promptApproval");
216         t.addParam("db", dbname);
217         t.addParam("UserName", loginName);
218
219         //
220
//Here we save the validation entry from the request to the session so
221
//that after the redirect, the Administrator can decide to postpone his
222
//decision and still leave the validation link available.
223
//
224
request.getSession().setPersistentAttribute(ValidationEntry.SESSION_KEY,
225                 request.getSession().getAttribute(ValidationEntry.SESSION_KEY));
226
227         t.redirectTransition(request, response);
228
229         return response;
230     }
231
232     /**
233      * Convenience version of the above with no arguments.
234      *
235      * @param stringCode the string to retrieve
236      * @return The local-language string corresponding to the given code to
237      * return the local language by considering the user as well.
238      */

239     protected String JavaDoc getString(String JavaDoc stringCode) {
240         Object JavaDoc[] args = {};
241
242         return Messages.getString("com.jcorporate.expresso.core.ExpressoSchema",
243                 stringCode, args);
244     }
245     /* getString(String) */
246
247     /**
248      * Pass on a call to retrieve an appropriate localized string from the
249      * correct Schema object. This version of the call is overridden with more
250      * sophisticated versions in DBController (which knows the user id) to
251      * return the local language by considering the user as well.
252      *
253      * @param stringCode the string to retrieve
254      * @param args the formatting arguments
255      * @return java.lang.String
256      */

257     protected String JavaDoc getString(String JavaDoc stringCode, Object JavaDoc[] args) {
258         return Messages.getString("com.jcorporate.expresso.core.ExpressoSchema",
259                 stringCode, args);
260     }
261     /* getString(String, Object[]) */
262 }
263
Popular Tags