package com.projectning.service.manager.impl;

import java.security.SecureRandom;
import java.time.Instant;
import java.util.Random;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.projectning.service.dao.UserDao;
import com.projectning.service.domain.User;
import com.projectning.service.manager.UserManager;

@Component
public class UserManagerImpl implements UserManager{

	@Autowired UserDao userDao;
	
	@Override
	public void register(String username, String password) throws IllegalStateException {
		if (username.length() > 32)
			throw new IllegalStateException("Username exceeds maximum length 32");
		if(password.length() != 32)
			throw new IllegalStateException("Password format incorrect");
		
		final Random r = new SecureRandom();
		byte[] salt = new byte[32];
		r.nextBytes(salt);
		String encodedSalt = Base64.encodeBase64String(salt).substring(0, 32);
		
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setCreatedAt(Instant.now());
	}

}

@@ -118,22 +118,137 @@
 va.util.
-Random
+ArrayList;%0Aimport java.util.List;%0Aimport java.util.Random;%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern
 ;%0A%0Aimpor
@@ -482,18 +482,130 @@
 ce.d
-omain.User
+ao.impl.QueryTerm;%0Aimport com.projectning.service.domain.User;%0Aimport com.projectning.service.exceptions.NotFoundException
 ;%0Aim
@@ -774,12 +774,14 @@
 lic 
-void
+String
  reg
@@ -785,16 +785,23 @@
 register
+ForSalt
 (String 
@@ -812,33 +812,83 @@
 name
-, String password) 
+) throws IllegalStateException %7B%0A%09%09if(checkUsername(username))%0A%09%09%09
 throw
-s
+ new
  Ill
@@ -901,26 +901,51 @@
 ateException
- %7B
+(%22Username already exist%22);
 %0A%09%09if (usern
@@ -1043,34 +1043,107 @@
 ;%0A%09%09
-if(password.length() != 32
+Pattern p = Pattern.compile(%22.+@.+%5C%5C.%5Ba-z%5D+%22);%0A%09%09Matcher m = p.matcher(username);%0A%09%09if(!m.matches()
 )%0A%09%09
@@ -1180,36 +1180,44 @@
 on(%22
-Password format incorrect%22);
+Username has to be an email%22);%0A%09%09%0A%09%09
 %0A%09%09%0A
@@ -1461,16 +1461,17 @@
 ord(
+%22
 password
 );%0A%09
@@ -1466,16 +1466,17 @@
 password
+%22
 );%0A%09%09use
@@ -1510,10 +1510,815 @@
 ));%0A
+%09%09user.setEmailConfirmed(false);%0A%09%09user.setSalt(encodedSalt);%0A%09%09userDao.persist(user);%0A%09%09%0A%09%09return encodedSalt;%0A%09%7D%0A%09%0A%09@Override%0A%09public void register(String username, String password) throws IllegalStateException, NotFoundException %7B%0A%09%09if(username.length() %3E 32)%0A%09%09%09throw new IllegalStateException(%22Username internal error%22);%0A%09%09if(password.length() != 32)%0A%09%09%09throw new IllegalStateException(%22Password internal error%22);%0A%09%09%0A%09%09List%3CQueryTerm%3E values = new ArrayList%3CQueryTerm%3E();%0A%09%09values.add(UserDao.Field.USERNAME.getQueryTerm(username));%0A%09%09User user = userDao.findObject(values);%0A%09%7D%0A%0A%09@Override%0A%09public boolean checkUsername(String username) %7B%0A%09%09List%3CQueryTerm%3E values = new ArrayList%3CQueryTerm%3E();%0A%09%09values.add(UserDao.Field.USERNAME.getQueryTerm(username));%0A%09%09return userDao.getCount(values) %3E 0;%0A%09%09%0A
 %09%7D%0A%0A%7D%0A
+%0A%09%0A

Update user password method