Skip to content

Instantly share code, notes, and snippets.

@enekochan
Last active October 22, 2015 12:38
Show Gist options
  • Save enekochan/df7b89730224c3e76994 to your computer and use it in GitHub Desktop.
Save enekochan/df7b89730224c3e76994 to your computer and use it in GitHub Desktop.
Autologin URL with random hash for FOSUserBundle
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
class LoginController extends Controller
{
/**
* @Route("/login_hash/{login_hash}", name="login_hash")
* @param Request $request
* @param String $login_hash
* @return Response
* @throws AccessDeniedException
*/
public function loginAction(Request $request, $login_hash)
{
/** @var \AppBundle\Entity\User $user */
$user = $this->container->get('security.token_storage')->getToken()->getUser();
if (is_object($user)) {
throw new AccessDeniedException();
}
if ($login_hash) {
/** @var \FOS\UserBundle\Doctrine\UserManager $userManager */
$userManager = $this->get('fos_user.user_manager');
$user = $userManager->findUserBy(array('loginHash' => $login_hash));
if ($user) {
// Here, "main" is the name of the firewall in your security.yml
$token = new UsernamePasswordToken($user, $user->getPassword(), 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
// Fire the login event and logging the user (setToken doesn't do this automatically)
$event = new InteractiveLoginEvent($request, $token);
$this->get('event_dispatcher')->dispatch('security.interactive_login', $event);
// Remove the login hash
$user->setLoginHash(null);
$userManager->updateUser($user);
// Redirect to the home page
return $this->redirect($this->generateUrl('homepage'));
}
}
throw new AccessDeniedException();
}
}
services:
sonata.admin.user:
class: AppBundle\Admin\UserAdmin
tags:
- { name: sonata.admin, manager_type: orm, group: "Content", label: "User" }
arguments:
- ~
- AppBundle\Entity\User
- ~
calls:
- [ setTranslationDomain, [AppBundle]]
- [ setUserManager, ["@fos_user.user_manager"]]
<?php
namespace AppBundle\Admin;
use Symfony\Component\Security\Core\Role\RoleHierarchy;
use Symfony\Component\Security\Core\Role\Role;
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use FOS\UserBundle\Model\UserManagerInterface;
class UserAdmin extends Admin
{
protected $userManager;
public function setUserManager(UserManagerInterface $userManager)
{
$this->userManager = $userManager;
}
/**
* @return UserManagerInterface
*/
public function getUserManager()
{
return $this->userManager;
}
...
public function prePersist($user)
{
// Create the first login hash value
$loginHash = $this->container->get('app.service.random_generator')->randomString(64);
$user->setLoginHash($loginHash);
$this->getUserManager()->updateUser($user);
}
}
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=64, nullable=true)
*/
protected $loginHash;
public function __construct()
{
parent::__construct();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set loginHash
*
* @param string $loginHash
* @return User
*/
public function setLoginHash($loginHash)
{
$this->loginHash = $loginHash;
return $this;
}
/**
* Get loginHash
*
* @return string
*/
public function getLoginHash()
{
return $this->loginHash;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment