<?php
namespace App\Security\User;
use Es\FootprintBundle\Services\Auth;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
class ApiUserProvider implements UserProviderInterface
{
private $es_footprint_auth;
public function __construct(Auth $es_footprint_auth)
{
$this->es_footprint_auth = $es_footprint_auth;
}
public function loadUserByIdentifier($tokenizedUrl): UserInterface
{
return $this->loadUserByUsername($tokenizedUrl);
}
public function loadUserByUsername(string $tokenizedUrl): UserInterface
{
$response = $this->es_footprint_auth->checkFootPrint($tokenizedUrl);
if ('error' == $response['status']) {
throw new AuthenticationException($response['message']);
}
$data = $response['data'];
$username = $data["_id"];
$contactMail = isset($data["email"]) ? $data["email"] : null;
$displayName = isset($data["displayName"]) ? $data["displayName"] : null;
$firstName = isset($data["firstName"]) ? $data["firstName"] : null;
$lastName = isset($data["lastName"]) ? $data["lastName"] : null;
$roles = ['ROLE_USER'];
$groups = [];
$applications = [];
$companies = [];
/* We gather user informations (roles, app, groups, companies) */
if (isset($data["roles"])) {
foreach ($data["roles"] as $role) {
array_push($roles, strtoupper($role));
}
}
if (isset($data["groups"])) {
foreach ($data["groups"] as $group) {
array_push($groups, $group["_id"]);
}
}
if (isset($data["applications"])) {
foreach ($data["applications"] as $application) {
array_push($applications, $application["_id"]);
}
}
if (isset($data["companyName"])) {
array_push($companies, $data["companyName"]);
}
$apiUser = new ApiUser($username, $contactMail, $displayName, $firstName, $lastName, $tokenizedUrl, $roles, $groups, $companies, $applications);
return $apiUser;
}
public function refreshUser(UserInterface $user)
{
if (!$user instanceof ApiUser) {
throw new UnsupportedUserException(
sprintf('Instances of "%s" are not supported.', get_class($user))
);
}
return $this->loadUserByUsername($user->getApiKey());
}
public function supportsClass(string $class)
{
return ApiUser::class === $class;
}
}