Checkpoint 5. +fix; +find by qr-code

This commit is contained in:
Gnazarov 2025-02-20 15:10:21 +03:00
parent 010e862edc
commit 6acc58cb59
15 changed files with 114 additions and 22 deletions

@ -24,6 +24,8 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
.csrf().disable()
.authorizeRequests()
.antMatchers("/api/employee/registration").hasAuthority("ROLE_ADMIN")
.antMatchers("/api/employee/login").hasAnyAuthority("ROLE_EMPLOYEE", "ROLE_ADMIN")
.antMatchers("/api/employee/**").authenticated()
.anyRequest().authenticated()
.and()
.httpBasic()

@ -1,6 +1,7 @@
package com.example.nto.controller;
import com.example.nto.dto.CodeDTO;
import com.example.nto.dto.EmployeeDTO;
import com.example.nto.dto.RegisterDTO;
import com.example.nto.entity.Code;
@ -98,4 +99,9 @@ public class EmployeeController {
return ResponseEntity.status(HttpStatus.OK).body(service.findEmployeeDTOByUsername(authentication.getName()));
}
@GetMapping("/qr-code")
public ResponseEntity<EmployeeDTO> getEmployeeByCode(@RequestBody CodeDTO dto){
return ResponseEntity.status(HttpStatus.OK).body(service.findEmployeeByCode(dto));
}
}

@ -0,0 +1,8 @@
package com.example.nto.dto;
import lombok.Data;
@Data
public class CodeDTO {
private long value;
}

@ -47,6 +47,10 @@ public class Employee implements UserDetails {
// @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
// @JoinTable(name = "Employee_AUTHORITIES")
@OneToOne
@JoinColumn(name = "code_id", referencedColumnName = "id")
private Code code;
@Override
public boolean isAccountNonExpired() {
return true;

@ -0,0 +1,7 @@
package com.example.nto.exceptions;
public class AuthorityNotFoundException extends RuntimeException {
public AuthorityNotFoundException(String message) {
super(message);
}
}

@ -0,0 +1,7 @@
package com.example.nto.exceptions;
public class CodeNotFoundException extends RuntimeException {
public CodeNotFoundException(String message) {
super(message);
}
}

@ -1,5 +1,7 @@
package com.example.nto.exceptions.handler;
import com.example.nto.exceptions.AuthorityNotFoundException;
import com.example.nto.exceptions.CodeNotFoundException;
import com.example.nto.exceptions.EmployeeAlreadyExistException;
import com.example.nto.exceptions.EmployeeNotFoundException;
import org.springframework.http.HttpStatus;
@ -19,5 +21,14 @@ public class GlobalExceptionHandler {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
}
@ExceptionHandler(AuthorityNotFoundException.class)
public ResponseEntity<String> authorityNotFoundExceptionHandler(AuthorityNotFoundException e){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
}
@ExceptionHandler(CodeNotFoundException.class)
public ResponseEntity<String> codeNotFoundExceptionHandler(CodeNotFoundException e){
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
}
}

@ -1,7 +1,10 @@
package com.example.nto.repository;
import com.example.nto.entity.Employee;
import com.example.nto.entity.Authority;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AuthorityRepository extends JpaRepository<Employee, Long> {
import java.util.Optional;
public interface AuthorityRepository extends JpaRepository<Authority, Long> {
Optional<Authority> findByAuthority(String authority);
}

@ -3,5 +3,8 @@ package com.example.nto.repository;
import com.example.nto.entity.Code;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.Optional;
public interface CodeRepository extends JpaRepository<Code, Long> {
Optional<Code> findByValue(long value);
}

@ -1,5 +1,6 @@
package com.example.nto.repository;
import com.example.nto.entity.Code;
import com.example.nto.entity.Employee;
import org.springframework.data.jpa.repository.JpaRepository;
@ -7,4 +8,7 @@ import java.util.Optional;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
public Optional<Employee> findEmployeeByUsername(String login);
public Optional<Employee> findEmployeeByCode(Code code);
}

@ -1,5 +1,6 @@
package com.example.nto.service;
import com.example.nto.dto.CodeDTO;
import com.example.nto.dto.EmployeeDTO;
import com.example.nto.dto.RegisterDTO;
import com.example.nto.entity.Employee;
@ -12,4 +13,6 @@ public interface EmployeeService {
public Employee findEmployeeByUsername(String login);
EmployeeDTO createEmployee(RegisterDTO dto);
EmployeeDTO findEmployeeByCode(CodeDTO dto);
}

@ -1,10 +1,17 @@
package com.example.nto.service.impl;
import com.example.nto.dto.CodeDTO;
import com.example.nto.dto.EmployeeDTO;
import com.example.nto.dto.RegisterDTO;
import com.example.nto.entity.Authority;
import com.example.nto.entity.Code;
import com.example.nto.entity.Employee;
import com.example.nto.exceptions.AuthorityNotFoundException;
import com.example.nto.exceptions.CodeNotFoundException;
import com.example.nto.exceptions.EmployeeAlreadyExistException;
import com.example.nto.exceptions.EmployeeNotFoundException;
import com.example.nto.repository.AuthorityRepository;
import com.example.nto.repository.CodeRepository;
import com.example.nto.repository.EmployeeRepository;
import com.example.nto.service.EmployeeService;
import com.example.nto.util.EmployeeMapper;
@ -14,39 +21,47 @@ import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.Optional;
import java.util.Set;
@Service
@AllArgsConstructor
public class EmployeeServiceImpl implements EmployeeService {
private EmployeeRepository repository;
private EmployeeRepository employeeRepository;
private final PasswordEncoder passwordEncoder;
private final AuthorityRepository authorityRepository;
private final CodeRepository codeRepository;
@Override
public void updateLocalTime(Employee employee) {
employee.setLastVisit(LocalDateTime.now().toString());
repository.save(employee);
employeeRepository.save(employee);
}
@Override
public Employee findEmployeeByUsername(String login) {
return repository.findEmployeeByUsername(login).orElseThrow(() -> new EmployeeNotFoundException("Employee with username "+login+" not found"));
return employeeRepository.findEmployeeByUsername(login).orElseThrow(() -> new EmployeeNotFoundException("Employee with username "+login+" not found"));
}
public EmployeeDTO findEmployeeDTOByUsername(String login) {
return EmployeeMapper.convertToDTO(repository.findEmployeeByUsername(login).orElseThrow(() -> new EmployeeNotFoundException("Employee with username "+login+" not found")));
return EmployeeMapper.convertToDTO(employeeRepository.findEmployeeByUsername(login).orElseThrow(() -> new EmployeeNotFoundException("Employee with username "+login+" not found")));
}
@Override
public EmployeeDTO createEmployee(RegisterDTO dto) {
Optional<Employee> optionalEmployee = repository.findEmployeeByUsername(dto.getUsername());
Optional<Employee> optionalEmployee = employeeRepository.findEmployeeByUsername(dto.getUsername());
if(optionalEmployee.isPresent()){
throw new EmployeeAlreadyExistException("Employee with username " + dto.getUsername() +" already exist");
}
Optional<Authority> optionalAuthority = authorityRepository.findByAuthority("ROLE_EMPLOYEE");
if(optionalAuthority.isEmpty()){
throw new AuthorityNotFoundException("Authority not found");
}
Employee employee = new Employee();
employee.setName(dto.getName());
employee.setUsername(dto.getUsername());
@ -58,7 +73,24 @@ public class EmployeeServiceImpl implements EmployeeService {
employee.setJobPos(dto.getJobPos());
employee.setLastVisit(LocalDateTime.now().toString());
return EmployeeMapper.convertToDTO(repository.save(employee));
employee.setAuthorities(Set.of(optionalAuthority.get()));
return EmployeeMapper.convertToDTO(employeeRepository.save(employee));
}
@Override
public EmployeeDTO findEmployeeByCode(CodeDTO dto) {
Optional<Code> optionalCode = codeRepository.findByValue(dto.getValue());
if(optionalCode.isEmpty()){
throw new CodeNotFoundException("Qr-Code "+dto.getValue()+" not found");
}
Optional<Employee> optionalEmployee = employeeRepository.findEmployeeByCode(optionalCode.get());
if(optionalEmployee.isEmpty()){
throw new EmployeeNotFoundException("Employee by qr-code "+dto.getValue()+" not found");
}
return EmployeeMapper.convertToDTO(optionalEmployee.get());
}

@ -30,7 +30,7 @@ spring:
hibernate:
#ddl-auto: none
ddl-auto: update
ddl-auto: none
# Показываем запросы
show-sql: true

@ -3,21 +3,21 @@ VALUES
(1, 'ROLE_EMPLOYEE'),
(2, 'ROLE_ADMIN');
INSERT INTO employee (id, username, password, name, photo, job_pos, last_visit)
INSERT INTO code (id, value)
VALUES
(1, 'pivanov', '$2a$12$oSvuYhIhHJtyw3Gp542S3.WI2aupaIQ5265ItMDvnTPopcLQudx9q', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30'),
(2, 'ipetrov', '$2a$12$oSvuYhIhHJtyw3Gp542S3.WI2aupaIQ5265ItMDvnTPopcLQudx9q', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35'),
(3, 'asemenov', '$2a$12$oSvuYhIhHJtyw3Gp542S3.WI2aupaIQ5265ItMDvnTPopcLQudx9q', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31'),
(4, 'afedorov', '$2a$12$oSvuYhIhHJtyw3Gp542S3.WI2aupaIQ5265ItMDvnTPopcLQudx9q', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36'),
(5, 'gnazarov', '$2a$12$QH3S01HpdzDARg4qrZ4Qee9SkFmxmau2SiEvsSg5M17K2vBBm673O', 'Назаров Г. Н.', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Администратор', '2024-02-12T08:36');
(1, 1234567890123456789),
(2, 9223372036854775807),
(3, 1122334455667788990),
(4, 998877665544332211),
(5, 5566778899001122334);
INSERT INTO code (value)
INSERT INTO employee (username, password, name, photo, job_pos, last_visit, code_id)
VALUES
(1234567890123456789),
(9223372036854775807),
(1122334455667788990),
(998877665544332211),
(5566778899001122334);
('pivanov', '$2a$12$oSvuYhIhHJtyw3Gp542S3.WI2aupaIQ5265ItMDvnTPopcLQudx9q', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30', 3),
('ipetrov', '$2a$12$oSvuYhIhHJtyw3Gp542S3.WI2aupaIQ5265ItMDvnTPopcLQudx9q', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35', 2),
('asemenov', '$2a$12$oSvuYhIhHJtyw3Gp542S3.WI2aupaIQ5265ItMDvnTPopcLQudx9q', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31', 4),
('afedorov', '$2a$12$oSvuYhIhHJtyw3Gp542S3.WI2aupaIQ5265ItMDvnTPopcLQudx9q', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36', 1),
('gnazarov', '$2a$12$QH3S01HpdzDARg4qrZ4Qee9SkFmxmau2SiEvsSg5M17K2vBBm673O', 'Назаров Г. Н.', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Администратор', '2024-02-12T08:36', 5);
INSERT INTO employee_authorities(employee_id, authorities_id)
VALUES

@ -8,9 +8,11 @@ password VARCHAR(255),
name VARCHAR(255),
photo VARCHAR(255),
job_pos VARCHAR(255),
last_visit VARCHAR(255)
last_visit VARCHAR(255),
--authorities_id BIGINT NOT NULL,
--CONSTRAINT fk_employee_authorities FOREIGN KEY(authorities_id) REFERENCES authorities(id)
code_id BIGINT NOT NULL,
CONSTRAINT fk_employee_code FOREIGN KEY(code_id) REFERENCES code(id)
);
CREATE TABLE IF NOT EXISTS employee_authorities(