<?php
namespace ProjectBiz\PortalBundle\Command;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
/**
* Base class for commands to provide the loginUser() function.
*/
class Command extends \Symfony\Component\Console\Command\Command {
public function __construct(
\Swift_Mailer $mailer,
ContainerInterface $container
)
{
$this->mailer = $mailer;
$this->container = $container;
parent::__construct();
}
protected function loginUser($username)
{
$userProvider = $this->container->get('projectbiz.user.user_provider');
$user = $userProvider->loadUserByUsername($username);
// create the authentication token
$token = new UsernamePasswordToken(
$user,
null,
'main',
$user->getRoles()
);
// give it to the security context
$this->container->get('security.token_storage')->setToken($token);
return $user;
}
}