com.xlrit.gears.plugin.msgraph.SyncHandlerImpl Maven / Gradle / Ivy
package com.xlrit.gears.plugin.msgraph;
import java.util.Map;
import java.util.Set;
import com.xlrit.gears.base.model.Role;
import com.xlrit.gears.base.model.User;
import com.xlrit.gears.base.repository.RoleRepository;
import com.xlrit.gears.base.repository.UserRepository;
import com.xlrit.gears.engine.util.EngineUtils;
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
public class SyncHandlerImpl implements SyncHandler {
private final Map groupMappings;
private final RoleRepository roles;
private final UserRepository users;
@Override
public Role handleGroup(com.microsoft.graph.models.Group provGroup) {
String roleId = EngineUtils.uuidToUlid(provGroup.id);
Role role = getOrCreateRoleById(roleId);
String roleName = groupMappings.get(provGroup.id);
role.setExternalId(provGroup.id);
role.setName(roleName);
role.setDescription(provGroup.description);
return role;
}
@Override
public User handleUser(com.microsoft.graph.models.User provUser) {
String userId = EngineUtils.uuidToUlid(provUser.id);
User nativeUser = getOrCreateUserById(userId);
nativeUser.setExternalId(provUser.id);
nativeUser.setUsername(provUser.userPrincipalName);
nativeUser.setFamilyName(provUser.surname);
nativeUser.setGivenName(provUser.givenName);
nativeUser.setEmailAddress(provUser.mail);
nativeUser.setMobileNumber(provUser.mobilePhone);
nativeUser.setActive(provUser.accountEnabled != Boolean.FALSE);
return nativeUser;
}
@Override
public void handleMembership(Role role, Set users) {
role.setUsersBidi(users);
}
private Role getOrCreateRoleById(String roleId) {
Role role = roles.findById(roleId);
return role != null ? role : roles.create(roleId);
}
private User getOrCreateUserById(String userId) {
User user = users.findById(userId);
return user != null ? user : users.create(userId);
}
}