<?php
namespace App\Security;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Smt\CoreBundle\Service\SmtAuth;
use Smt\CoreBundle\Service\SmtApi;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class UserProvider implements UserProviderInterface, PasswordUpgraderInterface
{
private $Lo_SmtApi;
private $o_Session;
public function __construct(SmtApi $Ao_SmtApi, SessionInterface $session){
$this->Lo_SmtApi = $Ao_SmtApi;
$this->o_Session = $session;
}
/**
* @required
*/
public function setSmtApi(SmtApi $smtapi){
$this->Lo_SmtApi = $smtapi;
}
/**
* Symfony calls this method if you use features like switch_user
* or remember_me.
*
* If you're not using these features, you do not need to implement
* this method.
*
* @return UserInterface
*
* @throws UsernameNotFoundException if the user is not found
*/
public function loadUserByUsername($credencials)
{
// Load a User object from your data source or throw UsernameNotFoundException.
// The $username argument may not actually be a username:
// it is whatever value is being returned by the getUsername()
// method in your User class.
$user = new User;
$Lo_SmtAuth = new SmtAuth($this->Lo_SmtApi);
$La_Result = $Lo_SmtAuth->authentication($credencials['email'], $credencials['password'], $this->o_Session);
//$Lo_ApiController = new ApiController;
//$La_Result = $Lo_ApiController->authSmt($credencials['email'], $credencials['password']);
if (!$La_Result['b_Success']) {
// fail authentication with a custom error
throw new UsernameNotFoundException();
}
$user->setEmail($credencials['email']);
$user->setPersonID($La_Result['i_SSP_PersonID']);
$user->setFirstName($La_Result['s_FirstName']);
$user->setLastName($La_Result['s_LastName']);
//$user->setSetCookie($La_Result['LoginHeaders']['Set-Cookie']);
$this->o_Session->set('core/a_Person/LoginHeaders/Set-Cookie', $La_Result['LoginHeaders']['Set-Cookie']);
$user->setRoles($La_Result['a_ADRoles']);
$user->setFAKNumber($La_Result['s_FAKNumber']);
$user->setBirthdate($La_Result['s_Birthdate']);
$user->setAddressRole($La_Result['a_AddressRole']);
$user->setProperties($La_Result['o_Properties']);
return $user;
}
/**
* Refreshes the user after being reloaded from the session.
*
* When a user is logged in, at the beginning of each request, the
* User object is loaded from the session and then this method is
* called. Your job is to make sure the user's data is still fresh by,
* for example, re-querying for fresh User data.
*
* If your firewall is "stateless: true" (for a pure API), this
* method is not called.
*
* @return UserInterface
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Invalid user class "%s".', get_class($user)));
}
/* $Lo_SmtAuth = new SmtAuth($this->Lo_SmtApi);
$La_Result = $Lo_SmtAuth->validateSession($user->getSetCookie(), $user->getPersonID());
// Return a User object after making sure its data is "fresh".
// Or throw a UsernameNotFoundException if the user no longer exists.
if(!$La_Result){
throw new UsernameNotFoundException(sprintf("Session ungültig oder abgelaufen."));
} */
return $user;
}
/**
* Tells Symfony to use this provider for this User class.
*/
public function supportsClass($class)
{
return User::class === $class;
}
/**
* Upgrades the encoded password of a user, typically for using a better hash algorithm.
*/
public function upgradePassword(UserInterface $user, string $newEncodedPassword): void
{
// TODO: when encoded passwords are in use, this method should:
// 1. persist the new password in the user storage
// 2. update the $user object with $user->setPassword($newEncodedPassword);
}
}