Added AccountController
This commit is contained in:
parent
2ee1d18360
commit
5113358181
@ -0,0 +1,24 @@
|
||||
package ru.dragonestia.picker.controller;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.dragonestia.picker.api.model.account.ResponseAccount;
|
||||
import ru.dragonestia.picker.model.Account;
|
||||
import ru.dragonestia.picker.service.AccountService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/accounts")
|
||||
@RequiredArgsConstructor
|
||||
public class AccountsController {
|
||||
|
||||
private final AccountService accountService;
|
||||
|
||||
@GetMapping("/current")
|
||||
ResponseAccount currentAccount() {
|
||||
var account = (Account) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
|
||||
return account.toResponseObject();
|
||||
}
|
||||
}
|
||||
@ -3,12 +3,16 @@ package ru.dragonestia.picker.model;
|
||||
import org.jetbrains.annotations.Contract;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
import ru.dragonestia.picker.api.model.account.IAccount;
|
||||
import ru.dragonestia.picker.api.model.account.ResponseAccount;
|
||||
|
||||
import java.beans.Transient;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Account implements UserDetails {
|
||||
public class Account implements IAccount, UserDetails {
|
||||
|
||||
private final String username;
|
||||
private final String lowerUsername;
|
||||
@ -24,10 +28,15 @@ public class Account implements UserDetails {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Permission> getAuthorities() {
|
||||
public @NotNull Collection<Permission> getAuthorities() {
|
||||
return permissions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLocked() {
|
||||
return locked;
|
||||
}
|
||||
|
||||
@Contract("_ -> this")
|
||||
public @NotNull Account setAuthorities(@NotNull Set<Permission> permissions) {
|
||||
this.permissions = permissions;
|
||||
@ -35,10 +44,15 @@ public class Account implements UserDetails {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getPassword() {
|
||||
public @NotNull String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<String> getPermissions() {
|
||||
return getAuthorities().stream().map(Enum::name).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
@Contract("_ -> this")
|
||||
public @NotNull Account setPassword(String value) {
|
||||
password = value;
|
||||
@ -96,4 +110,8 @@ public class Account implements UserDetails {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public @NotNull ResponseAccount toResponseObject() {
|
||||
return new ResponseAccount(username, password, getPermissions(), locked);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,16 +3,10 @@ package ru.dragonestia.picker.model;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
|
||||
public enum Permission implements GrantedAuthority {
|
||||
ADMIN("admin");
|
||||
|
||||
private final String id;
|
||||
|
||||
Permission(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
ADMIN;
|
||||
|
||||
@Override
|
||||
public String getAuthority() {
|
||||
return id;
|
||||
return name();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user