Partly working security: registration form, authorisaztion, simple authentication
[proteocache.git] / server / compbio / controllers / UserController.java
diff --git a/server/compbio/controllers/UserController.java b/server/compbio/controllers/UserController.java
new file mode 100644 (file)
index 0000000..94be377
--- /dev/null
@@ -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);
+       }
+
+}