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,9 @@
package com.epam.izh.rd.online.exception;

public class NotAccessException extends UnsupportedOperationException {

public NotAccessException() {
System.out.println("Недостаточно прав для выполнения операции");
}

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

public class NotCorrectPasswordException extends Exception {

public NotCorrectPasswordException() {
System.out.println("Пароль введен неверно!");
}

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

public class SimplePasswordException extends Exception {

public SimplePasswordException() {
System.out.println("Пароль не соответствует требованиям безопасности");
}

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

import com.epam.izh.rd.online.entity.User;
import com.epam.izh.rd.online.repository.IUserRepository;
import com.epam.izh.rd.online.repository.UserRepository;

public class UserAlreadyRegisteredException extends Exception {

public UserAlreadyRegisteredException(User foundUser) {
System.out.println("Пользователь с логином " + foundUser.getLogin() + " уже зарегистрирован");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.epam.izh.rd.online.exception;

public class UserNotFoundException extends Exception {

public UserNotFoundException() {
System.out.println("Пользователь с таким логином не найден");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public interface IUserRepository {

User findByLogin(String login);

User findByPassword(String password); // (^_^)

void deleteByLogin(String login);
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public User findByLogin(String login) {
.findFirst().orElse(null);
}

@Override
public User findByPassword(String password) {
Objects.requireNonNull(password, "Отсутствует пароль для поиска пользователя");

return userDatabase.stream()
.filter(user -> user.getPassword().equals(password))
.findFirst().orElse(null);
}

/**
* Метод удаления пользователя с определенным логином.
* <p>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.epam.izh.rd.online.service;

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

Expand All @@ -24,19 +26,25 @@ public AuthenticationService(IUserRepository userRepository) {
*
* @param user - пользователь проходящий авторизацию
*/

@Override
public User login(User user) {
// Находим пользователя в базе
User foundUser = userRepository.findByLogin(user.getLogin());
public User login(User user) throws Exception {

if (userRepository.findByLogin(user.getLogin()) == null) {
throw new UserNotFoundException();
}

User foundUser = userRepository.findByLogin(user.getLogin());

//
// Здесь необходимо реализовать перечисленные выше проверки
//
if (userRepository.findByPassword(user.getPassword()) == null || !foundUser.getPassword().equals(userRepository.findByPassword(user.getPassword()).getPassword())) {
throw new NotCorrectPasswordException();
}

// Устанавливаем найденного пользователя, который прошел все проверки, как вошедшего в систему.
CurrentUserManager.setCurrentLoggedInUser(foundUser);

return foundUser;
// Устанавливаем найденного пользователя, который прошел все проверки, как вошедшего в систему.
CurrentUserManager.setCurrentLoggedInUser(foundUser);
return foundUser;

}

/**
Expand All @@ -46,4 +54,6 @@ public User login(User user) {
public void logout() {
CurrentUserManager.setCurrentLoggedInUser(null);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.epam.izh.rd.online.entity.User;

public interface IAuthenticationService {
User login(User user);
User login(User user) throws Exception;

void logout();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public interface IUserService {

User register(User user);
User register(User user) throws Exception;

void delete(String login);
}
40 changes: 35 additions & 5 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,14 @@
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.stream.Collectors;

public class UserService implements IUserService {

private IUserRepository userRepository;
Expand All @@ -29,12 +34,31 @@ public UserService(IUserRepository userRepository) {
*
* @param user - даныне регистрирующегося пользователя
*/



@Override
public User register(User user) {
public User register(User user) throws Exception {

// проверяем заполняемость полей

if (user.getPassword().trim().length() == 0 || user.getLogin().trim().length() == 0) {
throw new IllegalArgumentException("Ошибка в заполнении полей");
}

//
// Здесь необходимо реализовать перечисленные выше проверки
//
// проверяем есть ли в базе юзер с таким же логином
User foundUser = userRepository.findByLogin(user.getLogin());


if (foundUser != null) {
throw new UserAlreadyRegisteredException(foundUser);
}

// проверяем сложность пароля

if (user.getPassword().trim().chars().filter(y -> y < 48 || y > 57).mapToObj(String::valueOf).collect(Collectors.toList()).size() < 1) {
throw new SimplePasswordException();
}

// Если все проверки успешно пройдены, сохраняем пользователя в базу
return userRepository.save(user);
Expand All @@ -61,9 +85,15 @@ public User register(User user) {
public void delete(String login) {

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

userRepository.deleteByLogin(login);
}
catch(UnsupportedOperationException e) {

userRepository.deleteByLogin(login);
throw new NotAccessException();

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

}
Expand Down