end-points login and getAllPassing added
This commit is contained in:
parent
06fca48446
commit
96cf747f8b
@ -26,7 +26,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
.csrf().disable()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/h2-console/**").permitAll()
|
||||
.antMatchers("api/{username}/passing").hasAuthority("ROLE_ADMIN")
|
||||
.antMatchers("api/**").hasAuthority("ROLE_ADMIN")
|
||||
.antMatchers("api/{username}/**").hasAnyAuthority("ROLE_USER", "ROLE_ADMIN")
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
|
@ -10,41 +10,42 @@ import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("api/{username}")
|
||||
@RequestMapping("api")
|
||||
@RequiredArgsConstructor
|
||||
public class UserController {
|
||||
|
||||
private final UserService userService;
|
||||
private final PassingService passingService;
|
||||
|
||||
@GetMapping("/auth")
|
||||
@GetMapping("/{username}/auth")
|
||||
public ResponseEntity<String> getUserByUsername(@PathVariable String username) {
|
||||
UserDTO userDTO = userService.getUserByUsername(username);
|
||||
return ResponseEntity.ok("данный логин существует - можно пользоваться приложением");
|
||||
}
|
||||
|
||||
@GetMapping("/info")
|
||||
@GetMapping("/{username}/info")
|
||||
public ResponseEntity<UserDTO> getUserInfoByUsername(@PathVariable String username) {
|
||||
return ResponseEntity.ok(userService.getUserInfoByUsername(username));
|
||||
}
|
||||
|
||||
@PatchMapping("/open")
|
||||
@PatchMapping("/{username}/open")
|
||||
public ResponseEntity<String> patchUserByUsername(@PathVariable String username) {
|
||||
UserDTO userDTO = userService.patchUserByUsername(username);
|
||||
return ResponseEntity.ok("дверь открылась");
|
||||
}
|
||||
|
||||
@GetMapping("/passing")
|
||||
@GetMapping("/{username}/passing")
|
||||
public List<PassingDTO> getPassingByUsername(@PathVariable String username) {
|
||||
return passingService.getPassingByUsername(username);
|
||||
}
|
||||
|
||||
@GetMapping("/passing/paginated")
|
||||
@GetMapping("/{username}/passing/paginated")
|
||||
public ResponseEntity<Page<PassingDTO>> getAllPassingPaginated(
|
||||
@RequestParam(defaultValue = "0") int page,
|
||||
@RequestParam(defaultValue = "1") int size) {
|
||||
@ -52,8 +53,18 @@ public class UserController {
|
||||
return ResponseEntity.ok(passingService.getAllPassingPaginated(pageable));
|
||||
}
|
||||
|
||||
@PostMapping("/passing/new")
|
||||
@PostMapping("/{username/passing/new")
|
||||
public ResponseEntity<PassingDTO> createPassing(@RequestBody PassingDTO dto) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(passingService.createPassing(dto));
|
||||
}
|
||||
|
||||
@GetMapping("/passing")
|
||||
public List<PassingDTO> getAllPassing() {
|
||||
return passingService.getAllPassing();
|
||||
}
|
||||
|
||||
@GetMapping("/login")
|
||||
public ResponseEntity<UserDTO> login(Authentication authentication){
|
||||
return ResponseEntity.ok(userService.getUserByUsername(authentication.getName()));
|
||||
}
|
||||
}
|
||||
|
@ -9,5 +9,6 @@ public class UserDTO {
|
||||
private String name;
|
||||
private String photo;
|
||||
private String position;
|
||||
private String authority;
|
||||
// private String lastVisit;
|
||||
}
|
||||
|
@ -33,6 +33,9 @@ public class User implements UserDetails{
|
||||
@Column(name = "position")
|
||||
private String position;
|
||||
|
||||
@Column(name = "authorities")
|
||||
private String authority;
|
||||
|
||||
// @Column(name = "lastVisit")
|
||||
// private String lastVisit;
|
||||
|
||||
|
@ -0,0 +1,7 @@
|
||||
package org.example.exception;
|
||||
|
||||
public class AuthorityNotFoundException extends RuntimeException {
|
||||
public AuthorityNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package org.example.exception.handler;
|
||||
|
||||
import org.example.exception.AuthorityNotFoundException;
|
||||
import org.example.exception.UserNotFoundException;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@ -12,4 +13,9 @@ public class GlobalExceptionHandler extends RuntimeException {
|
||||
public ResponseEntity<String> handleUserNotFoundException(UserNotFoundException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
|
||||
@ExceptionHandler(AuthorityNotFoundException.class)
|
||||
public ResponseEntity<String> handleAuthorityNotFoundException(AuthorityNotFoundException e) {
|
||||
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface UserRepository extends JpaRepository<User, Long> {
|
||||
Optional<User> findByAuthority(String authority);
|
||||
|
||||
Optional<User> findByUsername(String username);
|
||||
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import org.springframework.data.domain.Pageable;
|
||||
import java.util.List;
|
||||
|
||||
public interface PassingService {
|
||||
List<PassingDTO> getAllPassing();
|
||||
|
||||
List<PassingDTO> getPassingByUsername(String username);
|
||||
|
||||
Page<PassingDTO> getAllPassingPaginated(Pageable pageable);
|
||||
|
@ -3,8 +3,11 @@ package org.example.service.impl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.example.dto.PassingDTO;
|
||||
import org.example.entity.Passing;
|
||||
import org.example.entity.User;
|
||||
import org.example.exception.AuthorityNotFoundException;
|
||||
import org.example.exception.UserNotFoundException;
|
||||
import org.example.repository.PassingRepository;
|
||||
import org.example.repository.UserRepository;
|
||||
import org.example.service.PassingService;
|
||||
import org.example.util.PassingMapper;
|
||||
import org.springframework.data.domain.Page;
|
||||
@ -12,12 +15,25 @@ import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class PassingServiceImpl implements PassingService {
|
||||
private final PassingRepository passingRepository;
|
||||
private final UserRepository userRepository;
|
||||
|
||||
@Override
|
||||
public List<PassingDTO> getAllPassing() {
|
||||
Optional<User> userOptional = userRepository.findByAuthority("ROLE_ADMIN");
|
||||
if (userOptional.isEmpty()) {
|
||||
throw new AuthorityNotFoundException("Нет прав");
|
||||
}
|
||||
|
||||
return passingRepository.findAll().stream().map(PassingMapper::convertToDto).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PassingDTO> getPassingByUsername(String Username) {
|
||||
return passingRepository.findByUsername(Username).stream().map(PassingMapper::convertToDto).collect(Collectors.toList());
|
||||
|
@ -13,6 +13,7 @@ public class UserMapper {
|
||||
dto.setName(user.getName());
|
||||
dto.setPhoto(user.getPhoto());
|
||||
dto.setPosition(user.getPosition());
|
||||
dto.setAuthority(user.getAuthority());
|
||||
// dto.setLastVisit(user.getLastVisit());
|
||||
|
||||
return dto;
|
||||
|
Loading…
x
Reference in New Issue
Block a user