Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.epam.izh.rd.online.exception;

public class NotAccessException extends Exception{
public NotAccessException() {
}

public NotAccessException(String message) {
System.out.print(message);
}

public NotAccessException(String message, Throwable cause) {
super(message, cause);
}

public NotAccessException(Throwable cause) {
super(cause);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.epam.izh.rd.online.exception;

public class NotCorrectPasswordException extends Exception {
public NotCorrectPasswordException() {
}

public NotCorrectPasswordException(String message) {
super(message);
System.out.print(message);
}

public NotCorrectPasswordException(String message, Throwable cause) {
super(message, cause);
}

public NotCorrectPasswordException(Throwable cause) {
super(cause);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.epam.izh.rd.online.exception;

public class SimplePasswordException extends Exception {
public SimplePasswordException() {
}

public SimplePasswordException(String message) {
super(message);
System.out.print(message);
}

public SimplePasswordException(String message, Throwable cause) {
super(message, cause);
}

public SimplePasswordException(Throwable cause) {
super(cause);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.epam.izh.rd.online.exception;

public class UserAlreadyRegisteredException extends Exception {
public UserAlreadyRegisteredException() {
}

public UserAlreadyRegisteredException(String message) {
super(message);
System.out.print(message);
}

public UserAlreadyRegisteredException(String message, Throwable cause) {
super(message, cause);
}

public UserAlreadyRegisteredException(Throwable cause) {
super(cause);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.epam.izh.rd.online.exception;

public class UserNotFoundException extends Exception {
public UserNotFoundException() {
}

public UserNotFoundException(String message) {
super(message);
System.out.print(message);
}

public UserNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public UserNotFoundException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public User save(User user) {
} else {
userWithSameLogin.setPassword(user.getPassword());
}

return user;
}

Expand Down Expand Up @@ -75,4 +74,9 @@ public void deleteByLogin(String login) {

userDatabase.removeIf(user -> user.getLogin().equalsIgnoreCase(login));
}

@Override
public String toString() {
return " "+userDatabase;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.exception.NotCorrectPasswordException;
import com.epam.izh.rd.online.exception.UserNotFoundException;
import com.epam.izh.rd.online.repository.IUserRepository;

public class AuthenticationService implements IAuthenticationService {
Expand All @@ -25,20 +27,27 @@ public AuthenticationService(IUserRepository userRepository) {
* @param user - пользователь проходящий авторизацию
*/
@Override
public User login(User user) {
public User login(User user) throws UserNotFoundException, NotCorrectPasswordException {
// Находим пользователя в базе
User foundUser = userRepository.findByLogin(user.getLogin());

//
// Здесь необходимо реализовать перечисленные выше проверки
//

if (foundUser == null){
throw new UserNotFoundException("Пользователь с таким логином не найден");
}
if ( !( user.getLogin().compareTo(foundUser.getPassword()) ==0 ) ){
throw new NotCorrectPasswordException("Пароль введен неверно!");
}
// Устанавливаем найденного пользователя, который прошел все проверки, как вошедшего в систему.
CurrentUserManager.setCurrentLoggedInUser(foundUser);

return foundUser;
}



/**
* Данный метод очищает данные о текущем (активном) пользователе.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.exception.NotCorrectPasswordException;
import com.epam.izh.rd.online.exception.UserNotFoundException;

public interface IAuthenticationService {
User login(User user);
User login(User user) throws UserNotFoundException, NotCorrectPasswordException;

void logout();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.exception.NotAccessException;
import com.epam.izh.rd.online.exception.SimplePasswordException;
import com.epam.izh.rd.online.exception.UserAlreadyRegisteredException;

public interface IUserService {

User register(User user);
User register(User user) throws UserAlreadyRegisteredException, SimplePasswordException;

void delete(String login);
void delete(String login) throws NotAccessException;
}
29 changes: 26 additions & 3 deletions src/main/java/com/epam/izh/rd/online/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.epam.izh.rd.online.service;

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.exception.NotAccessException;
import com.epam.izh.rd.online.exception.SimplePasswordException;
import com.epam.izh.rd.online.exception.UserAlreadyRegisteredException;
import com.epam.izh.rd.online.repository.IUserRepository;
import com.epam.izh.rd.online.repository.UserRepository;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UserService implements IUserService {

private IUserRepository userRepository;
Expand All @@ -30,12 +36,26 @@ public UserService(IUserRepository userRepository) {
* @param user - даныне регистрирующегося пользователя
*/
@Override
public User register(User user) {
public User register(User user) throws UserAlreadyRegisteredException, SimplePasswordException {

//
// Здесь необходимо реализовать перечисленные выше проверки
//
if(user.getLogin().isEmpty() || user.getPassword().isEmpty()){
throw new IllegalArgumentException("Ошибка в заполнении полей");
}

System.out.println(user.getLogin());
System.out.println(userRepository);
if( userRepository.findByLogin(user.getLogin()) != null){
throw new UserAlreadyRegisteredException("Пользователь с логином '" + user.getLogin() + "' уже зарегистрирован");
}

Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(user.getPassword());
if (matcher.find()){
throw new SimplePasswordException("Пароль не соответствует требованиям безопасности");
}
// Если все проверки успешно пройдены, сохраняем пользователя в базу
return userRepository.save(user);
}
Expand All @@ -58,11 +78,14 @@ public User register(User user) {
*
* @param login
*/
public void delete(String login) {
public void delete(String login) throws NotAccessException {

// Здесь необходимо сделать доработку метод

try {
userRepository.deleteByLogin(login);
}catch (UnsupportedOperationException e){
throw new NotAccessException("Недостаточно прав для выполнения операции");
}

// Здесь необходимо сделать доработку метода

Expand Down