This commit is contained in:
Björn Fromme committed 2023-09-02 18:47:42 +02:00
1 parent 23e66b08e8
commit 08f7836d41
21 files changed
+693 -82

No files matched your search

+49 -21
View File
@@ -6,6 +6,9 @@ use App\BusProNet\ApiClient;
use App\BusProNet\ApiClientException;
use App\BusProNet\Model\CrmAttributesResponse;
use App\BusProNet\Model\ProfileResponse;
use App\Entity\Embeddable\Address;
use App\Entity\Embeddable\Communication;
use App\Entity\Embeddable\Profile;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
@@ -81,19 +84,9 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
return new RedirectResponse($url);
}
private function getOrCreateLocalUser(ProfileResponse $profile, string $email, string $password): ?User
private function getOrCreateLocalUser(ProfileResponse $profileResponse, string $email, string $password): ?User
{
$repository = $this->entityManager->getRepository(User::class);
$user = $repository->findOneBy([
'busProAddressId' => $profile->getAddressId(),
'busProPersonId' => $profile->getPersonId(),
]);
if (null !== $user) {
return $user;
}
// Fetch CRM attributes, early return in case of an API arror
try {
/** @var CrmAttributesResponse $crmAttributes */
$crmAttributes = $this->apiClient->getCrmAttributes($email, $password);
@@ -101,19 +94,54 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
return null;
}
// Collect user's roles from CRM attributes
$roles = [];
if ($crmAttributes->isAdmin()) {
$roles[] = 'ROLE_ADMIN';
}
if ($crmAttributes->isManager()) {
$roles[] = 'ROLE_MANAGER';
}
if ($crmAttributes->isTeamer()) {
$roles[] = 'ROLE_TEAMER';
}
// Get profile data from API response
$profile = Profile::fromApiResponse($profileResponse);
$address = Address::fromApiResponse($profileResponse);
$communication = Communication::fromApiResponse($profileResponse);
// Check if user is already present in local database
$repository = $this->entityManager->getRepository(User::class);
$user = $repository->findOneBy([
'profile.busProAddressId' => $profileResponse->getAddressId(),
'profile.busProPersonId' => $profileResponse->getPersonId(),
]);
// Update existing user's roles and address and return it
if (null !== $user) {
$user
->setProfile($profile)
->setAddress($address)
->setCommunication($communication)
->setRoles($roles)
;
return $user;
}
// Create new user entity to persist locally otherwise
$user = new User();
$user
->setBusProAddressId($profile->getAddressId())
->setBusProPersonId($profile->getPersonId())
->setEmail($profile->getCommunication()->getEmail())
->setFirstName($profile->getFirstName())
->setLastName($profile->getName())
->setEmail($profileResponse->getCommunication()->getEmail())
->setProfile($profile)
->setAddress($address)
->setCommunication($communication)
->setRoles($roles)
;
if ($crmAttributes->isTeamer()) {
$user->setRole('ROLE_TEAMER');
}
$this->entityManager->persist($user);
$this->entityManager->flush();