我遇到了Symfony2会话的问题。
我试图创建一个在每个控制器顶部执行的服务。 此服务的目的是验证用户身份。
我首先检查会话以查看是否有" auth"键。 如果没有,我会检查是否有cookie ......
问题在于访问服务中的用户会话。
我已将Request对象传递给我的服务参数,并使用$request->getSession();
获取会话。
一切似乎都有效,但会话中没有数据存储......
我很确定问题来自于我在服务中获得会话的方式。但是,我不知道如何以另一种方式做到这一点。
有人能帮助我吗?
这是我的 services.yml 文件:
services:
myBundle.authService:
class: MyCompany\MyBundle\Services\AuthService
scope: request
arguments:
request: @request
logger: @logger
em: @doctrine.orm.entity_manager
这是我的 AuthService 类:
namespace MyCompany\MyBundle\Services;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
use Doctrine\ORM\EntityManager;
use Monolog\Logger;
use MyCompany\MyBundle\Entity\User;
use MyCompany\MyBundle\Entity\Session;
class AuthService
{
private $_request;
private $_logger;
private $_em;
private $_session;
public function __construct(Request $request, Logger $logger, EntityManager $em)
{
$this->_request = $request;
$this->_logger = $logger;
$this->_em = $em;
$this->_session = $this->_request->getSession();
$this->_session->start();
$this->_fUser = null;
$encoders = array(new XmlEncoder(), new JsonEncoder());
$normalizers = array(new GetSetMethodNormalizer());
$this->_serializer = new Serializer($normalizers, $encoders);
}
// THIS FUNCTION IS CALLED IN CONTROLLER
public function checkAuth()
{
$_return = false;
if($this->checkSession())
{
$_return = true;
}
else
{
$userCookie = $this->checkCookie();
if($userCookie)
{
if(is_array($userCookie) && array_key_exists('token', $userCookie) && array_key_exists('value', $userCookie))
{
$fUser = $this->findUserBySession($userCookie['token'], $userCookie['value']);
if($fUser)
{
$_return = $this->updateSession($fUser);
}
}
}
}
return $_return;
}
// CHECK IF SESSION HAS 'FUSER' KEY AND 'AUTH' KEY
private function checkSession()
{
$_return = false;
if($this->_session && $this->_session->has("FUSER") && $this->_session->has("AUTH") && preg_match("/[0-9a-zA-Z]{58}/", $this->_session->get("AUTH")) === true)
{
// I NEVER GO HERE
$this->_logger->info("OK \o/");
$_return = $this->_session->get('FUSER');
}
return $_return;
}
// CHECK IF AUTH COOKIE IS SET
private function checkCookie()
{
$_return = false;
$cookies = $this->_request->cookies;
if($cookies && count($cookies) > 0)
{
foreach($cookies as $key => $val)
{
if(preg_match("/[0-9a-zA-Z]{58}/", $key) && preg_match("/[0-9a-zA-Z]{58}/", $val))
{
$_return = array('token' => $key, 'value' => $val);
}
}
}
return $_return;
}
// FIND SESSION IN DB AND USER ASSOCIATED
private function findUserBySession($token, $value)
{
$_return = false;
$fSessionRepository = $this->_em->getRepository("MyCompanyBundle:FSession");
if($fSessionRepository)
{
$fSession = $fSessionRepository->findOneBy(array('token' => $value));
if($fSession)
{
$fUser = $fSession->getUser();
if($fUser)
{
if($fUser->getToken() === $token)
{
$_return = $fUser;
}
}
}
}
return $_return;
}
// UPDATE SESSION TO REGISTER 'USER' AND 'AUTH' KEYS
public function updateSession(User $fUser)
{
$_return = false;
if($this->_session)
{
$this->_session->set('FUSER', $this->serialize($fUser, 'json'));
$this->_session->set('AUTH', User::generateToken());
// THIS WORKS FINE
$this->_logger->info("OK !!! _o/");
$_return = $this->serialize($fUser, 'json');
}
return $_return;
}
// SERIALIZE OBJECT
public function serialize($JSONObject, $format)
{
return $this->_serializer->serialize($JSONObject, $format);
}
// DESERIALIZE OBJECT
public function deserialize($JSONObject, $entity, $format)
{
return $this->_serializer->deserialize($JSONObject, $entity, $format);
}
}
提前感谢您的时间和帮助。