This commit is contained in:
truettwo 2025-02-19 12:02:40 +03:00
parent ae15a71a96
commit 23f38f31d2
2 changed files with 42 additions and 10 deletions

19
pom.xml
View File

@ -52,6 +52,25 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -1,27 +1,35 @@
package com.example.nto.controller; package com.example.nto.controller;
import com.example.nto.entity.Employee; import com.example.nto.entity.Employee;
import com.example.nto.entity.User;
import com.example.nto.service.EmployeeService; import com.example.nto.service.EmployeeService;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
//я поменял на BAD_REQUEST 06.12.24 23:00 //я поменял на BAD_REQUEST 06.12.24 23:00
@RestController @RestController
@RequestMapping("/api")//база @RequestMapping("/api")
public class EmployeeController { public class EmployeeController {
private final EmployeeService employeeService; private final EmployeeService employeeService;
public EmployeeController(EmployeeService employeeService) { public EmployeeController(EmployeeService employeeService) {
this.employeeService = employeeService; this.employeeService = employeeService;
} }
@PreAuthorize("hasRole('ADMIN')")
@GetMapping("/admin/employees")
public ResponseEntity<List<Employee>> getAllEmployees() {
List<Employee> employees = employeeService.findAll();
return ResponseEntity.ok(employees);
}
@GetMapping("/{login}/auth")//auth @GetMapping("/{login}/auth")//auth
public ResponseEntity<?> authenticate(@PathVariable String login) { public ResponseEntity<?> authenticate(@PathVariable String login) {
@ -37,20 +45,16 @@ public class EmployeeController {
} }
@GetMapping("/{login}/info")//info @GetMapping("/{login}/info")
public ResponseEntity<?> getInfo(@PathVariable String login) { public ResponseEntity<?> getInfo(@PathVariable String login) {
Optional<Employee> employee = employeeService.findByLogin(login); Optional<Employee> employee = employeeService.findByLogin(login);
if (employee.isPresent()) { if (employee.isPresent()) {
return ResponseEntity.ok(employee.get()); 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<String, Long> payload) { public ResponseEntity<?> openDoor(@PathVariable String login, @RequestBody Map<String, Long> payload) {
Long code = payload.get("value"); Long code = payload.get("value");
@ -76,5 +80,14 @@ public class EmployeeController {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid code"); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Invalid code");
} }
} }
@GetMapping("/auth")
public ResponseEntity<?> authenticate(@RequestParam String username, @RequestParam String password) {
User user = userService.findByUsername(username);
if (user != null && passwordEncoder.matches(password, user.getPassword())) {
return ResponseEntity.ok("Valid login");
}
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid login");
}
} }
// made by truettwo and maks )) // made by truettwo and maks ))