From: Sasha Sherstnev Date: Fri, 6 Dec 2013 17:39:14 +0000 (+0000) Subject: Partly working security: registration form, authorisaztion, simple authentication X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=95f270e6b3ec72bd29b5ac6e09f803dbaa64380e;p=proteocache.git Partly working security: registration form, authorisaztion, simple authentication --- diff --git a/WEB-INF/lib/aopalliance-1.0.jar b/WEB-INF/lib/aopalliance-1.0.jar new file mode 100644 index 0000000..578b1a0 Binary files /dev/null and b/WEB-INF/lib/aopalliance-1.0.jar differ diff --git a/datadb/compbio/cassandra/CassandraUserManager.java b/datadb/compbio/cassandra/CassandraUserManager.java new file mode 100644 index 0000000..4bcd4b9 --- /dev/null +++ b/datadb/compbio/cassandra/CassandraUserManager.java @@ -0,0 +1,238 @@ +package compbio.cassandra; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.log4j.Logger; +import org.springframework.dao.DataIntegrityViolationException; + +import com.datastax.driver.core.Row; +import com.datastax.driver.core.Session; +import com.datastax.driver.core.ResultSet; +import com.datastax.driver.core.exceptions.QueryExecutionException; +import com.datastax.driver.core.exceptions.QueryValidationException; + +import compbio.proteocache.users.User; +import compbio.proteocache.users.UserManager; + +public class CassandraUserManager implements UserManager { + private Session session; + private static Logger log = Logger.getLogger(CassandraNativeConnector.class); + + public CassandraUserManager() { + Session inis = CassandraNativeConnector.getSession(); + setSession(inis); + } + + private void setSession(Session s) { + assert s != null; + session = s; + } + + public boolean addUser(User user) throws DataIntegrityViolationException { + String chkcom = "SELECT * FROM Users WHERE email = '" + user.getEmail() + "';"; + try { + ResultSet users = session.execute(chkcom); + if (0 < users.all().size()) { + throw new DataIntegrityViolationException("A user with email " + user.getEmail() + " exists"); + } + + } catch (QueryExecutionException e) { + String mess = "CassandraUserManagerImpl.addUser: query execution exception..."; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return false; + } catch (QueryValidationException e) { + String mess = "CassandraUserManagerImpl.addUser: query validation exception... Command: " + chkcom; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return false; + } + + long id = user.getId().longValue(); + if (id < 1) { + String com = "SELECT * FROM MainParameters WHERE Name = 'MaxUserId';"; + try { + ResultSet values = session.execute(com); + List list = values.all(); + /* + if (1 != list.size()) { + return false; + } + */ + String test = list.get(0).getString("Value"); + id = Long.parseLong(test, 10); + id++; + com = "INSERT INTO MainParameters (name,value) VALUES ('MaxUserId','" + id + "');"; + session.execute(com); + } catch (QueryExecutionException e) { + String mess = "CassandraUserManagerImpl.addUser: query execution exception..."; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return false; + } catch (QueryValidationException e) { + String mess = "CassandraUserManagerImpl.addUser: query validation exception... Command: " + com; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return false; + } + } + + String incom = "INSERT INTO Users (name, id, email, password, organisation, position, signedtolist, registrationdate) VALUES ('" + + user.getFullName() + "'," + id + ",'" + user.getEmail() + "','" + user.getPassword() + "','" + + user.getOrganisation() + "','" + user.getPosition() + "'," + user.isUpdateByEmail() + "," + + user.getRegistrationDate().getTime() + ");"; + try { + session.execute(incom); + } catch (QueryExecutionException e) { + String mess = "CassandraUserManagerImpl.addUser: query execution exception..."; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return false; + } catch (QueryValidationException e) { + String mess = "CassandraUserManagerImpl.addUser: query validation exception... Command: " + incom; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return false; + } + return true; + } + + public boolean saveUser(User user) { + boolean isSaved = false; + String com = "SELECT * FROM Users WHERE id = " + user.getId() + ";"; + try { + ResultSet users = session.execute(com); + if (1 < users.all().size()) { + return false; + } + isSaved = addUser(user); + } catch (QueryExecutionException e) { + String mess = "CassandraUserManagerImpl.saveUser: query execution exception..."; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + } catch (QueryValidationException e) { + String mess = "CassandraUserManagerImpl.saveUser: query validation exception... Command: " + com; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + } + return isSaved; + } + + public boolean deleteUser(long id) { + String com = "DELETE FROM Users WHERE id = " + id + ";"; + try { + session.execute(com); + } catch (QueryExecutionException e) { + String mess = "CassandraUserManagerImpl.deleteUser: query execution exception..."; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return false; + } catch (QueryValidationException e) { + String mess = "CassandraUserManagerImpl.deleteUser: query validation exception... Command: " + com; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return false; + } + return true; + } + + public User getUserById(long id) { + String com = "SELECT * FROM Users WHERE Id = " + id + ";"; + User user = new User(); + try { + ResultSet users = session.execute(com); + if (1 != users.all().size()) { + return null; + } + user = buildUser(users.one()); + } catch (QueryExecutionException e) { + String mess = "CassandraUserManagerImpl.addUser: query execution exception..."; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return null; + } catch (QueryValidationException e) { + String mess = "CassandraUserManagerImpl.addUser: query validation exception... Command: " + com; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return null; + } + return user; + } + + public User getUserByEmail(String email) { + String com = "SELECT * FROM Users WHERE Email = '" + email + "';"; + User user = new User(); + try { + ResultSet users = session.execute(com); + if (1 != users.all().size()) { + return null; + } + user = buildUser(users.one()); + } catch (QueryExecutionException e) { + String mess = "CassandraUserManagerImpl.getUserByEmail: query execution exception..."; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return null; + } catch (QueryValidationException e) { + String mess = "CassandraUserManagerImpl.getUserByEmail: query validation exception... Command: " + com; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return null; + } + return user; + } + + public List findAllUsers() { + List list = new ArrayList(); + String com = "SELECT * FROM Users;"; + try { + ResultSet results = session.execute(com); + List rows = results.all(); + for (Row r : rows) { + list.add(buildUser(r)); + } + } catch (QueryExecutionException e) { + String mess = "CassandraUserManagerImpl.findAllUsers: query execution exception..."; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return null; + } catch (QueryValidationException e) { + String mess = "CassandraUserManagerImpl.findAllUsers: query validation exception... Command: " + com; + System.out.println(mess); + log.error(mess); + log.error(e.getLocalizedMessage(), e.getCause()); + return null; + } + return list; + } + + private User buildUser(Row r) { + User u = new User(); + u.setFullName(r.getString("Name")); + u.setId(new Long(r.getLong("Id"))); + u.setEmail(r.getString("Email")); + u.setPassword(r.getString("Password")); + u.setOrganisation(r.getString("Organisation")); + u.setPosition(r.getString("Position")); + u.setUpdateByEmail(r.getBool("SignedToList")); + u.setRegistrationDate(new Date(r.getLong("RegistrationDate"))); + return u; + } +} diff --git a/engine/compbio/engine/JobStatus.java b/engine/compbio/engine/JobStatus.java new file mode 100644 index 0000000..393f401 --- /dev/null +++ b/engine/compbio/engine/JobStatus.java @@ -0,0 +1,40 @@ +package compbio.engine; + +import java.util.Set; + +/** + * List of all posible final job statuses + * + */ +public enum JobStatus { + OK, TIMEDOUT, STOPPED, JPREDERROR; + + public static JobStatus getJobStatus(String status) { + status = status.trim().toLowerCase(); + for (JobStatus st : JobStatus.values()) { + if (st.toString().equalsIgnoreCase(status)) { + return st; + } + } + return null; + } + + public static String toString(Set statuses) { + if (statuses == null || statuses.isEmpty()) { + return "No known services...\n"; + } + String value = ""; + for (JobStatus st : statuses) { + if (null != st) { + value += st + "\n"; + } else { + value += "Unknown Job Status\n"; + } + } + return value; + } + + public static void main(String[] args) { + System.out.println(OK); + } +} diff --git a/engine/compbio/proteocache/users/User.java b/engine/compbio/proteocache/users/User.java new file mode 100644 index 0000000..5e300d6 --- /dev/null +++ b/engine/compbio/proteocache/users/User.java @@ -0,0 +1,120 @@ +package compbio.proteocache.users; + +import java.io.Serializable; +import java.util.Date; + +import org.springframework.format.annotation.DateTimeFormat; + +import static org.apache.commons.lang.builder.EqualsBuilder.*; +import static org.apache.commons.lang.builder.HashCodeBuilder.*; +import static org.apache.commons.lang.builder.ToStringBuilder.*; + +public class User implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 1L; + + private Long id = -1L; + private String email; + private String password; + private String fullName; + private boolean updateByEmail; + private String position; + private String organisation; + + @DateTimeFormat(pattern = "hh:mma MMM d, YYYY") + private Date registrationDate; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getEmail() { + return email; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + + public void setFullName(String fullName) { + this.fullName = fullName; + } + + public String getFullName() { + return fullName; + } + + public String getPosition() { + return position; + } + + public void setPosition(String position) { + this.position = position; + } + + public void setOrganisation(String organisation) { + this.organisation = organisation; + } + + private String checkNoUTFsymbols(String s) { + if (null != s) { + if (3 < s.length()) + if (s.substring(0, 2).matches("^&#")) { + return "Unknown organization"; + } + } + return s; + } + + public String getOrganisation() { + return checkNoUTFsymbols(organisation); + } + + public void setUpdateByEmail(boolean updateByEmail) { + this.updateByEmail = updateByEmail; + } + + public boolean isUpdateByEmail() { + return updateByEmail; + } + + public Date getRegistrationDate() { + return this.registrationDate; + } + + public void setRegistrationDate(Date registrationDate) { + this.registrationDate = registrationDate; + } + + // plumbing + @Override + public boolean equals(Object obj) { + return reflectionEquals(this, obj); + } + + @Override + public int hashCode() { + return reflectionHashCode(this); + } + + @Override + public String toString() { + return reflectionToString(this); + } + +} diff --git a/engine/compbio/proteocache/users/UserManager.java b/engine/compbio/proteocache/users/UserManager.java new file mode 100644 index 0000000..48f3fb3 --- /dev/null +++ b/engine/compbio/proteocache/users/UserManager.java @@ -0,0 +1,21 @@ +package compbio.proteocache.users; + +import java.util.List; + +import compbio.proteocache.users.User; + +public interface UserManager { + + public boolean addUser(User user); + + public boolean saveUser(User user); + + public boolean deleteUser(long id); + + public User getUserById(long id); + + public User getUserByEmail(String email); + + public List findAllUsers(); + +} diff --git a/server/compbio/controllers/UserController.java b/server/compbio/controllers/UserController.java new file mode 100644 index 0000000..94be377 --- /dev/null +++ b/server/compbio/controllers/UserController.java @@ -0,0 +1,123 @@ +package compbio.controllers; + +import java.security.Principal; +import java.util.Date; +import java.util.regex.Pattern; + +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.mail.SimpleMailMessage; +import org.springframework.mail.javamail.JavaMailSender; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; + +import compbio.proteocache.users.User; +import compbio.proteocache.users.UserManager; +import compbio.cassandra.CassandraUserManager; + +@Controller +public class UserController { + + //@Inject + //JavaMailSender mailSender; + private final Pattern EMAIL = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"); + + @RequestMapping(value = "/register/query", method = RequestMethod.GET) + public String printPublicHome(ModelMap model) { + User user = new User(); + model.addAttribute(user); + return "Register"; + } + + @RequestMapping(value = "/register/do", method = RequestMethod.POST) + public String addUser(Model model, @ModelAttribute("user") User user, BindingResult bindingResult) { + + if (bindingResult.hasErrors()) { + return "Register"; + } + + int fullName = user.getFullName().length(); + if (fullName < 6 || 50 < fullName) { + bindingResult.addError(new FieldError("user", "fullName", "Your full name must be between 3 and 50 symbols long!")); + model.addAttribute("error", "wrong password"); + return "Register"; + } + + if (!EMAIL.matcher(user.getEmail()).find()) { + bindingResult.addError(new FieldError("user", "email", "Email is empty or in a wrong form!")); + model.addAttribute("error", "wrong email"); + return "Register"; + } + + int password = user.getPassword().length(); + if (password < 6 || 20 < password) { + bindingResult.addError(new FieldError("user", "password", "The password must be at least 6 symbols long!")); + model.addAttribute("error", "wrong password"); + return "Register"; + } + + int organisation = 0; + if (null != user.getOrganisation()) + organisation = user.getOrganisation().length(); + if (organisation < 3 || 250 < organisation) { + bindingResult.addError(new FieldError("user", "organisation", "The organisation must be between 3 and 250 symbols long!")); + model.addAttribute("error", "wrong organisation name"); + return "Register"; + } + + user.setRegistrationDate(new Date()); + UserManager cm = new CassandraUserManager(); + try { + cm.addUser(user); + } catch (DataIntegrityViolationException e) { + bindingResult.addError(new FieldError("user", "email", "This email (username) is already in use!")); + model.addAttribute("error", "used email"); + return "Register"; + } + if (user.isUpdateByEmail()) { + subscribeToList(user.getEmail()); + } + /* + Account.autoLogin(user, request, authenticationManager); + */ + return "redirect:/index"; + } + + @RequestMapping(value = "/register/edit/do", method = RequestMethod.POST) + public String editUser(Model model, @ModelAttribute("user") User user, BindingResult bindingResult) { + + if (bindingResult.hasErrors()) { + return "Register"; + } + + user.setRegistrationDate(new Date()); + UserManager cm = new CassandraUserManager(); + try { + cm.addUser(user); + } catch (DataIntegrityViolationException e) { + bindingResult.addError(new FieldError("user", "email", "This email (username) is already in use!")); + return "Register"; + } + if (user.isUpdateByEmail()) { + subscribeToList(user.getEmail()); + } + + return "redirect:/index"; + } + + private void subscribeToList(String email) { + SimpleMailMessage message = new SimpleMailMessage(); + message.setFrom(email); + message.setTo("proteocache-discuss-subscribe@compbio.dundee.ac.uk"); + message.setSubject("ProteoCache mailing list subscription"); + message.setText("testing " + email); + //mailSender.send(message); + } + +} diff --git a/webapp/view/Register.jsp b/webapp/view/Register.jsp new file mode 100644 index 0000000..77584c2 --- /dev/null +++ b/webapp/view/Register.jsp @@ -0,0 +1,128 @@ + + +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + + +
+ + + +
+
+
Create a new user account
+
+
+ + +
+ + +
+ +

+ +

+ +

+ +

+ +

+ + Subscribe to ProteoCache mailing list +

+ +
+
+ +
+ + +

+ + +

+ + +

+ + +

+ + +

+ + Subscribe to ProteoCache mailing list +

+ +
+
+
+
+
+ + + +
+
+ +
+ + \ No newline at end of file diff --git a/webapp/view/fragments/mainmenu_and_figures.jsp b/webapp/view/fragments/mainmenu_and_figures.jsp deleted file mode 100644 index b5d8e56..0000000 --- a/webapp/view/fragments/mainmenu_and_figures.jsp +++ /dev/null @@ -1,45 +0,0 @@ -<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> -<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> - - - - diff --git a/webapp/view/fragments/publicmenu.jsp b/webapp/view/fragments/publicmenu.jsp new file mode 100644 index 0000000..8661af9 --- /dev/null +++ b/webapp/view/fragments/publicmenu.jsp @@ -0,0 +1,30 @@ +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> + + + + diff --git a/webapp/view/hello.jsp b/webapp/view/hello.jsp new file mode 100644 index 0000000..1c12555 --- /dev/null +++ b/webapp/view/hello.jsp @@ -0,0 +1,10 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + +

Message : ${message}

+

Username : ${username}

+ + "> Logout + + + diff --git a/webapp/view/login.jsp b/webapp/view/login.jsp new file mode 100644 index 0000000..37e1b3e --- /dev/null +++ b/webapp/view/login.jsp @@ -0,0 +1,52 @@ + + +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> +<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> + + + + + + +
+ + + +
+
+
Enter your username (email used during registration) and password
+
+
+
+
+ + +
+ +

+ +

+ +
+
+ +
+ +

+ +

${error}

+ +
+
+
+
+
+
+
+ +
+ + diff --git a/webapp/view/public.jsp b/webapp/view/public.jsp new file mode 100644 index 0000000..4017906 --- /dev/null +++ b/webapp/view/public.jsp @@ -0,0 +1,34 @@ + + +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> + + + + +
+ +
+
+
+
Execution statistics for the last days
+
+ bla-bla +
+
+
+
+
+
Overview
+
+ ProteoCache is as a repository of the result of running all tools in the Dundee Resource on + complete proteomes. The data are updated on a regular basis as tools are improved and genomes newly + sequenced or updated. +
+
+
+
+ +
+ + \ No newline at end of file diff --git a/webapp/view/support/Denied.jsp b/webapp/view/support/Denied.jsp new file mode 100644 index 0000000..928cf35 --- /dev/null +++ b/webapp/view/support/Denied.jsp @@ -0,0 +1,29 @@ +<%@ page language="java" contentType="text/html; charset=UTF-8" + pageEncoding="UTF-8"%> + + +<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> +<%@page import="java.util.ArrayList"%> + + + + +
+ + +
+
+
The page is not available...
+
+
+

You don'h have enough permissions to view the page

+
+
+ + +
+ + \ No newline at end of file