Feat: Implement entities for user and teamer, finalize login process

This commit is contained in:
Björn Fromme
2023-09-12 16:35:06 +02:00
parent 3739decab8
commit 35ca61d21b
27 changed files with 1008 additions and 442 deletions
+23 -44
View File
@@ -6,9 +6,7 @@ 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\BusProNet\UserDataHandler;
use App\Entity\User;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
@@ -33,7 +31,8 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
private readonly UrlGeneratorInterface $urlGenerator,
private readonly EntityManagerInterface $entityManager,
private readonly ApiClient $apiClient,
private readonly LoggerInterface $logger
private readonly LoggerInterface $logger,
private readonly UserDataHandler $userDataHandler
) {
}
@@ -72,6 +71,9 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
$user = $token->getUser();
$user->setLastLoginAt(new \DateTimeImmutable());
$this->logger->info('Login', [
'user' => $user->getUserIdentifier(),
]);
$this->entityManager->flush();
@@ -95,56 +97,33 @@ class BpnAuthenticator extends AbstractLoginFormAuthenticator implements Authent
}
// Collect user's roles from CRM attributes
$roles = [];
$roles = $this
->userDataHandler
->collectRoles($crmAttributes)
;
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);
// Determine teamer status from CRM attributes
$isTeamer = $crmAttributes->isTeamer();
// Check if user is already present in local database
$repository = $this->entityManager->getRepository(User::class);
$user = $this
->userDataHandler
->findLocalUser($profileResponse)
;
$user = $repository->findOneBy([
'profile.busProAddressId' => $profileResponse->getAddressId(),
'profile.busProPersonId' => $profileResponse->getPersonId(),
]);
// Update existing user's roles and address and return it
// Update existing user's roles and teamer data and return it
if (null !== $user) {
$user
->setProfile($profile)
->setAddress($address)
->setCommunication($communication)
->setRoles($roles)
$this
->userDataHandler
->updateLocalUser($user, $profileResponse, $roles, $isTeamer)
;
return $user;
}
// Create new user entity to persist locally otherwise
$user = new User();
$user
->setEmail($profileResponse->getCommunication()->getEmail())
->setProfile($profile)
->setAddress($address)
->setCommunication($communication)
->setRoles($roles)
return $this
->userDataHandler
->createLocalUser($profileResponse, $roles, $isTeamer)
;
$this->entityManager->persist($user);
$this->entityManager->flush();
return $user;
}
}