getUser() ) { $logger->debug('SFYCAS: redirecting'); return $this->redirectToRoute('app_cas_login'); } else { $logger->debug('SFYCAS: user already authenticated'); return $this->render('sfy_cas_login/index.html.twig', [ 'controller_name' => 'SfyCASLoginController', ]); } } /** * @Route("/cas/login", name="app_cas_login") */ public function login(AuthenticationUtils $authenticationUtils, Request $request, LoggerInterface $logger, SfyCASSessionRepository $sfyCASSessionRepository, SfyCASTicketGenerator $ticketGenerator): Response { if ($this->getUser()) { //return $this->redirectToRoute('target_path'); /*echo '
';
print_r($request->query->get('service'));
echo '
';
*/
$user = $this->getUser();
$_casSession=$sfyCASSessionRepository->findBy(['user' => $this->getUser()]);
if(is_array($_casSession) && count($_casSession) > 0) {
$casSession=$_casSession[0];
$ticket=$casSession->getTicket();
$logger->info('SFYCAS ticket ' . $casSession->getTicket() . ' found for user : ' . $user);
} else {
$logger->info('SFYCAS Session : create new cas session');
$casSession=new SfyCASSession();
$ticket=$ticketGenerator->getTicket();
$casSession->setLogin($user->getUsername());
$casSession->setUser($user);
$casSession->setTicket('ST-' . $ticket);
}
$chemin=$request->getBasePath();
//$logger->info('SFYCAS login base url : ' . $request);
$service=$request->query->get('service');
//$user=$this->getUser();
//echo $user->getUsername();
//$ticket=$ticketGenerator->getTicket();
//$casSession->setLogin($user->getUsername());
//$casSession->setUser($user);
//$casSession->setTicket('ST-' . $ticket);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($casSession);
$entityManager->flush();
//$response = new RedirectResponse($service. '?ticket=ST-ABFDABFE');
$attributes = parse_url($service);
//$params = $attributes['query'];
if(isset($attributes['query'])) {
$responseUrl = $service. '&ticket=ST-'.$ticket;
} else {
$responseUrl = $service. '?ticket=ST-'.$ticket;
}
//$response = new RedirectResponse($service. '?ticket=ST-' . $ticket);
$response = new RedirectResponse($responseUrl);
/*$response->headers->set('Set-Cookie',
Cookie::create(
'sfy_cas_session',
$casSession->getTicket(),
time() + 60 * 60 * 24 * 30,
'/',
null,
false,
true,
false,
Cookie::SAMESITE_LAX
)
);
*/
$response->headers->setCookie(Cookie::create('CASTGC', 'ST-' . $ticket));
$logger->info('SFYCAS login user : ' . $user . 'ticket ' . $ticket . ' service : ' . $service);
return $response;
//return $this->redirect($service. '?ticket=ST-ABFDABFE');
}
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('sfy_cas_login/caslogin.html.twig', ['last_username' => $lastUsername, 'error' => $error]);
}
/**
* @Route("/cas/logout", name="app_cas_logout")
*/
public function logout()
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}