User: update, auth processing, get id
This commit is contained in:
parent
12fbacc765
commit
fddb65868c
6 changed files with 141 additions and 11 deletions
|
|
@ -20,6 +20,7 @@ import ru.spcex.clearing.backendapi.controller.request.cud.utilities.UserAuthAct
|
||||||
import ru.spcex.clearing.backendapi.controller.response.BasicSpcexResponse;
|
import ru.spcex.clearing.backendapi.controller.response.BasicSpcexResponse;
|
||||||
import ru.spcex.clearing.backendapi.controller.response.cud.CudResponse;
|
import ru.spcex.clearing.backendapi.controller.response.cud.CudResponse;
|
||||||
import ru.spcex.clearing.backendapi.controller.response.entity.CommonGetAllResponse;
|
import ru.spcex.clearing.backendapi.controller.response.entity.CommonGetAllResponse;
|
||||||
|
import ru.spcex.clearing.backendapi.controller.response.entity.user.UserIdResponse;
|
||||||
import ru.spcex.clearing.backendapi.security.KeycloakUtils;
|
import ru.spcex.clearing.backendapi.security.KeycloakUtils;
|
||||||
import ru.spcex.clearing.backendapi.service.IOperator;
|
import ru.spcex.clearing.backendapi.service.IOperator;
|
||||||
import ru.spcex.clearing.backendapi.service.IStateLoader;
|
import ru.spcex.clearing.backendapi.service.IStateLoader;
|
||||||
|
|
@ -59,6 +60,23 @@ public class UserController extends AbstractQueueController {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "get my id.")
|
||||||
|
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = UserIdResponse.class)})
|
||||||
|
@RequestMapping(path = "get-my-id", method = RequestMethod.GET)
|
||||||
|
@ResponseBody
|
||||||
|
public UserIdResponse getId() {
|
||||||
|
UserIdResponse response = new UserIdResponse();
|
||||||
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
|
String username = KeycloakUtils.getUserNameFromAuthentication(authentication);
|
||||||
|
User user = userImdg.getSingleObjectByFieldValues(Map.of("identifier", username));
|
||||||
|
if (user == null) {
|
||||||
|
log.info("getId user {} authenticated, but User object was not created", username);
|
||||||
|
throw new IllegalStateException("couldn't return user id");
|
||||||
|
}
|
||||||
|
response.getPayload().setId(user.getId());
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@ApiOperation(value = "update user")
|
@ApiOperation(value = "update user")
|
||||||
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = CudResponse.class), @ApiResponse(code = 400, message = "Ошибка валидации", response = BasicSpcexResponse.class)})
|
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = CudResponse.class), @ApiResponse(code = 400, message = "Ошибка валидации", response = BasicSpcexResponse.class)})
|
||||||
|
|
@ -72,10 +90,10 @@ public class UserController extends AbstractQueueController {
|
||||||
log.warn("user {} sended update request for user {}", username, userUpdateAction.getUsername());
|
log.warn("user {} sended update request for user {}", username, userUpdateAction.getUsername());
|
||||||
throw new IllegalStateException("cannot perform delete action for " + username);
|
throw new IllegalStateException("cannot perform delete action for " + username);
|
||||||
}
|
}
|
||||||
User user = userImdg.getSingleObjectByFieldValues(Map.of("identifier", username));
|
// User user = userImdg.getSingleObjectByFieldValues(Map.of("identifier", username));
|
||||||
if (user == null) {
|
// if (user == null) {
|
||||||
log.info("user {} authenticated, but User object was not created", username);
|
// log.info("update user {} authenticated, but User object was not created", username);
|
||||||
}
|
// }
|
||||||
return processRequest(Consts.USER_UPDATE, userUpdateAction);
|
return processRequest(Consts.USER_UPDATE, userUpdateAction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
package ru.spcex.clearing.backendapi.controller.response.entity.user;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
import io.swagger.annotations.ApiModel;
|
||||||
|
import io.swagger.annotations.ApiModelProperty;
|
||||||
|
import ru.spcex.clearing.backendapi.controller.response.BasicSpcexResponse;
|
||||||
|
|
||||||
|
@ApiModel(description = "Ответ содержащий id текущего пользователя.")
|
||||||
|
public class UserIdResponse extends BasicSpcexResponse {
|
||||||
|
|
||||||
|
@JsonProperty
|
||||||
|
@ApiModelProperty(value = "Полезная нагрузка")
|
||||||
|
private UserIdPayload payload = new UserIdPayload();
|
||||||
|
|
||||||
|
public UserIdPayload getPayload() {
|
||||||
|
return payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPayload(UserIdPayload payload) {
|
||||||
|
this.payload = payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class UserIdPayload {
|
||||||
|
@JsonProperty
|
||||||
|
@ApiModelProperty(value = "Id текущего пользователя")
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -3,6 +3,7 @@ package ru.spcex.clearing.backendapi.security.handlers;
|
||||||
import org.keycloak.KeycloakPrincipal;
|
import org.keycloak.KeycloakPrincipal;
|
||||||
import org.keycloak.adapters.spi.KeycloakAccount;
|
import org.keycloak.adapters.spi.KeycloakAccount;
|
||||||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
|
||||||
|
import org.keycloak.representations.AccessToken;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
|
@ -34,16 +35,24 @@ public class AuthAuditSuccessLoginHandler implements AuthenticationSuccessHandle
|
||||||
try {
|
try {
|
||||||
KeycloakAuthenticationToken auth = (KeycloakAuthenticationToken) authentication;
|
KeycloakAuthenticationToken auth = (KeycloakAuthenticationToken) authentication;
|
||||||
KeycloakAccount details = (KeycloakAccount) auth.getDetails();
|
KeycloakAccount details = (KeycloakAccount) auth.getDetails();
|
||||||
String username = ((KeycloakPrincipal) details.getPrincipal())
|
KeycloakPrincipal<?> principal = (KeycloakPrincipal<?>) details.getPrincipal();
|
||||||
.getKeycloakSecurityContext()
|
String username = principal.getKeycloakSecurityContext()
|
||||||
.getToken()
|
.getToken()
|
||||||
.getPreferredUsername();
|
.getPreferredUsername();
|
||||||
Set<String> roles = details.getRoles();
|
Set<String> roles = details.getRoles();
|
||||||
|
|
||||||
if (roles.contains(Admin.getKey()) || roles.contains(Security.getKey()) || roles.contains(Superviser.getKey())) {
|
if (roles.contains(Admin.getKey()) || roles.contains(Security.getKey()) || roles.contains(Superviser.getKey())) {
|
||||||
|
AccessToken token = principal.getKeycloakSecurityContext().getToken();
|
||||||
String clientIp = request != null ? request.getRemoteAddr() : null;
|
String clientIp = request != null ? request.getRemoteAddr() : null;
|
||||||
String serverIp = request != null ? request.getLocalAddr() : null;
|
String serverIp = request != null ? request.getLocalAddr() : null;
|
||||||
userAutoCreation.sendAuthSuccessEvent(username, roles, clientIp, serverIp);
|
userAutoCreation.sendAuthSuccessEvent(username,
|
||||||
|
roles,
|
||||||
|
clientIp,
|
||||||
|
serverIp,
|
||||||
|
token.getEmail(),
|
||||||
|
token.getGivenName(),
|
||||||
|
token.getFamilyName(),
|
||||||
|
token.getMiddleName());
|
||||||
}
|
}
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
log.error(ExceptionUtils.getStackTrace(e));
|
log.error(ExceptionUtils.getStackTrace(e));
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,8 @@ public class UserAuthProcessor {
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendAuthSuccessEvent(String identifier, Collection<String> roles, String userIp, String serverIp) {
|
public void sendAuthSuccessEvent(String identifier, Collection<String> roles, String userIp, String serverIp,
|
||||||
|
String email, String name, String surname, String middleName) {
|
||||||
UserAuthSystemAction authEvent = new UserAuthSystemAction();
|
UserAuthSystemAction authEvent = new UserAuthSystemAction();
|
||||||
UserAuthRequest requestData = authEvent.toRequest();
|
UserAuthRequest requestData = authEvent.toRequest();
|
||||||
requestData.setUsername(identifier);
|
requestData.setUsername(identifier);
|
||||||
|
|
@ -41,6 +42,10 @@ public class UserAuthProcessor {
|
||||||
requestData.setRoles(new ArrayList<>(roles));
|
requestData.setRoles(new ArrayList<>(roles));
|
||||||
requestData.setServerIp(serverIp + ":" + serverPort); //getServerAddress() + ":" + serverPort
|
requestData.setServerIp(serverIp + ":" + serverPort); //getServerAddress() + ":" + serverPort
|
||||||
requestData.setClientIp(userIp);
|
requestData.setClientIp(userIp);
|
||||||
|
requestData.setEmail(email);
|
||||||
|
requestData.setName(name);
|
||||||
|
requestData.setSurname(surname);
|
||||||
|
requestData.setMiddleName(middleName);
|
||||||
try {
|
try {
|
||||||
operator.sendRequestToQueue(Consts.USER_AUTH_SUCCESS, authEvent);
|
operator.sendRequestToQueue(Consts.USER_AUTH_SUCCESS, authEvent);
|
||||||
} catch (Throwable e) { //ExecutionException | InterruptedException
|
} catch (Throwable e) { //ExecutionException | InterruptedException
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import java.time.LocalDate;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
@ -121,10 +122,28 @@ public class UserService extends QueueConsumer implements InitializingBean {
|
||||||
UserAuthRequest authInfo = authEvent.getRequestPayload();
|
UserAuthRequest authInfo = authEvent.getRequestPayload();
|
||||||
log.debug("user name={} authenticated", authInfo.getUsername());
|
log.debug("user name={} authenticated", authInfo.getUsername());
|
||||||
UserInfo userInfo = createUserIfNeeded(authInfo.getUsername());
|
UserInfo userInfo = createUserIfNeeded(authInfo.getUsername());
|
||||||
|
updatePersonalInfoIfNeeded(userInfo, authInfo.getEmail(), authInfo.getName(), authInfo.getSurname(), authInfo.getMiddleName());
|
||||||
changeRoles(userInfo.userId, userInfo.isNew, authInfo.getRoles());
|
changeRoles(userInfo.userId, userInfo.isNew, authInfo.getRoles());
|
||||||
connected(userInfo.userId, authInfo.getTime(), authInfo.getClientIp(), authInfo.getServerIp());
|
connected(userInfo.userId, authInfo.getTime(), authInfo.getClientIp(), authInfo.getServerIp());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void updatePersonalInfoIfNeeded(UserInfo userInfo, String email, String name, String surname, String middleName) {
|
||||||
|
User user = userInfo.user;
|
||||||
|
if (!Objects.equals(user.getEmail(), email)
|
||||||
|
|| !Objects.equals(user.getName(), name)
|
||||||
|
|| !Objects.equals(user.getLastName(), surname)
|
||||||
|
|| !Objects.equals(user.getMiddleName(), middleName)) {
|
||||||
|
user.setEmail(email);
|
||||||
|
user.setName(name);
|
||||||
|
user.setLastName(surname);
|
||||||
|
user.setMiddleName(middleName);
|
||||||
|
if (!userInfo.isNew) {
|
||||||
|
user.setUpdated(Instant.now());
|
||||||
|
}
|
||||||
|
userMap.update(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void usersLogoutSuccess(BaseRequest<UserLogoutRequest> logoutEvent) {
|
private void usersLogoutSuccess(BaseRequest<UserLogoutRequest> logoutEvent) {
|
||||||
UserLogoutRequest logoutInfo = logoutEvent.getRequestPayload();
|
UserLogoutRequest logoutInfo = logoutEvent.getRequestPayload();
|
||||||
log.debug("user name={} logout", logoutInfo.getUsername());
|
log.debug("user name={} logout", logoutInfo.getUsername());
|
||||||
|
|
@ -152,9 +171,9 @@ public class UserService extends QueueConsumer implements InitializingBean {
|
||||||
user.setCreated(Instant.now());
|
user.setCreated(Instant.now());
|
||||||
user.setIdentifier(username);
|
user.setIdentifier(username);
|
||||||
userMap.insert(user);
|
userMap.insert(user);
|
||||||
return new UserInfo(true, user.getId());
|
return new UserInfo(true, user.getId(), user);
|
||||||
} else {
|
} else {
|
||||||
return new UserInfo(false, user.getId());
|
return new UserInfo(false, user.getId(), user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -223,10 +242,12 @@ public class UserService extends QueueConsumer implements InitializingBean {
|
||||||
private static class UserInfo {
|
private static class UserInfo {
|
||||||
boolean isNew;
|
boolean isNew;
|
||||||
Long userId;
|
Long userId;
|
||||||
|
User user;
|
||||||
|
|
||||||
public UserInfo(boolean isNew, Long userId) {
|
public UserInfo(boolean isNew, Long userId, User user) {
|
||||||
this.isNew = isNew;
|
this.isNew = isNew;
|
||||||
this.userId = userId;
|
this.userId = userId;
|
||||||
|
this.user = user;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,15 @@ public class UserAuthRequest {
|
||||||
private String serverIp;
|
private String serverIp;
|
||||||
@JsonProperty
|
@JsonProperty
|
||||||
private String clientIp;
|
private String clientIp;
|
||||||
|
@JsonProperty
|
||||||
|
private String email;
|
||||||
|
@JsonProperty
|
||||||
|
private String name;
|
||||||
|
@JsonProperty
|
||||||
|
private String surname;
|
||||||
|
@JsonProperty
|
||||||
|
private String middleName;
|
||||||
|
|
||||||
|
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
return username;
|
return username;
|
||||||
|
|
@ -63,4 +72,36 @@ public class UserAuthRequest {
|
||||||
public void setClientIp(String clientIp) {
|
public void setClientIp(String clientIp) {
|
||||||
this.clientIp = clientIp;
|
this.clientIp = clientIp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getEmail() {
|
||||||
|
return email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEmail(String email) {
|
||||||
|
this.email = email;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSurname() {
|
||||||
|
return surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSurname(String surname) {
|
||||||
|
this.surname = surname;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMiddleName() {
|
||||||
|
return middleName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMiddleName(String middleName) {
|
||||||
|
this.middleName = middleName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue