implemented account controller

This commit is contained in:
Andrey Terentev 2024-05-11 12:27:57 +07:00 committed by Andrey Terentev
parent 8361b52d81
commit 19c672fd3a
5 changed files with 60 additions and 15 deletions

View File

@ -3,32 +3,44 @@ package ru.dragonestia.picker.controller;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import ru.dragonestia.picker.controller.response.ResponseObject; 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.List;
import java.util.stream.Collectors;
@RestController @RestController
@RequestMapping("/accounts") @RequestMapping("/accounts")
@RequiredArgsConstructor @RequiredArgsConstructor
public class AccountsController { public class AccountsController {
private final AccountService accountService;
private final PasswordEncoder passwordEncoder;
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
@GetMapping @GetMapping
List<String> listAccounts() { List<String> listAccounts() {
throw new UnsupportedOperationException("Not implemented"); return accountService.allAccounts().stream().map(account -> account.getId().getValue()).toList();
} }
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
@GetMapping("/target/{accountId}") @GetMapping("/target/{accountId}")
ResponseObject.Account targetAccountDetails(@PathVariable String 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')") @PreAuthorize("hasRole('ADMIN')")
@GetMapping("/list") @GetMapping("/list")
ResponseObject.Account listAccountsDetails(@RequestParam List<String> id) { List<ResponseObject.Account> listAccountsDetails(@RequestParam List<String> id) {
throw new UnsupportedOperationException("Not implemented"); return id.stream().map(this::targetAccountDetails).toList();
} }
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
@ -36,25 +48,45 @@ public class AccountsController {
ResponseEntity<Void> createAccount(@RequestParam String username, ResponseEntity<Void> createAccount(@RequestParam String username,
@RequestParam String password, @RequestParam String password,
@RequestParam List<String> permissions) { @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')") @PreAuthorize("hasRole('ADMIN')")
@DeleteMapping("/target/{accountId}") @DeleteMapping("/target/{accountId}")
ResponseEntity<Void> removeAccount(@PathVariable String 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')") @PreAuthorize("hasRole('ADMIN')")
@PutMapping("/target/{accountId}/permissions") @PutMapping("/target/{accountId}/permissions")
ResponseEntity<Void> setPermissions(@PathVariable String accountId, ResponseEntity<Void> setPermissions(@PathVariable String accountId, @RequestParam List<String> permissions) {
@RequestParam List<String> permissions) { var account = accountService.findAccount(AccountId.of(accountId))
throw new UnsupportedOperationException("Not implemented"); .orElseThrow(() -> DoesNotExistsException.forAccount(AccountId.of(accountId)));
setPermissions(account, permissions);
return ResponseEntity.ok().build();
} }
@PreAuthorize("hasRole('ADMIN') || principal.username.equals(accountId)") @PreAuthorize("hasRole('ADMIN') || principal.username.equals(accountId)")
@PutMapping("/target/{accountId}/password") @PutMapping("/target/{accountId}/password")
ResponseEntity<?> changePassword(@PathVariable String accountId, @RequestParam String newPassword) { 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);
} }
} }

View File

@ -1,6 +1,5 @@
package ru.dragonestia.picker.controller.response; package ru.dragonestia.picker.controller.response;
import ru.dragonestia.picker.model.entity.EntityId;
import ru.dragonestia.picker.model.instance.type.PickingMethod; import ru.dragonestia.picker.model.instance.type.PickingMethod;
import java.util.List; import java.util.List;
@ -30,5 +29,10 @@ public final class ResponseObject {
public record PickedRoom(Room room, List<String> entities) {} 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());
}
}
} }

View File

@ -1,5 +1,6 @@
package ru.dragonestia.picker.exception; package ru.dragonestia.picker.exception;
import ru.dragonestia.picker.model.account.AccountId;
import ru.dragonestia.picker.model.instance.InstanceId; import ru.dragonestia.picker.model.instance.InstanceId;
import ru.dragonestia.picker.model.room.RoomId; import ru.dragonestia.picker.model.room.RoomId;
@ -16,4 +17,12 @@ public class DoesNotExistsException extends RuntimeException {
public static DoesNotExistsException forRoom(RoomId id) { public static DoesNotExistsException forRoom(RoomId id) {
return new DoesNotExistsException("Does not exists room with id '%s'".formatted(id.toString())); 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));
}
} }

View File

@ -14,7 +14,7 @@ public interface AccountService extends UserDetailsService {
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
Account createNewAccount(AccountId id, String password); Account createNewAccount(AccountId id, String password);
Optional<Account> findAccount(String accountId); Optional<Account> findAccount(AccountId id);
@PreAuthorize("hasRole('ADMIN')") @PreAuthorize("hasRole('ADMIN')")
Collection<Account> allAccounts(); Collection<Account> allAccounts();

View File

@ -43,8 +43,8 @@ public class AccountServiceImpl implements AccountService {
} }
@Override @Override
public Optional<Account> findAccount(String accountId) { public Optional<Account> findAccount(AccountId id) {
return Optional.ofNullable(accounts.getOrDefault(accountId, null)); return Optional.ofNullable(accounts.getOrDefault(id.getValue(), null));
} }
@Override @Override