Skip to content

Commit

Permalink
Merge branch 'registration-resource'
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwechner committed Jan 24, 2012
2 parents f2af59b + e07bb5f commit dbf89dc
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 36 deletions.
6 changes: 3 additions & 3 deletions src/build/dependencies.xml
Expand Up @@ -57,7 +57,7 @@
-->

<!-- IMPORTANT: Please make sure to update pom-core.xml as well! -->
<dependency groupId="wyona-org-security" artifactId="wyona-org-security-core" version="0.3-dev-r27c65fbc0bcfcc24b7678ea00bf4102efc12d6c4"/>
<dependency groupId="wyona-org-security" artifactId="wyona-org-security-core" version="0.3-dev-r7c88414ad21ab207d51de04dac537f173c1f0958"/>

<!-- TODO: Add to yanel.xml, because these libs are only needed during runtime -->
<dependency groupId="avalon-framework" artifactId="avalon-framework-api" version="4.3"/>
Expand All @@ -80,8 +80,8 @@
<!-- IMPORTANT: Please also see src/build/targets/webapp/webapp.xml re ambiguous libs -->
<dependency groupId="xalan" artifactId="xalan" version="2.7.0"/>

<dependency groupId="wyona-org-security" artifactId="wyona-org-security-impl" version="0.3-dev-r27c65fbc0bcfcc24b7678ea00bf4102efc12d6c4"/>
<dependency groupId="wyona-org-security" artifactId="wyona-org-security-util" version="0.3-dev-r27c65fbc0bcfcc24b7678ea00bf4102efc12d6c4"/>
<dependency groupId="wyona-org-security" artifactId="wyona-org-security-impl" version="0.3-dev-r7c88414ad21ab207d51de04dac537f173c1f0958"/>
<dependency groupId="wyona-org-security" artifactId="wyona-org-security-util" version="0.3-dev-r7c88414ad21ab207d51de04dac537f173c1f0958"/>
<!-- IMPORTANT: Please make sure to update pom-core.xml as well! -->
<dependency groupId="yarep" artifactId="yarep-impl" version="1.0-dev-rad19798901b2280b7236e915ae96746ab630fcc1"/>
<dependency groupId="spring" artifactId="spring"
Expand Down
6 changes: 3 additions & 3 deletions src/build/pom-core.xml
Expand Up @@ -10,19 +10,19 @@
<groupId>wyona-org-security</groupId>
<artifactId>wyona-org-security-core</artifactId>
<!-- IMPORTANT: Please make sure to update dependencies.xml as well! -->
<version>0.3-dev-r27c65fbc0bcfcc24b7678ea00bf4102efc12d6c4</version>
<version>0.3-dev-r7c88414ad21ab207d51de04dac537f173c1f0958</version>
</dependency>
<dependency>
<groupId>wyona-org-security</groupId>
<artifactId>wyona-org-security-impl</artifactId>
<!-- IMPORTANT: Please make sure to update dependencies.xml as well! -->
<version>0.3-dev-r27c65fbc0bcfcc24b7678ea00bf4102efc12d6c4</version>
<version>0.3-dev-r7c88414ad21ab207d51de04dac537f173c1f0958</version>
</dependency>
<dependency>
<groupId>wyona-org-security</groupId>
<artifactId>wyona-org-security-util</artifactId>
<!-- IMPORTANT: Please make sure to update dependencies.xml as well! -->
<version>0.3-dev-r27c65fbc0bcfcc24b7678ea00bf4102efc12d6c4</version>
<version>0.3-dev-r7c88414ad21ab207d51de04dac537f173c1f0958</version>
</dependency>
<dependency>
<groupId>yarep</groupId>
Expand Down
3 changes: 2 additions & 1 deletion src/resources/registration/resource.xml
Expand Up @@ -11,6 +11,7 @@ User registration

<rtd>
<!-- Optional -->
<property name="email-confirmation" default-value="true"/>
<property name="email-confirmation" default-value="true"/> <!-- INFO: Set to false if no email confirmation/activation should re required. -->
<property name="groups"/> <!-- INFO: Comma separated list of group IDs, to which user shall be added automatically during registration -->
</rtd>
</resource>
@@ -0,0 +1,53 @@
/*
* Copyright 2012 Wyona
*/
package org.wyona.yanel.resources.registration;

/**
*
*/
public class UserRegistrationBean {

private String firstname;
private String lastname;
private String email;
private String password;

/**
*
*/
public UserRegistrationBean(String firstname, String lastname, String email, String password) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.password = password;
}

/**
*
*/
public String getFirstname() {
return firstname;
}

/**
*
*/
public String getLastname() {
return lastname;
}

/**
*
*/
public String getEmail() {
return email;
}

/**
*
*/
public String getPassword() {
return password;
}
}
Expand Up @@ -28,6 +28,10 @@
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;

/*
import com.konakart.appif.CustomerRegistrationIf;
import com.konakart.appif.KKEngIf;
Expand All @@ -40,7 +44,7 @@ public class UserRegistrationResource extends BasicXMLResource {

private static Logger log = Logger.getLogger(UserRegistrationResource.class);

private static String NAMESPACE = "http://www.wyona.org/yanel/user-registration/1.0";
static String NAMESPACE = "http://www.wyona.org/yanel/user-registration/1.0";

private static String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";

Expand Down Expand Up @@ -76,7 +80,7 @@ protected InputStream getContentXML(String viewId) throws Exception {
if (email != null) {
processRegistrationRequest(doc, email);
} else if (uuid != null) {
if(activateRegistration(uuid)) {
if(activateRegistration(uuid, doc)) {
Element activateSuccessfulE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "activation-successful"));
} else {
Element activationFailedE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "activation-failed"));
Expand Down Expand Up @@ -219,7 +223,7 @@ public boolean exists() {
* Send email containing a confirmation link
*/
private void sendConfirmationLinkEmail(Document doc, String uuid, String firstame, String lastname, String email) {
log.warn("DEBUG: Do not register user right away, but send an email containing a confirmation link...");
log.warn("DEBUG: Do not register user right away, but send an email to '" + email + "' containing a confirmation link...");
Element rootElement = doc.getDocumentElement();

try {
Expand Down Expand Up @@ -247,33 +251,51 @@ private void registerUser(Document doc, String firstname, String lastname, Strin

// INFO: Yanel registration
org.wyona.security.core.api.User user = getRealm().getIdentityManager().getUserManager().createUser("" + customerID, firstname + " " + lastname, email, password);
user.setLanguage(getContentLanguage());
org.wyona.security.core.api.User alias = getRealm().getIdentityManager().getUserManager().createAlias(email, "" + customerID);
// TODO: Move adding to groups into separated method
String groupsCSV = getResourceConfigProperty("groups");
if (groupsCSV != null) {
String[] groupIDs = null;
if (groupsCSV.indexOf(",") >= 0) {
groupIDs = groupsCSV.split(",");
} else {
groupIDs = new String[1];
groupIDs[0] = groupsCSV;
}
for (int i = 0; i < groupIDs.length; i++) {
if (getRealm().getIdentityManager().getGroupManager().existsGroup(groupIDs[i])) {
getRealm().getIdentityManager().getGroupManager().getGroup(groupIDs[i]).addMember(user);
} else {
log.warn("No such group: " + groupIDs[i]);
}
}
}

Element ncE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "new-customer-registered"));
ncE.setAttributeNS(NAMESPACE, "id", "" + customerID);

javax.servlet.http.HttpSession httpSession = getEnvironment().getRequest().getSession(true);

// Login
/*
String konakartSessionID = shared.login(email, password, getRealm(), httpSession);
if (konakartSessionID != null && konakartSessionID.length() > 0) {
httpSession.setAttribute(shared.KONAKART_SESSION_ID, konakartSessionID);
Element succE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "login-successful"));
succE.setAttributeNS(NAMESPACE, "username", "" + email);
// TODO: Copy/paste shopping cart (???)
} else {
Element errE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "login-failed"));
errE.setAttributeNS(NAMESPACE, "username", "" + email);
log.error("Login failed for new user: " + email);
}
// Login
javax.servlet.http.HttpSession httpSession = getEnvironment().getRequest().getSession(true);
String konakartSessionID = shared.login(email, password, getRealm(), httpSession);
if (konakartSessionID != null && konakartSessionID.length() > 0) {
httpSession.setAttribute(shared.KONAKART_SESSION_ID, konakartSessionID);
Element succE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "login-successful"));
succE.setAttributeNS(NAMESPACE, "username", "" + email);
// TODO: Copy/paste shopping cart (???)
} else {
Element errE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "login-failed"));
errE.setAttributeNS(NAMESPACE, "username", "" + email);
log.error("Login failed for new user: " + email);
}
*/

/*
} catch(com.konakart.app.KKUserExistsException e) { // WARN: It seems that KonaKart is using nested exceptions and hence this one is not caught!
log.warn(e.getMessage());
Element fnE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "user-already-exists"));
fnE.appendChild(doc.createTextNode("" + e.getMessage()));
} catch(com.konakart.app.KKUserExistsException e) { // WARN: It seems that KonaKart is using nested exceptions and hence this one is not caught!
log.warn(e.getMessage());
Element fnE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "user-already-exists"));
fnE.appendChild(doc.createTextNode("" + e.getMessage()));
*/
} catch(Exception e) {
log.error(e, e);
Expand All @@ -286,11 +308,11 @@ private void registerUser(Document doc, String firstname, String lastname, Strin
* Save registration request persistently
* @param email E-Mail address of user
*/
private void saveRegistrationRequest(String uuid, String firstname, String lastname, String email, String city, String phone) {
Document doc = getRegistrationRequestAsXML(uuid, firstname, lastname, email, city, phone);
private void saveRegistrationRequest(String uuid, String firstname, String lastname, String email, String city, String phone, String password) {
Document doc = getRegistrationRequestAsXML(uuid, firstname, lastname, email, city, phone, password);
Node node = null;
try {
String path = "/" + uuid;
String path = getActivationNodePath(uuid);
if (!getRealm().getRepository().existsNode(path)) {
node = YarepUtil.addNodes(getRealm().getRepository(), path, NodeType.RESOURCE);
} else {
Expand All @@ -307,14 +329,23 @@ private void saveRegistrationRequest(String uuid, String firstname, String lastn
* Generate registration request as XML
* @param email E-Mail address of user
*/
private Document getRegistrationRequestAsXML(String uuid, String firstname, String lastname, String email, String city, String phone) {
private Document getRegistrationRequestAsXML(String uuid, String firstname, String lastname, String email, String city, String phone, String password) {
Document doc = XMLHelper.createDocument(NAMESPACE, "registration-request");
Element rootElem = doc.getDocumentElement();
rootElem.setAttribute("uuid", uuid);

DateFormat df = new SimpleDateFormat(DATE_FORMAT);
rootElem.setAttribute("request-time", df.format(new Date().getTime()));

// IMPORTANT TODO: Password needs to be encrypted!
Element passwordElem = doc.createElementNS(NAMESPACE, "password");
passwordElem.setTextContent(password);
rootElem.appendChild(passwordElem);

Element lastnameElem = doc.createElementNS(NAMESPACE, "lastname");
lastnameElem.setTextContent(lastname);
rootElem.appendChild(lastnameElem);

Element firstnameElem = doc.createElementNS(NAMESPACE, "firstname");
firstnameElem.setTextContent(firstname);
rootElem.appendChild(firstnameElem);
Expand Down Expand Up @@ -369,6 +400,11 @@ private void processRegistrationRequest(Document doc, String email) throws Excep
Element exception = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "email-not-valid"));
inputsValid = false;
} else {
if (getRealm().getIdentityManager().getUserManager().existsAlias(email)) {
log.warn("E-Mail '" + email + "' is already used as alias!");
Element exception = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "email-in-use"));
inputsValid = false;
}
/*
if(kkEngine.doesCustomerExistForEmail(email)) {
Element exception = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "email-in-use"));
Expand Down Expand Up @@ -512,7 +548,7 @@ private void processRegistrationRequest(Document doc, String email) throws Excep
registerUser(doc, firstname, lastname, email, password);
} else {
String uuid = java.util.UUID.randomUUID().toString();
saveRegistrationRequest(uuid, firstname, lastname, email, city, phone);
saveRegistrationRequest(uuid, firstname, lastname, email, city, phone, password);
sendConfirmationLinkEmail(doc, uuid, firstname, lastname, email);
}
} else {
Expand All @@ -521,19 +557,102 @@ private void processRegistrationRequest(Document doc, String email) throws Excep
}

/**
*
* Try to activate user registration
* @param uuid UUID of user registration activation request
*/
private boolean activateRegistration(String uuid) {
private boolean activateRegistration(String uuid, Document doc) {
try {
String path = "/" + uuid;
String path = getActivationNodePath(uuid);
if (getRealm().getRepository().existsNode(path)) {

UserRegistrationBean urBean = readRegistrationRequest(getRealm().getRepository().getNode(path));

registerUser(doc, urBean.getFirstname(), urBean.getLastname(), urBean.getEmail(), urBean.getPassword());
getRealm().getRepository().getNode(path).delete();

Element rootElement = doc.getDocumentElement();
// TODO: Add gender/salutation
Element emailE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "email"));
emailE.appendChild(doc.createTextNode(urBean.getEmail()));
Element firstnameE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "firstname"));
firstnameE.appendChild(doc.createTextNode(urBean.getFirstname()));
Element lastnameE = (Element) rootElement.appendChild(doc.createElementNS(NAMESPACE, "lastname"));
lastnameE.appendChild(doc.createTextNode(urBean.getLastname()));

return true;
} else {
log.error("No such activation request node: " + path);
return false;
}
} catch(Exception e) {
log.error(e, e);
return false;
}
}

/**
*
*/
private String getActivationNodePath(String uuid) {
return "/user-registration-requests/" + uuid + ".xml";
}

/**
* Read user registration request from repository node
* @param node Repository node containing firstname, lastname, etc.
*/
private UserRegistrationBean readRegistrationRequest(Node node) throws Exception {
Document doc = XMLHelper.readDocument(node.getInputStream());
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new UserRegistrationNamespaceContext());

// TODO: Get creation date to determine expire date!
String firstname = (String) xpath.evaluate("/ur:registration-request/ur:firstname", doc, XPathConstants.STRING);
String lastname = (String) xpath.evaluate("/ur:registration-request/ur:lastname", doc, XPathConstants.STRING);
String email = (String) xpath.evaluate("/ur:registration-request/ur:email", doc, XPathConstants.STRING);
String password = (String) xpath.evaluate("/ur:registration-request/ur:password", doc, XPathConstants.STRING);
UserRegistrationBean urBean = new UserRegistrationBean(firstname, lastname, email, password);
return urBean;
}
}

/**
*
*/
class UserRegistrationNamespaceContext implements javax.xml.namespace.NamespaceContext {

/**
*
*/
public String getNamespaceURI(String prefix) {
if (prefix == null) {
throw new IllegalArgumentException("No prefix provided!");
} else if (prefix.equals("ur")) {
return UserRegistrationResource.NAMESPACE;
} else if (prefix.equals("xhtml")) {
return "http://www.w3.org/1999/xhtml";
} else if (prefix.equals("dc")) {
return "http://purl.org/dc/elements/1.1/";
} else if (prefix.equals("dcterms")) {
return "http://purl.org/dc/terms/";
} else {
return javax.xml.XMLConstants.NULL_NS_URI;
}
}

/**
*
*/
public String getPrefix(String namespaceURI) {
// Not needed in this context.
throw new UnsupportedOperationException();
}

/**
*
*/
public java.util.Iterator getPrefixes(String namespaceURI) {
// Not needed in this context.
throw new UnsupportedOperationException();
}
}

0 comments on commit dbf89dc

Please sign in to comment.