001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.core.identity.services.workflow.impl;
023:
024: import java.util.Locale;
025:
026: import org.jboss.logging.Logger;
027: import org.jboss.portal.common.io.IOTools;
028: import org.jboss.portal.core.identity.services.IdentityConstants;
029: import org.jboss.portal.core.identity.services.IdentityUserManagementService;
030: import org.jboss.portal.core.identity.services.metadata.CoreIdentityConfigurationException;
031: import org.jboss.portal.core.identity.services.metadata.IdentityUIConfigurationService;
032: import org.jboss.portal.core.identity.services.workflow.UserContainer;
033: import org.jboss.portal.core.identity.services.workflow.ValidateEmailService;
034: import org.jboss.portal.identity.User;
035: import org.jboss.portal.jems.as.JNDI;
036: import org.jboss.portal.jems.as.system.AbstractJBossService;
037: import org.jboss.portal.workflow.service.WorkflowService;
038: import org.jbpm.JbpmContext;
039: import org.jbpm.graph.exe.ProcessInstance;
040: import org.jbpm.graph.exe.Token;
041:
042: /**
043: * @author <a href="mailto:emuckenh@redhat.com">Emanuel Muckenhuber</a>
044: * @version $Revision$
045: */
046: public class ValidateEmailServiceImpl extends AbstractJBossService
047: implements ValidateEmailService {
048:
049: /** The e-mail validation process name */
050: private String processName = IdentityConstants.jbp_identity_validate_email_process_name;
051:
052: /** The core-identity configuration service */
053: private IdentityUIConfigurationService identityUIConfigurationService;
054:
055: /** The core-identity user management service */
056: private IdentityUserManagementService identityUserManagementService;
057:
058: /** The workflow service */
059: private WorkflowService workflowService = null;
060:
061: /** The logger */
062: private static final Logger log = Logger
063: .getLogger(ValidateEmailService.class);
064:
065: /** The JNDI binding */
066: private JNDI.Binding jndiBinding;
067:
068: /** The jndi name */
069: private String jndiName = null;
070:
071: public void startService() throws Exception {
072: super .startService();
073:
074: if (this .jndiName != null) {
075: jndiBinding = new JNDI.Binding(jndiName, this );
076: jndiBinding.bind();
077: }
078: }
079:
080: public void stopService() throws Exception {
081: super .stopService();
082:
083: if (jndiBinding != null) {
084: jndiBinding.unbind();
085: jndiBinding = null;
086: }
087: }
088:
089: public WorkflowService getWorkflowService()
090: throws CoreIdentityConfigurationException {
091: if (workflowService == null) {
092: workflowService = (WorkflowService) identityUIConfigurationService
093: .getWorkflowService();
094: }
095: return this .workflowService;
096: }
097:
098: public void setWorkflowService(WorkflowService workflowService) {
099: this .workflowService = workflowService;
100: }
101:
102: public String getJNDIName() {
103: return this .jndiName;
104: }
105:
106: public void setJNDIName(String jndiName) {
107: this .jndiName = jndiName;
108: }
109:
110: public IdentityUIConfigurationService getIdentityUIConfigurationService() {
111: return identityUIConfigurationService;
112: }
113:
114: public void setIdentityUIConfigurationService(
115: IdentityUIConfigurationService identityUIConfigurationService) {
116: this .identityUIConfigurationService = identityUIConfigurationService;
117: }
118:
119: public IdentityUserManagementService getIdentityUserManagementService() {
120: return identityUserManagementService;
121: }
122:
123: public void setIdentityUserManagementService(
124: IdentityUserManagementService identityUserManagementService) {
125: this .identityUserManagementService = identityUserManagementService;
126: }
127:
128: public String changeEmail(String url, User user, String email,
129: Locale locale) throws CoreIdentityConfigurationException {
130: if (!this .identityUIConfigurationService.getConfiguration()
131: .enableWorkflow()) {
132: this .getIdentityUserManagementService().updateEmail(
133: user.getUserName(), email);
134: return IdentityConstants.REGISTRATION_REGISTERED;
135: } else {
136: this .changeEmailWorkflow(url, user, email, locale);
137: return IdentityConstants.REGISTRATION_PENDING;
138: }
139: }
140:
141: public void changeEmailWorkflow(String url, User user,
142: String email, Locale locale)
143: throws CoreIdentityConfigurationException {
144: JbpmContext jbpmContext = null;
145: ProcessInstance processInstance = null;
146: // String registrationHash = this.hashGen(); - generated when sending email
147: boolean success = false;
148: try {
149: jbpmContext = this .getWorkflowService()
150: .getJbpmConfiguration().createJbpmContext();
151: processInstance = jbpmContext
152: .newProcessInstance(this .processName);
153: Token token = processInstance.getRootToken();
154:
155: processInstance.getContextInstance().setVariable(
156: IdentityConstants.PORTAL_URL, url);
157: processInstance.getContextInstance().setVariable(
158: IdentityConstants.VARIABLE_USER,
159: new UserContainer(user));
160: processInstance.getContextInstance().setVariable(
161: IdentityConstants.VARIABLE_EMAIL, email);
162: processInstance.getContextInstance().setVariable(
163: IdentityConstants.VARIABLE_LOCALE, locale);
164: processInstance.getContextInstance().setVariable(
165: IdentityConstants.ACTION,
166: IdentityConstants.ACTION_CHANGE_EMAIL);
167:
168: token.signal();
169: success = true;
170: } finally {
171: if (processInstance != null && success) {
172: jbpmContext.save(processInstance);
173: }
174: IOTools.safeClose(jbpmContext);
175: }
176: }
177:
178: public String validateEmail(String id, String registrationHash)
179: throws CoreIdentityConfigurationException {
180: String success = IdentityConstants.VALIDATION_FAILED;
181: if (!this .identityUIConfigurationService.getConfiguration()
182: .enableWorkflow()) {
183: return success;
184: }
185:
186: JbpmContext jbpmContext = null;
187: try {
188: long processId = Long.valueOf(id).longValue();
189: jbpmContext = this .getWorkflowService()
190: .getJbpmConfiguration().createJbpmContext();
191: ProcessInstance processInstance = jbpmContext
192: .getProcessInstance(processId);
193:
194: if (processInstance != null) {
195: Token token = processInstance.getRootToken();
196: if (token != null
197: && token
198: .getNode()
199: .getName()
200: .equals(
201: IdentityConstants.JBPM_NODE_EMAIL_VALIDATION)) {
202: String hash = (String) processInstance
203: .getContextInstance().getVariable(
204: IdentityConstants.VALIDATION_HASH);
205: if (registrationHash.equals(hash)
206: && !processInstance.hasEnded()) {
207: token
208: .signal(IdentityConstants.JBPM_TRANSITION_VALIDATED);
209: success = processInstance
210: .getProcessDefinition().getName();
211: }
212: }
213: }
214: } finally {
215: IOTools.safeClose(jbpmContext);
216: }
217: return success;
218: }
219: }
|