172 lines
6.9 KiB
PHP
172 lines
6.9 KiB
PHP
<?php
|
|
|
|
namespace Cadoles\CASBundle\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
|
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
|
|
|
use Cadoles\CoreBundle\Entity\User;
|
|
|
|
class SecurityController extends Controller
|
|
{
|
|
public function metadataAction()
|
|
{
|
|
return parent::metadataAction();
|
|
}
|
|
|
|
public function loginAction(Request $request)
|
|
{
|
|
// Masteridentity
|
|
$masteridentity=$this->getParameter("masteridentity");
|
|
|
|
// Init Client CAS
|
|
\phpCAS::setDebug(false);
|
|
\phpCAS::client(CAS_VERSION_2_0, $this->getParameter('cas_host'), $this->getParameter('cas_port'), is_null($this->getParameter('cas_path')) ? '' : $this->getParameter('cas_path'), false);
|
|
\phpCAS::setNoCasServerValidation();
|
|
|
|
|
|
// Authentification
|
|
\phpCAS::forceAuthentication();
|
|
|
|
// Récupération UID
|
|
$username = \phpCAS::getUser();
|
|
|
|
// Récupération Attribut
|
|
$attributes = \phpCAS::getAttributes();
|
|
|
|
// Suppression des Attributs en tableaux
|
|
foreach ($attributes as $key => $value) {
|
|
if(is_array($value))
|
|
unset($attributes[$key]);
|
|
}
|
|
|
|
// Rechercher l'utilisateur
|
|
$em = $this->getDoctrine()->getManager();
|
|
if(isset($attributes[$this->getParameter('user_attr_cas_username')]))
|
|
$username = $attributes[$this->getParameter('user_attr_cas_username')];
|
|
|
|
if(isset($attributes[$this->getParameter('user_attr_cas_mail')]))
|
|
$email = $attributes[$this->getParameter('user_attr_cas_mail')];
|
|
|
|
if(isset($attributes[$this->getParameter('user_attr_cas_lastname')]))
|
|
$lastname = $attributes[$this->getParameter('user_attr_cas_lastname')];
|
|
|
|
if(isset($attributes[$this->getParameter('user_attr_cas_firstname')]))
|
|
$firstname = $attributes[$this->getParameter('user_attr_cas_firstname')];
|
|
|
|
$user = $em->getRepository('CadolesCoreBundle:User')->findOneBy(array("username"=>$username));
|
|
$exists = $user ? true : false;
|
|
|
|
if (!$exists) {
|
|
if($masteridentity=="SQL") {
|
|
// C'est pas normal que l'on puisse se connecter alors que l'utilisateur n'est pas connu en base
|
|
// La base étant le maitre de l'identité
|
|
throw $this->createNotFoundException('Permission denied');
|
|
}
|
|
|
|
if($masteridentity=="LDAP") {
|
|
// Normalement la synchronisation des comptes aurait du générer le compte en base c'est donc pas normal
|
|
// Peut-être juste relancer une synchronisation
|
|
throw $this->createNotFoundException('Permission denied. Need to synchronize LDAP ? Contact your administrator');
|
|
}
|
|
|
|
if($masteridentity=="SSO") {
|
|
// Là c'est normal que potentiellement il n'existe pas il faut donc l'autogénérer
|
|
$user = new User();
|
|
|
|
// On calcule le niveau01 de l'utilisateur
|
|
$niveau01=$em->getRepository('CadolesCoreBundle:Niveau01')->calculateNiveau01($attributes);
|
|
if(!$niveau01)
|
|
throw $this->createNotFoundException('Permission denied. No Organisation Niveau 01 match');
|
|
|
|
$user->setUsername($username);
|
|
$user->setEmail($email);
|
|
$user->setLastname($lastname);
|
|
$user->setFirstname($firstname);
|
|
|
|
$user->setPassword("CASPWD-".$username);
|
|
$user->setSalt("CASPWD-".$username);
|
|
|
|
$user->setNiveau01($niveau01);
|
|
$user->setSiren($niveau01->getSiren());
|
|
$user->setSiret("");
|
|
|
|
$user->setAvatar("noavatar.png");
|
|
$user->setVisible(true);
|
|
$user->setAuthlevel("simple");
|
|
$user->setRole("ROLE_USER");
|
|
|
|
$em->persist($user);
|
|
$em->flush();
|
|
|
|
// On calcule les groupes de l'utilisateur
|
|
$user=$groups=$em->getRepository('CadolesCoreBundle:Group')->calculateGroup($user,$attributes);
|
|
}
|
|
}
|
|
else {
|
|
// Mise à jour des valeurs uniquement si le maitre de l'identité est le SSO
|
|
if($masteridentity=="SSO") {
|
|
// On calcule le niveau01 de l'utilisateur
|
|
$niveau01=$em->getRepository('CadolesCoreBundle:Niveau01')->calculateNiveau01($attributes);
|
|
if(!$niveau01)
|
|
throw $this->createNotFoundException('Permission denied. No Organisation Niveau 01 match');
|
|
|
|
// On s'assure que le niveau 02 appartient bien au niveau 01 calculé
|
|
$sameniveau01=(!is_null($user->getNiveau02())&&$niveau01==$user->getNiveau02()->getNiveau01());
|
|
|
|
// On calcule les groupes de l'utilisateur
|
|
$user=$groups=$em->getRepository('CadolesCoreBundle:Group')->calculateGroup($user,$attributes);
|
|
|
|
$user->setLastname($lastname);
|
|
$user->setFirstname($firstname);
|
|
$user->setEmail($email);
|
|
if(!$sameniveau01) $user->setNiveau02(null);
|
|
|
|
$em->persist($user);
|
|
$em->flush();
|
|
}
|
|
}
|
|
|
|
// Calcul Service
|
|
/*
|
|
$user = $em->getRepository('CadolesCoreBundle:User')->calculateServices($user, $attributes);
|
|
|
|
// Attributs calculés
|
|
$attributes = $em->getRepository('CadolesCoreBundle:User')->calculateAttributes($user, $attributes);
|
|
*/
|
|
|
|
// Sauvegarde des attributes en session
|
|
$this->get('session')->set('attributes', $attributes);
|
|
|
|
// Autoconnexion
|
|
// Récupérer le token de l'utilisateur
|
|
$token = new UsernamePasswordToken($user, null, "main", $user->getRoles());
|
|
$this->get("security.token_storage")->setToken($token);
|
|
|
|
// Simuler l'evenement de connexion
|
|
$event = new InteractiveLoginEvent($request, $token);
|
|
$dispatcher = new EventDispatcher();
|
|
$dispatcher->dispatch("security.interactive_login", $event);
|
|
|
|
|
|
return $this->redirect($this->generateUrl('cadoles_core_home'));
|
|
}
|
|
|
|
public function logoutAction() {
|
|
// Init Client CAS
|
|
\phpCAS::setDebug(false);
|
|
\phpCAS::client(CAS_VERSION_2_0, $this->getParameter('cas_host'), $this->getParameter('cas_port'), is_null($this->getParameter('cas_path')) ? '' : $this->getParameter('cas_path'), true);
|
|
\phpCAS::setNoCasServerValidation();
|
|
|
|
|
|
// Logout
|
|
$url=$this->generateUrl('cadoles_core_home', array(), UrlGeneratorInterface::ABSOLUTE_URL);
|
|
\phpCAS::logout(array("service"=>$url));
|
|
}
|
|
}
|