1 package compbio.controllers;
4 import java.util.regex.Pattern;
6 import javax.naming.directory.DirContext;
8 import org.springframework.beans.factory.annotation.Autowired;
9 import org.springframework.dao.DataIntegrityViolationException;
10 import org.springframework.mail.SimpleMailMessage;
11 import org.springframework.security.ldap.LdapUtils;
12 import org.springframework.ldap.core.ContextSource;
13 import org.springframework.stereotype.Controller;
14 import org.springframework.ui.Model;
15 import org.springframework.ui.ModelMap;
16 import org.springframework.validation.BindingResult;
17 import org.springframework.validation.FieldError;
18 import org.springframework.web.bind.annotation.ModelAttribute;
19 import org.springframework.web.bind.annotation.RequestMapping;
20 import org.springframework.web.bind.annotation.RequestMethod;
21 import org.springframework.web.bind.annotation.RequestParam;
23 import compbio.proteocache.users.User;
24 import compbio.proteocache.users.UserManager;
25 import compbio.cassandra.CassandraUserManager;
28 public class UserController {
31 ContextSource contextSource;
33 // JavaMailSender mailSender;
34 private final Pattern EMAIL = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");
36 @RequestMapping(value = "/register/query", method = RequestMethod.GET)
37 public String RegisterForm(ModelMap model) {
38 User user = new User();
39 model.addAttribute(user);
43 @RequestMapping(value = "/register/edit/query", method = RequestMethod.GET)
44 public String AccountForm(ModelMap model) {
46 User user = new User();
47 user.setFullName("Sasha Sherstnev");
48 user.setEmail("admin@admin.com");
49 user.setOrganisation("UoD");
50 user.setPosition("cleaner");
51 user.setUpdateByEmail(true);
52 model.addAttribute("u", user);
55 return "support/Notimplemented";
58 @RequestMapping(value = "/register/do", method = RequestMethod.POST)
59 public String addUser(Model model, @ModelAttribute("user") User user, BindingResult bindingResult) {
61 if (bindingResult.hasErrors()) {
65 int fullName = user.getFullName().length();
66 if (fullName < 6 || 50 < fullName) {
67 bindingResult.addError(new FieldError("user", "fullName", "Your full name must be between 3 and 50 symbols long!"));
68 model.addAttribute("error", "wrong password");
72 if (!EMAIL.matcher(user.getEmail()).find()) {
73 bindingResult.addError(new FieldError("user", "email", "Email is empty or in a wrong form!"));
74 model.addAttribute("error", "wrong email");
78 int password = user.getPassword().length();
79 if (password < 6 || 20 < password) {
80 bindingResult.addError(new FieldError("user", "password", "The password must be at least 6 symbols long!"));
81 model.addAttribute("error", "wrong password");
86 if (null != user.getOrganisation())
87 organisation = user.getOrganisation().length();
88 if (organisation < 3 || 250 < organisation) {
89 bindingResult.addError(new FieldError("user", "organisation", "The organisation must be between 3 and 250 symbols long!"));
90 model.addAttribute("error", "wrong organisation name");
94 user.setRegistrationDate(new Date());
95 CassandraUserManager cm = new CassandraUserManager();
98 } catch (DataIntegrityViolationException e) {
99 bindingResult.addError(new FieldError("user", "email", "This email (username) is already in use!"));
100 model.addAttribute("error", "used email");
103 if (user.isUpdateByEmail()) {
104 subscribeToList(user.getEmail());
107 * Account.autoLogin(user, request, authenticationManager);
109 return "redirect:/index";
112 @RequestMapping(value = "/register/edit/do", method = RequestMethod.POST)
113 public String editUser(Model model, @ModelAttribute("user") User user, BindingResult bindingResult) {
115 if (bindingResult.hasErrors()) {
119 user.setRegistrationDate(new Date());
120 UserManager cm = new CassandraUserManager();
123 } catch (DataIntegrityViolationException e) {
124 bindingResult.addError(new FieldError("user", "email", "This email (username) is already in use!"));
127 if (user.isUpdateByEmail()) {
128 subscribeToList(user.getEmail());
131 return "redirect:/index";
134 private void subscribeToList(String email) {
135 SimpleMailMessage message = new SimpleMailMessage();
136 message.setFrom(email);
137 message.setTo("proteocache-discuss-subscribe@compbio.dundee.ac.uk");
138 message.setSubject("ProteoCache mailing list subscription");
139 message.setText("testing " + email);
140 // mailSender.send(message);
143 private boolean authenticate(String userDn, String credentials) {
144 DirContext ctx = null;
146 ctx = contextSource.getContext(userDn, credentials);
148 } catch (Exception e) {
149 // Context creation failed - authentication did not succeed
150 System.out.println("LDAP Login failed");
153 // It is imperative that the created DirContext instance is always
155 LdapUtils.closeContext(ctx);
159 @RequestMapping(value = "/ldaplogindo", method = RequestMethod.POST)
160 public String LDAPlogin(Model model, @RequestParam("j_username") String username, @RequestParam("j_password") String credentials) {
161 System.out.println("Try to authenticate with LDAP: username: " + username + ", credentials: " + credentials);
162 if (authenticate(username, credentials)) {