prevent non admin users from receiving info about others
This commit is contained in:
parent
26937a8328
commit
4529f95264
@ -28,7 +28,9 @@ public class EmployeeController {
|
|||||||
|
|
||||||
@GetMapping("/info")
|
@GetMapping("/info")
|
||||||
public EmployeeDTO info(@RequestParam final String login) {
|
public EmployeeDTO info(@RequestParam final String login) {
|
||||||
return employeeService.getEmployeeDTO(login);
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
String recipientLogin = authentication.getName();
|
||||||
|
return employeeService.getEmployeeDTO(login, recipientLogin);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PatchMapping("/open")
|
@PatchMapping("/open")
|
||||||
|
@ -6,7 +6,7 @@ import org.springframework.security.core.userdetails.UserDetails;
|
|||||||
|
|
||||||
public interface EmployeeService {
|
public interface EmployeeService {
|
||||||
|
|
||||||
EmployeeDTO getEmployeeDTO(String login);
|
EmployeeDTO getEmployeeDTO(String login, final String recipientLogin);
|
||||||
|
|
||||||
void updateVisit(String login, long value);
|
void updateVisit(String login, long value);
|
||||||
|
|
||||||
|
@ -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 NotAnAdminException extends RuntimeException {}
|
@ -1,6 +1,7 @@
|
|||||||
package com.example.nto.service.impl;
|
package com.example.nto.service.impl;
|
||||||
|
|
||||||
import com.example.nto.mapper.EmployeeDTOMapper;
|
import com.example.nto.mapper.EmployeeDTOMapper;
|
||||||
|
import com.example.nto.model.EmployeeRoleType;
|
||||||
import com.example.nto.model.dto.EmployeeDTO;
|
import com.example.nto.model.dto.EmployeeDTO;
|
||||||
import com.example.nto.model.entity.Employee;
|
import com.example.nto.model.entity.Employee;
|
||||||
import com.example.nto.repository.CodeRepository;
|
import com.example.nto.repository.CodeRepository;
|
||||||
@ -9,11 +10,13 @@ import com.example.nto.service.EmployeeService;
|
|||||||
import com.example.nto.service.exception.CodeNotFoundException;
|
import com.example.nto.service.exception.CodeNotFoundException;
|
||||||
import com.example.nto.service.exception.EmployeeBannedException;
|
import com.example.nto.service.exception.EmployeeBannedException;
|
||||||
import com.example.nto.service.exception.EmployeeNotFoundException;
|
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;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@ -34,30 +37,30 @@ public class EmployeeServiceImpl implements EmployeeService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EmployeeDTO getEmployeeDTO(final String login) {
|
public EmployeeDTO getEmployeeDTO(final String login, final String recipientLogin) {
|
||||||
if (!employeeRepository.existsByLogin(login)) {
|
employeeExists(login);
|
||||||
throw new EmployeeNotFoundException();
|
|
||||||
}
|
|
||||||
var e = employeeRepository.findEmployeeByLogin(login);
|
var e = employeeRepository.findEmployeeByLogin(login);
|
||||||
|
if (!Objects.equals(e.getLogin(), recipientLogin)) {
|
||||||
|
var recipient = getEmployee(recipientLogin);
|
||||||
|
if (recipient.getRole() != EmployeeRoleType.ADMIN) {
|
||||||
|
throw new NotAnAdminException();
|
||||||
|
}
|
||||||
|
}
|
||||||
return employeeDTOMapper.map(e);
|
return employeeDTOMapper.map(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Employee getEmployee(final String login) {
|
private Employee getEmployee(final String login) {
|
||||||
if (!employeeRepository.existsByLogin(login)) {
|
employeeExists(login);
|
||||||
throw new EmployeeNotFoundException();
|
|
||||||
}
|
|
||||||
return employeeRepository.findEmployeeByLogin(login);
|
return employeeRepository.findEmployeeByLogin(login);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateVisit(final String login, final long value) {
|
public void updateVisit(final String login, final long value) {
|
||||||
if (!employeeRepository.existsByLogin(login)) {
|
employeeExists(login);
|
||||||
throw new EmployeeNotFoundException();
|
|
||||||
}
|
|
||||||
if (!codeRepository.existsByValue(value)) {
|
if (!codeRepository.existsByValue(value)) {
|
||||||
throw new CodeNotFoundException();
|
throw new CodeNotFoundException();
|
||||||
}
|
}
|
||||||
final Employee employee = employeeRepository.findEmployeeByLogin(login);
|
final Employee employee = getEmployee(login);
|
||||||
if (employee.getIsBanned()) {
|
if (employee.getIsBanned()) {
|
||||||
throw new EmployeeBannedException();
|
throw new EmployeeBannedException();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user