[Ornek] Kullaniciyi fonksiyonel sekilde login etme

59 views
Skip to first unread message

Osman Üngür

unread,
Feb 15, 2016, 4:41:15 AM2/15/16
to Symfony 2 Turkish Users
Selamlar, bir cok defa tekrar tekrar yaptigim ancak ihtiyacim oldugunda bulamadimdan ornek olmasi acisindan kullaniciyi elle fonksiyonel sekilde nasil login edeceginizin bir ornegini buraya ekliyorum. Bu ayrica unit testlerdede login islemi yapmanizi saglar.

Ornekte kullanicinin uyeligi onaylaniyor ve elimizdeki user objesi ile kullanici login ederek yonlendiriyoruz.

Burada security firewall'ununuzun adinin main oldugunu varsayiyoruz.


/**
* @Route("/account-confirmation/{confirmation_token}", requirements={"confirmation_token" = "[a-z0-9]{32}"}, name="security_account_confirmation")
*/
public function accountConfirmationAction(Request $request, $confirmation_token)
{
$user = $this->getDoctrine()
->getRepository('AppBundle:User')
->findOneBy(['confirmationToken' => $confirmation_token]);

if (!$user) {
throw $this->createNotFoundException();
}

if ($user->getIsVerified()) {
throw $this->createNotFoundException();
}

$user->setConfirmationToken(null);
$user->setIsVerified(true);

$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();

$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
$event = new InteractiveLoginEvent($request, $token);
$this->get('event_dispatcher')->dispatch(SecurityEvents::INTERACTIVE_LOGIN, $event);

return $this->redirectToRoute('dashboard');
}

Faruk Temur

unread,
Feb 15, 2016, 3:25:47 PM2/15/16
to symfon...@googlegroups.com
Paylaşımın için teşekkürler 👍

--

---
Bu iletiyi Google Grupları'ndaki "Symfony 2 Turkish Users" grubuna abone olduğunuz için aldınız.
Bu grubun aboneliğinden çıkmak ve bu gruptan artık e-posta almamak için symfony-2-tr...@googlegroups.com adresine e-posta gönderin.
Daha fazla seçenek için https://groups.google.com/d/optout adresini ziyaret edin.

Selim Can CABA

unread,
Aug 18, 2016, 8:27:16 PM8/18/16
to Symfony 2 Turkish Users
Öncelikle teşekkürler, bir projemde bunu kullandım. Githubta gezerken bir bundle'da bu işi kolaylaştırdıklarını gördüm. Belki kullanan olur. 
Bundle'ı kurduğumuzda aşağıdaki kod ile login işlemini yapabiliyoruz.

$this->get('security')->login($user);






15 Şubat 2016 Pazartesi 11:41:15 UTC+2 tarihinde Osman Ungur yazdı:
Reply all
Reply to author
Forward
0 new messages