prohibit banning admins

This commit is contained in:
Konstantin 2025-02-20 15:33:27 +03:00
parent a8480602c9
commit 141369cdec
2 changed files with 12 additions and 4 deletions

View File

@ -0,0 +1,7 @@
package com.example.nto.service.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.FORBIDDEN)
public class CantBanAnAdminException extends RuntimeException {}

View File

@ -9,10 +9,7 @@ import com.example.nto.repository.CodeRepository;
import com.example.nto.repository.EmployeeRepository; import com.example.nto.repository.EmployeeRepository;
import com.example.nto.repository.EntryRepository; import com.example.nto.repository.EntryRepository;
import com.example.nto.service.EmployeeService; import com.example.nto.service.EmployeeService;
import com.example.nto.service.exception.CodeNotFoundException; import com.example.nto.service.exception.*;
import com.example.nto.service.exception.EmployeeBannedException;
import com.example.nto.service.exception.EmployeeNotFoundException;
import com.example.nto.service.exception.NotAnAdminException;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -81,6 +78,10 @@ public class EmployeeServiceImpl implements EmployeeService {
@Override @Override
public void banEmployee(String login) { public void banEmployee(String login) {
employeeExists(login); employeeExists(login);
var e = getEmployee(login);
if (e.getRole() == EmployeeRoleType.ADMIN) {
throw new CantBanAnAdminException();
}
var employee = getEmployee(login); var employee = getEmployee(login);
employee.setIsBanned(true); employee.setIsBanned(true);
employeeRepository.save(employee); employeeRepository.save(employee);