1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
namespace SocleCommun\AuthentificationBundle\Security;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Bundle\DoctrineBundle\Registry;
class AuthentificationProvider implements UserProviderInterface{
protected $doctrine;
protected $dbUser;
protected $dbPass;
public function __construct(Registry $doctrine,$dbUser,$dbPass){
$this->doctrine = $doctrine;
$this->dbUser = $dbUser;
$this->dbPass = $dbPass;
}
private function switchDatabase($dbName='', $dbIp=''){
$paramDb['nomBase'] = $dbName;
$paramDb['hostBase'] = $dbIp;
$connection = $this->doctrine->getConnection('dynamique');
$connection->close();
$refConn = new \ReflectionObject($connection);
$refParams = $refConn->getProperty('_params');
$refParams->setAccessible('public');
$params = $refParams->getValue($connection);
$params['dbname'] = $dbName;
$params['user'] = $this->dbUser;
$params['password'] = $this->dbPass;
$params['host'] = $dbIp;
$refParams->setAccessible('private');
$refParams->setValue($connection, $params);
$this->doctrine->resetEntityManager('dynamique');
}
public function loadUserByUsername($username){
$q1 = $this->doctrine->getEntityManager()
->getRepository('SocleCommunAuthentificationBundle:config_client')
->findOneByIdentifiant($username);
$this->switchDatabase($q1->getNom_base(),$q1->getIpServeurPrive());
$q = $this->doctrine
->getEntityManager('dynamique')
->createQuery('
SELECT u FROM SocleCommunAuthentificationBundle:utilisateur u
WHERE u.username = :identifiant
OR u.email = :email')
->setParameters(array(
'identifiant' => $username,
'email' => $username,
));
try {
$user = $q->getSingleResult();
} catch (NoResultException $e) {
throw new UsernameNotFoundException(sprintf('Unable to find an active admin AcmeUserBundle:User object identified by "%s".', $username), null, 0, $e);
}
return $user;
}
public function refreshUser(UserInterface $user){
$class = get_class($user);
if (!$this->supportsClass($class)) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $class));
}
return $this->loadUserByUsername($user->getUsername());
}
public function supportsClass($class){
return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
}
} |
Partager