diff --git a/pom.xml b/pom.xml index d73f6d5..88282ee 100644 --- a/pom.xml +++ b/pom.xml @@ -52,25 +52,6 @@ spring-boot-starter-test test - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.security - spring-security-test - test - - \ No newline at end of file diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 61d5269..46618d9 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -1,89 +1,92 @@ package com.example.nto.controller; import com.example.nto.entity.Employee; -import com.example.nto.entity.User; import com.example.nto.service.EmployeeService; -import com.example.nto.service.UserService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.security.crypto.password.PasswordEncoder; // Импортируйте PasswordEncoder import org.springframework.web.bind.annotation.*; -import java.util.List; import java.util.Map; import java.util.Optional; +//я поменял на BAD_REQUEST 06.12.24 23:00 @RestController -@RequestMapping("/api") +@RequestMapping("/api")//база public class EmployeeController { private final EmployeeService employeeService; - private final UserService userService; - private final PasswordEncoder passwordEncoder; // Добавленное поле - public EmployeeController(EmployeeService employeeService, UserService userService, PasswordEncoder passwordEncoder) { + public EmployeeController(EmployeeService employeeService) { + this.employeeService = employeeService; - this.userService = userService; - this.passwordEncoder = passwordEncoder; // Инициализация поля + + } - @PreAuthorize("hasRole('ADMIN')") - @GetMapping("/admin/employees") - public ResponseEntity> getAllEmployees() { - List employees = employeeService.findAll(); - return ResponseEntity.ok(employees); - } - @PostMapping("/register") - public ResponseEntity registerUser(@RequestBody User user) { - userService.saveUser(user); - return ResponseEntity.status(HttpStatus.CREATED).body("User registered successfully"); - } - - @GetMapping("/{login}/auth") // auth + @GetMapping("/{login}/auth")//auth public ResponseEntity authenticate(@PathVariable String login) { Optional employee = employeeService.findByLogin(login); if (employee.isPresent()) { return ResponseEntity.ok("Valid login"); + + } else { return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login"); + } + } - @GetMapping("/{login}/info") + @GetMapping("/{login}/info")//info public ResponseEntity getInfo(@PathVariable String login) { Optional employee = employeeService.findByLogin(login); if (employee.isPresent()) { return ResponseEntity.ok(employee.get()); + + } else { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login"); + } - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login"); } - @PatchMapping("/{login}/open") // open + + + @PatchMapping("/{login}/open")//open public ResponseEntity openDoor(@PathVariable String login, @RequestBody Map payload) { Long code = payload.get("value"); if (code == null) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid payload"); + } + + Optional employee = employeeService.findByLogin(login); if (employee.isEmpty()) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid login"); + } + + if (employeeService.validateCode(login, code)) { return ResponseEntity.ok("Door opened"); + } else { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid code"); } } + @PostMapping("/auth") // auth + public ResponseEntity authenticate(@RequestBody Map payload) { + String login = payload.get("login"); + String password = payload.get("password"); - @GetMapping("/auth") - public ResponseEntity authenticate(@RequestParam String username, @RequestParam String password) { - Optional optionalUser = userService.findByUsername(username); // Исправление - if (optionalUser.isPresent() && passwordEncoder.matches(password, optionalUser.get().getPassword())) { // Исправление + Optional employee = employeeService.findByLogin(login); + if (employee.isPresent() && employee.get().getPassword().equals(password)) { return ResponseEntity.ok("Valid login"); + } else { + return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login or password"); } - return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login"); } -} \ No newline at end of file +} +// made by truettwo and maks )) \ No newline at end of file diff --git a/src/main/java/com/example/nto/entity/Employee.java b/src/main/java/com/example/nto/entity/Employee.java index 9c05060..2f94d2d 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -23,5 +23,7 @@ public class Employee { @Column(name = "last_visit") private LocalDateTime lastVisit; - // Геттеры и сеттеры для login, name, и других полей + private String password; // Добавлено поле для пароля + + // Геттеры и сеттеры для login, name, password и других полей } \ No newline at end of file diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 255ec94..a1ad883 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -9,13 +9,12 @@ CREATE TABLE IF NOT EXISTS employee ( ); -- Вставка данных в таблицу employee -INSERT INTO employee (id, login, name, photo, position, last_visit) +INSERT INTO employee (id, login, name, photo, position, last_visit, password) VALUES - (1, 'pivanov', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30'), - (2, 'ipetrov', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35'), - (3, 'asemenov', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31'), - (4, 'afedorov', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36'); - + (1, 'pivanov', 'Иванов Петр Федорович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-12T08:30', 'password123'), + (2, 'ipetrov', 'Петров Иван Константинович', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Аналитик', '2024-02-13T08:35', 'password456'), + (3, 'asemenov', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31', 'password789'), + (4, 'afedorov', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36', 'password000'); -- Создание таблицы code CREATE TABLE IF NOT EXISTS code ( value BIGINT