Use switch statements

This commit is contained in:
Brendan Le Ny 2024-06-12 15:20:26 +02:00
parent 26dc5edba2
commit 33bc30667d

View file

@ -45,16 +45,20 @@ public class ScimEventListenerProvider implements EventListenerProvider {
@Override @Override
public void onEvent(Event event) { public void onEvent(Event event) {
if (event.getType() == EventType.REGISTER) { EventType eventType = event.getType();
var user = getUser(event.getUserId()); String eventUserId = event.getUserId();
switch (eventType) {
case REGISTER -> {
var user = getUser(eventUserId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.create(UserAdapter.class, user)); dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.create(UserAdapter.class, user));
} }
if (event.getType() == EventType.UPDATE_EMAIL || event.getType() == EventType.UPDATE_PROFILE) { case UPDATE_EMAIL, UPDATE_PROFILE -> {
var user = getUser(event.getUserId()); var user = getUser(eventUserId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user)); dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
} }
if (event.getType() == EventType.DELETE_ACCOUNT) { case DELETE_ACCOUNT ->
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.delete(UserAdapter.class, event.getUserId())); dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.delete(UserAdapter.class, eventUserId));
default -> LOGGER.trace("ignore event " + eventType);
} }
} }
@ -66,41 +70,43 @@ public class ScimEventListenerProvider implements EventListenerProvider {
var matcher = pattern.matcher(event.getResourcePath()); var matcher = pattern.matcher(event.getResourcePath());
if (!matcher.find()) if (!matcher.find())
return; return;
if (event.getResourceType() == ResourceType.USER) { switch (event.getResourceType()) {
case USER -> {
var userId = matcher.group(1); var userId = matcher.group(1);
LOGGER.infof("%s %s", userId, event.getOperationType()); LOGGER.infof("%s %s", userId, event.getOperationType());
if (event.getOperationType() == OperationType.CREATE) { switch (event.getOperationType()) {
case CREATE -> {
var user = getUser(userId); var user = getUser(userId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.create(UserAdapter.class, user)); dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.create(UserAdapter.class, user));
user.getGroupsStream().forEach(group -> { user.getGroupsStream().forEach(group -> {
dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.replace(GroupAdapter.class, group)); dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.replace(GroupAdapter.class, group));
}); });
} }
if (event.getOperationType() == OperationType.UPDATE) { case UPDATE -> {
var user = getUser(userId); var user = getUser(userId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user)); dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
} }
if (event.getOperationType() == OperationType.DELETE) { case DELETE ->
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.delete(UserAdapter.class, userId)); dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.delete(UserAdapter.class, userId));
} }
} }
if (event.getResourceType() == ResourceType.GROUP) { case GROUP -> {
var groupId = matcher.group(1); var groupId = matcher.group(1);
LOGGER.infof("group %s %s", groupId, event.getOperationType()); LOGGER.infof("group %s %s", groupId, event.getOperationType());
if (event.getOperationType() == OperationType.CREATE) { switch (event.getOperationType()) {
case CREATE -> {
var group = getGroup(groupId); var group = getGroup(groupId);
dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.create(GroupAdapter.class, group)); dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.create(GroupAdapter.class, group));
} }
if (event.getOperationType() == OperationType.UPDATE) { case UPDATE -> {
var group = getGroup(groupId); var group = getGroup(groupId);
dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.replace(GroupAdapter.class, group)); dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.replace(GroupAdapter.class, group));
} }
if (event.getOperationType() == OperationType.DELETE) { case DELETE -> dispatcher.run(ScimDispatcher.SCOPE_GROUP,
dispatcher.run(ScimDispatcher.SCOPE_GROUP,
(client) -> client.delete(GroupAdapter.class, groupId)); (client) -> client.delete(GroupAdapter.class, groupId));
} }
} }
if (event.getResourceType() == ResourceType.GROUP_MEMBERSHIP) { case GROUP_MEMBERSHIP -> {
var userId = matcher.group(1); var userId = matcher.group(1);
var groupId = matcher.group(2); var groupId = matcher.group(2);
LOGGER.infof("%s %s from %s", event.getOperationType(), userId, groupId); LOGGER.infof("%s %s from %s", event.getOperationType(), userId, groupId);
@ -109,14 +115,16 @@ public class ScimEventListenerProvider implements EventListenerProvider {
var user = getUser(userId); var user = getUser(userId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user)); dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
} }
if (event.getResourceType() == ResourceType.REALM_ROLE_MAPPING) { case REALM_ROLE_MAPPING -> {
var type = matcher.group(1); var type = matcher.group(1);
var id = matcher.group(2); var id = matcher.group(2);
LOGGER.infof("%s %s %s roles", event.getOperationType(), type, id); LOGGER.infof("%s %s %s roles", event.getOperationType(), type, id);
if (StringUtils.equals(type, "users")) { switch (type) {
case "users" -> {
var user = getUser(id); var user = getUser(id);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user)); dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
} else if (StringUtils.equals(type, "groups")) { }
case "groups" -> {
var group = getGroup(id); var group = getGroup(id);
session.users().getGroupMembersStream(session.getContext().getRealm(), group).forEach(user -> { session.users().getGroupMembersStream(session.getContext().getRealm(), group).forEach(user -> {
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user)); dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
@ -124,6 +132,8 @@ public class ScimEventListenerProvider implements EventListenerProvider {
} }
} }
} }
}
}
private UserModel getUser(String id) { private UserModel getUser(String id) {
return session.users().getUserById(session.getContext().getRealm(), id); return session.users().getUserById(session.getContext().getRealm(), id);