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
public void onEvent(Event event) {
if (event.getType() == EventType.REGISTER) {
var user = getUser(event.getUserId());
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.create(UserAdapter.class, user));
}
if (event.getType() == EventType.UPDATE_EMAIL || event.getType() == EventType.UPDATE_PROFILE) {
var user = getUser(event.getUserId());
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
}
if (event.getType() == EventType.DELETE_ACCOUNT) {
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.delete(UserAdapter.class, event.getUserId()));
EventType eventType = event.getType();
String eventUserId = event.getUserId();
switch (eventType) {
case REGISTER -> {
var user = getUser(eventUserId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.create(UserAdapter.class, user));
}
case UPDATE_EMAIL, UPDATE_PROFILE -> {
var user = getUser(eventUserId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
}
case DELETE_ACCOUNT ->
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.delete(UserAdapter.class, eventUserId));
default -> LOGGER.trace("ignore event " + eventType);
}
}
@ -66,61 +70,67 @@ public class ScimEventListenerProvider implements EventListenerProvider {
var matcher = pattern.matcher(event.getResourcePath());
if (!matcher.find())
return;
if (event.getResourceType() == ResourceType.USER) {
var userId = matcher.group(1);
LOGGER.infof("%s %s", userId, event.getOperationType());
if (event.getOperationType() == OperationType.CREATE) {
var user = getUser(userId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.create(UserAdapter.class, user));
user.getGroupsStream().forEach(group -> {
dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.replace(GroupAdapter.class, group));
});
switch (event.getResourceType()) {
case USER -> {
var userId = matcher.group(1);
LOGGER.infof("%s %s", userId, event.getOperationType());
switch (event.getOperationType()) {
case CREATE -> {
var user = getUser(userId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.create(UserAdapter.class, user));
user.getGroupsStream().forEach(group -> {
dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.replace(GroupAdapter.class, group));
});
}
case UPDATE -> {
var user = getUser(userId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
}
case DELETE ->
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.delete(UserAdapter.class, userId));
}
}
if (event.getOperationType() == OperationType.UPDATE) {
case GROUP -> {
var groupId = matcher.group(1);
LOGGER.infof("group %s %s", groupId, event.getOperationType());
switch (event.getOperationType()) {
case CREATE -> {
var group = getGroup(groupId);
dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.create(GroupAdapter.class, group));
}
case UPDATE -> {
var group = getGroup(groupId);
dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.replace(GroupAdapter.class, group));
}
case DELETE -> dispatcher.run(ScimDispatcher.SCOPE_GROUP,
(client) -> client.delete(GroupAdapter.class, groupId));
}
}
case GROUP_MEMBERSHIP -> {
var userId = matcher.group(1);
var groupId = matcher.group(2);
LOGGER.infof("%s %s from %s", event.getOperationType(), userId, groupId);
var group = getGroup(groupId);
group.setSingleAttribute("scim-dirty", BooleanUtils.TRUE);
var user = getUser(userId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
}
if (event.getOperationType() == OperationType.DELETE) {
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.delete(UserAdapter.class, userId));
}
}
if (event.getResourceType() == ResourceType.GROUP) {
var groupId = matcher.group(1);
LOGGER.infof("group %s %s", groupId, event.getOperationType());
if (event.getOperationType() == OperationType.CREATE) {
var group = getGroup(groupId);
dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.create(GroupAdapter.class, group));
}
if (event.getOperationType() == OperationType.UPDATE) {
var group = getGroup(groupId);
dispatcher.run(ScimDispatcher.SCOPE_GROUP, (client) -> client.replace(GroupAdapter.class, group));
}
if (event.getOperationType() == OperationType.DELETE) {
dispatcher.run(ScimDispatcher.SCOPE_GROUP,
(client) -> client.delete(GroupAdapter.class, groupId));
}
}
if (event.getResourceType() == ResourceType.GROUP_MEMBERSHIP) {
var userId = matcher.group(1);
var groupId = matcher.group(2);
LOGGER.infof("%s %s from %s", event.getOperationType(), userId, groupId);
var group = getGroup(groupId);
group.setSingleAttribute("scim-dirty", BooleanUtils.TRUE);
var user = getUser(userId);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
}
if (event.getResourceType() == ResourceType.REALM_ROLE_MAPPING) {
var type = matcher.group(1);
var id = matcher.group(2);
LOGGER.infof("%s %s %s roles", event.getOperationType(), type, id);
if (StringUtils.equals(type, "users")) {
var user = getUser(id);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
} else if (StringUtils.equals(type, "groups")) {
var group = getGroup(id);
session.users().getGroupMembersStream(session.getContext().getRealm(), group).forEach(user -> {
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
});
case REALM_ROLE_MAPPING -> {
var type = matcher.group(1);
var id = matcher.group(2);
LOGGER.infof("%s %s %s roles", event.getOperationType(), type, id);
switch (type) {
case "users" -> {
var user = getUser(id);
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
}
case "groups" -> {
var group = getGroup(id);
session.users().getGroupMembersStream(session.getContext().getRealm(), group).forEach(user -> {
dispatcher.run(ScimDispatcher.SCOPE_USER, (client) -> client.replace(UserAdapter.class, user));
});
}
}
}
}
}