implemented account controller
This commit is contained in:
parent
8361b52d81
commit
19c672fd3a
@ -3,32 +3,44 @@ package ru.dragonestia.picker.controller;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.dragonestia.picker.controller.response.ResponseObject;
|
||||
import ru.dragonestia.picker.exception.DoesNotExistsException;
|
||||
import ru.dragonestia.picker.model.account.Account;
|
||||
import ru.dragonestia.picker.model.account.AccountId;
|
||||
import ru.dragonestia.picker.model.account.Permission;
|
||||
import ru.dragonestia.picker.service.AccountService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/accounts")
|
||||
@RequiredArgsConstructor
|
||||
public class AccountsController {
|
||||
|
||||
private final AccountService accountService;
|
||||
private final PasswordEncoder passwordEncoder;
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@GetMapping
|
||||
List<String> listAccounts() {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
return accountService.allAccounts().stream().map(account -> account.getId().getValue()).toList();
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@GetMapping("/target/{accountId}")
|
||||
ResponseObject.Account targetAccountDetails(@PathVariable String accountId) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
return accountService.findAccount(AccountId.of(accountId))
|
||||
.map(ResponseObject.Account::of)
|
||||
.orElseThrow(() -> DoesNotExistsException.forAccount(AccountId.of(accountId)));
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@GetMapping("/list")
|
||||
ResponseObject.Account listAccountsDetails(@RequestParam List<String> id) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
List<ResponseObject.Account> listAccountsDetails(@RequestParam List<String> id) {
|
||||
return id.stream().map(this::targetAccountDetails).toList();
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@ -36,25 +48,45 @@ public class AccountsController {
|
||||
ResponseEntity<Void> createAccount(@RequestParam String username,
|
||||
@RequestParam String password,
|
||||
@RequestParam List<String> permissions) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
var account = accountService.createNewAccount(AccountId.of(username), password);
|
||||
setPermissions(account, permissions);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@DeleteMapping("/target/{accountId}")
|
||||
ResponseEntity<Void> removeAccount(@PathVariable String accountId) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
accountService.findAccount(AccountId.of(accountId)).ifPresent(accountService::removeAccount);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
@PutMapping("/target/{accountId}/permissions")
|
||||
ResponseEntity<Void> setPermissions(@PathVariable String accountId,
|
||||
@RequestParam List<String> permissions) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
ResponseEntity<Void> setPermissions(@PathVariable String accountId, @RequestParam List<String> permissions) {
|
||||
var account = accountService.findAccount(AccountId.of(accountId))
|
||||
.orElseThrow(() -> DoesNotExistsException.forAccount(AccountId.of(accountId)));
|
||||
setPermissions(account, permissions);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN') || principal.username.equals(accountId)")
|
||||
@PutMapping("/target/{accountId}/password")
|
||||
ResponseEntity<?> changePassword(@PathVariable String accountId, @RequestParam String newPassword) {
|
||||
throw new UnsupportedOperationException("Not implemented");
|
||||
var account = accountService.findAccount(AccountId.of(accountId))
|
||||
.orElseThrow(() -> DoesNotExistsException.forAccount(AccountId.of(accountId)));
|
||||
account.setPassword(passwordEncoder.encode(newPassword));
|
||||
accountService.updateState(account);
|
||||
return ResponseEntity.ok().build();
|
||||
}
|
||||
|
||||
private void setPermissions(Account account, List<String> permissions) {
|
||||
account.setAuthorities(permissions.stream().map(permission -> {
|
||||
try {
|
||||
return Permission.valueOf(permission);
|
||||
} catch (IllegalArgumentException ex) {
|
||||
throw DoesNotExistsException.forPermission(permission);
|
||||
}
|
||||
}).collect(Collectors.toSet()));
|
||||
accountService.updateState(account);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
package ru.dragonestia.picker.controller.response;
|
||||
|
||||
import ru.dragonestia.picker.model.entity.EntityId;
|
||||
import ru.dragonestia.picker.model.instance.type.PickingMethod;
|
||||
|
||||
import java.util.List;
|
||||
@ -30,5 +29,10 @@ public final class ResponseObject {
|
||||
|
||||
public record PickedRoom(Room room, List<String> entities) {}
|
||||
|
||||
public record Account(String id, List<String> permissions, boolean locked) {}
|
||||
public record Account(String id, List<String> permissions, boolean locked) {
|
||||
|
||||
public static Account of(ru.dragonestia.picker.model.account.Account account) {
|
||||
return new Account(account.getUsername(), account.getAuthorities().stream().map(Enum::name).toList(), account.isLocked());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package ru.dragonestia.picker.exception;
|
||||
|
||||
import ru.dragonestia.picker.model.account.AccountId;
|
||||
import ru.dragonestia.picker.model.instance.InstanceId;
|
||||
import ru.dragonestia.picker.model.room.RoomId;
|
||||
|
||||
@ -16,4 +17,12 @@ public class DoesNotExistsException extends RuntimeException {
|
||||
public static DoesNotExistsException forRoom(RoomId id) {
|
||||
return new DoesNotExistsException("Does not exists room with id '%s'".formatted(id.toString()));
|
||||
}
|
||||
|
||||
public static DoesNotExistsException forAccount(AccountId id) {
|
||||
return new DoesNotExistsException("Does not exists account with id '%s'".formatted(id.toString()));
|
||||
}
|
||||
|
||||
public static DoesNotExistsException forPermission(String permission) {
|
||||
return new DoesNotExistsException("Does not exists permission '%s'".formatted(permission));
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ public interface AccountService extends UserDetailsService {
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
Account createNewAccount(AccountId id, String password);
|
||||
|
||||
Optional<Account> findAccount(String accountId);
|
||||
Optional<Account> findAccount(AccountId id);
|
||||
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
Collection<Account> allAccounts();
|
||||
|
||||
@ -43,8 +43,8 @@ public class AccountServiceImpl implements AccountService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Account> findAccount(String accountId) {
|
||||
return Optional.ofNullable(accounts.getOrDefault(accountId, null));
|
||||
public Optional<Account> findAccount(AccountId id) {
|
||||
return Optional.ofNullable(accounts.getOrDefault(id.getValue(), null));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user