This commit is contained in:
Justiks 2025-02-20 12:56:52 +03:00
parent f147f9acf9
commit e9642bde10
7 changed files with 83 additions and 52 deletions

35
.idea/workspace.xml generated
View File

@ -4,18 +4,14 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="067ac1f0-be04-4fe4-85c6-f870334053b8" name="Changes" comment="fix secure">
<change afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Entry.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/repository/EntryRepository.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/EntryService.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/impl/EntryServiceImpl.java" afterDir="false" />
<list default="true" id="067ac1f0-be04-4fe4-85c6-f870334053b8" name="Changes" comment="add Entiries table and bugfix">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/AdminController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/AdminController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/AuthController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Employee.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Employee.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/service/EmployeeService.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/EmployeeService.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/service/impl/EmployeeServiceImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Code.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Code.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Entry.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/entity/Entry.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/example/nto/repository/CodeRepository.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/example/nto/repository/CodeRepository.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/data.sql" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/data.sql" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -44,7 +40,7 @@
</component>
<component name="PropertiesComponent"><![CDATA[{
"keyToString": {
"Application.App.executor": "Debug",
"Application.App.executor": "Run",
"Maven.NTO-2024 [org.apache.maven.plugins:maven-clean-plugin:3.1.0:clean].executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"RunOnceActivity.git.unshallow": "true",
@ -174,7 +170,15 @@
<option name="project" value="LOCAL" />
<updated>1740038543344</updated>
</task>
<option name="localTasksCounter" value="10" />
<task id="LOCAL-00010" summary="add Entiries table and bugfix">
<option name="closed" value="true" />
<created>1740043625151</created>
<option name="number" value="00010" />
<option name="presentableId" value="LOCAL-00010" />
<option name="project" value="LOCAL" />
<updated>1740043625151</updated>
</task>
<option name="localTasksCounter" value="11" />
<servers />
</component>
<component name="VcsManagerConfiguration">
@ -187,14 +191,15 @@
<MESSAGE value="AUTHORIZATION WORK!" />
<MESSAGE value="create new endpoints (employee edit)" />
<MESSAGE value="fix secure" />
<option name="LAST_COMMIT_MESSAGE" value="fix secure" />
<MESSAGE value="add Entiries table and bugfix" />
<option name="LAST_COMMIT_MESSAGE" value="add Entiries table and bugfix" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java</url>
<line>16</line>
<line>14</line>
<option name="timeStamp" value="7" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
@ -209,12 +214,12 @@
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java</url>
<line>117</line>
<line>92</line>
<option name="timeStamp" value="43" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/com/example/nto/controller/EmployeeController.java</url>
<line>106</line>
<line>80</line>
<option name="timeStamp" value="44" />
</line-breakpoint>
</breakpoints>

View File

@ -23,6 +23,18 @@ import java.util.List;
@RestController
public class AuthController {
class RoleResponse {
private String role;
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
@Autowired
private EmployeeRepository employeeRepository;
@ -56,7 +68,7 @@ public class AuthController {
Эндпоинт авторизации, кинь сюда логин и пароль, и возможно я дам тебе возможность авторизоваться.
*/
@PostMapping("/api/login/")
private ResponseEntity<String> login(HttpServletRequest request, @RequestBody LoginBody loginBody) { //, @RequestParam String login, @RequestParam String password) {
private RoleResponse login(HttpServletRequest request, @RequestBody LoginBody loginBody) { //, @RequestParam String login, @RequestParam String password) {
Employee employee = employeeRepository.getByLogin(loginBody.login).get();
List<GrantedAuthority> authorities = new ArrayList<>();
@ -68,7 +80,9 @@ public class AuthController {
securityContext.setAuthentication(authentication);
HttpSession session = request.getSession(true);
session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
return ResponseEntity.status(HttpStatus.OK).build();
RoleResponse response = new RoleResponse();
response.setRole(employee.getRole());
return response;
}
}

View File

@ -11,8 +11,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class EmployeeController {
private final EmployeeService employeeService;
@ -31,27 +29,6 @@ public class EmployeeController {
this.entryRepository = entryRepository;
}
public static class EmployeeInfoBody {
private long id;
private List<Entry> entryList;
public List<Entry> getEntryList() {
return entryList;
}
public void setEntryList(List<Entry> entryList) {
this.entryList = entryList;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
}
/**
* Эндпоинт для проверки присутствия логина в емплоеееее таблице
*
@ -79,13 +56,10 @@ public class EmployeeController {
* Удален вариант получить 401 статус,так как теперь до этого эндпоинта нельзя достучаться без авторизации
*/
@GetMapping("/api/info")
public EmployeeInfoBody getEmployeeInfo() {
public Employee getEmployeeInfo() {
String login = SecurityContextHolder.getContext().getAuthentication().getName();
Employee employee = employeeService.getEmployeeInfo(login).get();
EmployeeInfoBody employeeInfoBody = new EmployeeInfoBody();
employeeInfoBody.setId(employee);
employeeInfoBody.setEntryList(entryService.getEntriesByEmployee(employee));
return employeeInfoBody;
return employee;
}
/**
@ -111,6 +85,7 @@ public class EmployeeController {
Entry entry = new Entry();
Employee employee = employeeService.getEmployeeByLogin(login);
entry.setEmployee(employee);
entry.setPlace(codeRepository.findByValue(value));
entryRepository.save(entry);
return ResponseEntity.status(HttpStatus.ACCEPTED).build();

View File

@ -1,11 +1,17 @@
package com.example.nto.entity;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
@Entity
@Data
@ -18,6 +24,11 @@ public class Code {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private long value;
private String name;
@OneToMany(mappedBy = "place")
@JsonIgnore
private List<Entry> entries = new ArrayList<>();
public long getId() {
return id;
@ -34,4 +45,12 @@ public class Code {
public void setValue(long value) {
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -1,5 +1,9 @@
package com.example.nto.entity;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import javax.persistence.*;
@Entity
@ -10,9 +14,14 @@ public class Entry {
private long id;
@ManyToOne
@JoinColumn(name = "employee")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityReference(alwaysAsId = true)
private Employee employee;
@ManyToOne
@JoinColumn(name = "place")
private Code place;
public long getId() {
return id;
}
@ -28,4 +37,12 @@ public class Entry {
public void setEmployee(Employee employee) {
this.employee = employee;
}
public Code getPlace() {
return place;
}
public void setPlace(Code place) {
this.place = place;
}
}

View File

@ -5,4 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
public interface CodeRepository extends JpaRepository<Code, Long> {
boolean existsByValue(Long value);
Code findByValue(Long value);
}

View File

@ -5,10 +5,10 @@ VALUES
(3, 'asemenov', '$2a$10$PRWHGoiil0XIipjWzu0MK.vMAxkdyoZQQliLtGh1TUhik7MyN2mje', 'USER', 'Семенов Анатолий Анатольевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Разработчик', '2024-02-13T08:31'),
(4, 'afedorov', '$2a$10$PRWHGoiil0XIipjWzu0MK.vMAxkdyoZQQliLtGh1TUhik7MyN2mje', 'USER', 'Федоров Александр Сергеевич', 'https://funnyducks.ru/upload/iblock/0cd/0cdeb7ec3ed6fddda0f90fccee05557d.jpg', 'Тестировщик', '2024-02-12T08:36');
INSERT INTO code (value)
INSERT INTO code (value, name)
VALUES
(1234567890123456789),
(9223372036854775807),
(1122334455667788990),
(998877665544332211),
(5566778899001122334);
(1234567890123456789, 'Комната Лимасова'),
(9223372036854775807, 'Компьютерный класс'),
(1122334455667788990, 'Туалет'),
(998877665544332211, 'Ядерный реактор'),
(5566778899001122334, 'Склад');