Skip to content
Open

Ok #21

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

public class NotAccessException extends Exception {
public NotAccessException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.epam.izh.rd.online.exception;

public class NotCorrectPasswordException extends Exception {
public NotCorrectPasswordException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.epam.izh.rd.online.exception;

public class SimplePasswordException extends Exception {
public SimplePasswordException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.epam.izh.rd.online.exception;

public class UserAlreadyRegisteredException extends Exception {
public UserAlreadyRegisteredException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.epam.izh.rd.online.exception;

public class UserNotFoundException extends Exception {
public UserNotFoundException(String message) {
super(message);
}
}
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,23 @@ public AuthenticationService(IUserRepository userRepository) {
* @param user - пользователь проходящий авторизацию
*/
@Override
public User login(User user) {
public User login(User user) throws Exception {
// Находим пользователя в базе
User foundUser = userRepository.findByLogin(user.getLogin());

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

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

return foundUser;
}


/**
* Данный метод очищает данные о текущем (активном) пользователе.
*/
Expand Down
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);
void delete(String login) throws Exception;
}
31 changes: 21 additions & 10 deletions src/main/java/com/epam/izh/rd/online/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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;

Expand Down Expand Up @@ -30,11 +33,19 @@ public UserService(IUserRepository userRepository) {
* @param user - даныне регистрирующегося пользователя
*/
@Override
public User register(User user) {
public User register(User user) throws Exception {
if (!user.getLogin().matches("\\w+")
|| !user.getPassword().matches("\\w+")) {
throw new IllegalArgumentException("Ошибка в заполнении полей");
}

//
// Здесь необходимо реализовать перечисленные выше проверки
//
if (!(userRepository.findByLogin(user.getLogin()) == null)) {
throw new UserAlreadyRegisteredException("Пользователь с логином " + user.getLogin() + " уже зарегистрирован");
}

if (user.getPassword().matches("\\d+")){
throw new SimplePasswordException("Пароль не соответствует требованиям безопасности");
}

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

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

public void delete(String login) throws Exception {
try {
userRepository.deleteByLogin(login);

// Здесь необходимо сделать доработку метода
} catch (UnsupportedOperationException e) {
throw new NotAccessException("Недостаточно прав для выполнения операции");
}

}

}