From 119a5868ac143ef9c9e9561405319022be1bdd0c Mon Sep 17 00:00:00 2001 From: truettwo Date: Wed, 19 Feb 2025 18:20:45 +0300 Subject: [PATCH] fixes --- .../nto/controller/EmployeeController.java | 8 ++++-- .../java/com/example/nto/entity/Code.java | 28 ++----------------- .../java/com/example/nto/entity/Employee.java | 19 ------------- src/main/resources/data.sql | 13 ++++----- 4 files changed, 14 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/example/nto/controller/EmployeeController.java b/src/main/java/com/example/nto/controller/EmployeeController.java index 90548b5..7ff1e22 100644 --- a/src/main/java/com/example/nto/controller/EmployeeController.java +++ b/src/main/java/com/example/nto/controller/EmployeeController.java @@ -5,6 +5,7 @@ import com.example.nto.service.EmployeeService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -74,10 +75,13 @@ public class EmployeeController { } } - @PreAuthorize("hasRole('admin')") // Проверьте, что пользователь имеет роль admin + @PreAuthorize("hasRole('admin')") @GetMapping("/workers") public ResponseEntity> getAllWorkers() { - List allEmployees = employeeService.findAll(); // Получить всех сотрудников + String currentUserName = SecurityContextHolder.getContext().getAuthentication().getName(); + // Логируем информацию о текущем пользователе + System.out.println("Current user: " + currentUserName); + List allEmployees = employeeService.findAll(); return ResponseEntity.ok(allEmployees); } } \ No newline at end of file diff --git a/src/main/java/com/example/nto/entity/Code.java b/src/main/java/com/example/nto/entity/Code.java index 9aef6b9..5455481 100644 --- a/src/main/java/com/example/nto/entity/Code.java +++ b/src/main/java/com/example/nto/entity/Code.java @@ -4,39 +4,15 @@ import javax.persistence.*; @Entity public class Code { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - private Long value; // Значение кода + private Long value; - @ManyToOne // Установите связь с Employee + @ManyToOne @JoinColumn(name = "employee_id", nullable = false) private Employee employee; // Геттеры и сеттеры - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Long getValue() { - return value; - } - - public void setValue(Long value) { - this.value = value; - } - - public Employee getEmployee() { - return employee; - } - - public void setEmployee(Employee employee) { - this.employee = employee; - } } \ 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 ef9ef89..5949da6 100644 --- a/src/main/java/com/example/nto/entity/Employee.java +++ b/src/main/java/com/example/nto/entity/Employee.java @@ -27,25 +27,6 @@ public class Employee { private String role; - public String getLogin() { - return login; // Возвращает логин - } - - public void setRole(String role) { - this.role = role; // Устанавливает роль - } - - public String getRole() { - return role; // Возвращает роль (например, 'admin' или 'user') - } - - public void setPassword(String password) { - this.password = password; // Устанавливает пароль - } - - public String getPassword() { - return password; // Возвращает пароль - } diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index d57876b..6ae1943 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -24,10 +24,9 @@ CREATE TABLE IF NOT EXISTS code ( ); -- Вставка данных в таблицу code -INSERT INTO code (value) -VALUES - (1234567890123456789), - (9223372036854775807), - (1122334455667788990), - (998877665544332211), - (5566778899001122334); \ No newline at end of file +INSERT INTO code (value, employee_id) VALUES + (1234567890123456789, 1), + (9223372036854775807, 2), + (1122334455667788990, 3), + (998877665544332211, 4), + (5566778899001122334, 1); \ No newline at end of file