This commit is contained in:
parent
d147732b0e
commit
b9a50d158b
3 changed files with 60 additions and 35 deletions
|
|
@ -17,6 +17,7 @@ import ru.spcex.clearing.platform.messaging.domain.cud.utilities.UserLogoutReque
|
|||
import ru.spcex.clearing.platform.messaging.service.QueueConsumer;
|
||||
import ru.spcex.platform.enumeration.ConnectionState;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.enumeration.UserRole;
|
||||
import ru.spcex.platform.imdg.api.Imdg;
|
||||
import ru.spcex.platform.imdg.api.ImdgProvider;
|
||||
import ru.spcex.platform.utils.time.TimeUtil;
|
||||
|
|
@ -24,11 +25,20 @@ import ru.spcex.platform.utils.time.TimeUtil;
|
|||
import java.time.Instant;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
public class UserService extends QueueConsumer implements InitializingBean {
|
||||
private final static Map<String, UserRole> roleMapping = new HashMap<>();
|
||||
static {
|
||||
roleMapping.put("CS_MKR_ADMIN", UserRole.Admin);
|
||||
roleMapping.put("CS_MKR_SUPERVISER", UserRole.Superviser);
|
||||
roleMapping.put("CS_MKR_SECURITY", UserRole.Security);
|
||||
}
|
||||
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||
private final Imdg<User> userMap;
|
||||
private final Imdg<UserRoleSession> userRoleSessionImdg;
|
||||
|
|
@ -146,11 +156,16 @@ public class UserService extends QueueConsumer implements InitializingBean {
|
|||
}
|
||||
|
||||
static Collection<RoleAction> defineAllRoleChanges(Collection<UserRoleSession> presentRoles, Collection<String> authRoles) {
|
||||
Collection<String> finalAuthRoles = authRoles.stream().flatMap((Function<String, Stream<String>>) keycloakRole -> {
|
||||
UserRole userRole = roleMapping.get(keycloakRole);
|
||||
if (userRole == null) return Stream.empty();
|
||||
return Stream.of(userRole.getKey());
|
||||
}).collect(Collectors.toList());
|
||||
return Stream.concat(
|
||||
presentRoles
|
||||
.stream()
|
||||
.filter(presentRole -> !(Status.Active.equalsByKey(presentRole.getStatus()) && contain(presentRole, authRoles)))
|
||||
.filter(presentRole -> !(Status.Blocked.equalsByKey(presentRole.getStatus()) && !contain(presentRole, authRoles)))
|
||||
.filter(presentRole -> !(Status.Active.equalsByKey(presentRole.getStatus()) && contain(presentRole, finalAuthRoles)))
|
||||
.filter(presentRole -> !(Status.Blocked.equalsByKey(presentRole.getStatus()) && !contain(presentRole, finalAuthRoles)))
|
||||
.map(role -> {
|
||||
RoleAction roleAction = new RoleAction();
|
||||
roleAction.roleToChange = role;
|
||||
|
|
@ -161,7 +176,7 @@ public class UserService extends QueueConsumer implements InitializingBean {
|
|||
}
|
||||
return roleAction;
|
||||
}),
|
||||
authRoles
|
||||
finalAuthRoles
|
||||
.stream()
|
||||
.filter(authRole -> !contain(authRole, presentRoles))
|
||||
.map(authRole -> {
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package ru.spcex.clearing.utility.service;
|
|||
import org.junit.jupiter.api.Test;
|
||||
import ru.clearing.classes.statics.data.user.UserRoleSession;
|
||||
import ru.spcex.platform.enumeration.Status;
|
||||
import ru.spcex.platform.enumeration.UserRole;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
|
@ -18,69 +19,59 @@ class UserServiceChangeRolesTest {
|
|||
@Test
|
||||
public void testTwoNewRoles() {
|
||||
Collection<UserRoleSession> userRoles = new ArrayList<>();
|
||||
userRoles.add(generate("admin", Status.Active));
|
||||
userRoles.add(generate("broker", Status.Active));
|
||||
userRoles.add(generate("manager", Status.Blocked));
|
||||
Collection<String> methodsFromAuth = List.of("admin", "broker", "makler", "user");
|
||||
userRoles.add(generate(UserRole.Admin.getKey(), Status.Active));
|
||||
Collection<String> methodsFromAuth = List.of("CS_MKR_ADMIN", "CS_MKR_SUPERVISER", "CS_MKR_SECURITY");
|
||||
|
||||
Collection<UserService.RoleAction> roleActions = UserService.defineAllRoleChanges(userRoles, methodsFromAuth);
|
||||
assertEquals(2, roleActions.size());
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "makler".equals(roleAction.roleName)));
|
||||
&& UserRole.Superviser.getKey().equals(roleAction.roleName)));
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "user".equals(roleAction.roleName)));
|
||||
&& UserRole.Security.getKey().equals(roleAction.roleName)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOneMissingTwoNew() {
|
||||
public void testOneMissingOneNew() {
|
||||
Collection<UserRoleSession> userRoles = new ArrayList<>();
|
||||
userRoles.add(generate("admin", Status.Active));
|
||||
userRoles.add(generate("broker", Status.Active));
|
||||
userRoles.add(generate("manager", Status.Active));
|
||||
Collection<String> methodsFromAuth = List.of("admin", "broker", "makler", "user");
|
||||
userRoles.add(generate(UserRole.Admin.getKey(), Status.Active));
|
||||
userRoles.add(generate(UserRole.Superviser.getKey(), Status.Active));
|
||||
Collection<String> methodsFromAuth = List.of("CS_MKR_ADMIN", "CS_MKR_SECURITY");
|
||||
|
||||
Collection<UserService.RoleAction> roleActions = UserService.defineAllRoleChanges(userRoles, methodsFromAuth);
|
||||
assertEquals(3, roleActions.size());
|
||||
assertEquals(2, roleActions.size());
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> !roleAction.create()
|
||||
&& roleAction.roleToChange.getUserRole().equals("manager")
|
||||
&& UserRole.Superviser.getKey().equals(roleAction.roleToChange.getUserRole())
|
||||
&& roleAction.newStatus.equals(Status.Blocked)));
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "makler".equals(roleAction.roleName)));
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "user".equals(roleAction.roleName)));
|
||||
&& UserRole.Security.getKey().equals(roleAction.roleName)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAllNew() {
|
||||
Collection<UserRoleSession> userRoles = new ArrayList<>();
|
||||
Collection<String> methodsFromAuth = List.of("admin", "broker", "makler", "user");
|
||||
Collection<String> methodsFromAuth = List.of("CS_MKR_ADMIN", "CS_MKR_SUPERVISER", "CS_MKR_SECURITY", "unknownRole");
|
||||
|
||||
Collection<UserService.RoleAction> roleActions = UserService.defineAllRoleChanges(userRoles, methodsFromAuth);
|
||||
assertEquals(4, roleActions.size());
|
||||
assertEquals(3, roleActions.size());
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "admin".equals(roleAction.roleName)));
|
||||
&& UserRole.Admin.getKey().equals(roleAction.roleName)));
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "broker".equals(roleAction.roleName)));
|
||||
&& UserRole.Superviser.getKey().equals(roleAction.roleName)));
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "makler".equals(roleAction.roleName)));
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "user".equals(roleAction.roleName)));
|
||||
&& UserRole.Security.getKey().equals(roleAction.roleName)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTwoNewRolesDifferentCase() {
|
||||
public void testOneNewRolesDifferentCase() {
|
||||
Collection<UserRoleSession> userRoles = new ArrayList<>();
|
||||
userRoles.add(generate("AdMin", Status.Active));
|
||||
userRoles.add(generate("broKer", Status.Active));
|
||||
userRoles.add(generate("MANAGER", Status.Blocked));
|
||||
Collection<String> methodsFromAuth = List.of("admin", "broker", "makler", "user");
|
||||
userRoles.add(generate(UserRole.Admin.getKey(), Status.Active));
|
||||
userRoles.add(generate(UserRole.Superviser.getKey(), Status.Active));
|
||||
Collection<String> methodsFromAuth = List.of("CS_MKR_ADMIN", "CS_MKR_SUPERVISER", "CS_MKR_SECURITY", "Cs_mkr_superviser", "unknownRole");
|
||||
|
||||
Collection<UserService.RoleAction> roleActions = UserService.defineAllRoleChanges(userRoles, methodsFromAuth);
|
||||
assertEquals(2, roleActions.size());
|
||||
assertEquals(1, roleActions.size());
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "makler".equalsIgnoreCase(roleAction.roleName)));
|
||||
assertTrue(roleActions.stream().anyMatch(roleAction -> roleAction.create()
|
||||
&& "user".equalsIgnoreCase(roleAction.roleName)));
|
||||
&& UserRole.Security.getKey().equalsIgnoreCase(roleAction.roleName)));
|
||||
}
|
||||
|
||||
private static UserRoleSession generate(String roleName, Status roleStatus) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
package ru.spcex.platform.enumeration;
|
||||
|
||||
import ru.spcex.platform.utils.enumeration.IEnumKey;
|
||||
|
||||
public enum UserRole implements IEnumKey {
|
||||
Admin("ADMN"), Superviser("SPVS"), Security("SCRT")
|
||||
;
|
||||
|
||||
private final String key;
|
||||
|
||||
UserRole(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue