Hello I'm a newbie in Symfony2. The problem is with users.
After downloading the file from the server, the user's session is overwritten by new data of another user from the database. Prior to download, all the data are correct, that I get from the $ this-> get (security.context) -> getToken () -> getUser ()
, but after downloading the file just get the data of another user from the database.
It turns out session overwritten.
/*config.yml*/
session:
save_path: %kernel.root_dir%/var/sessions
cookie_lifetime: 3600
cookie_httponly: false
This is Action:
public function takeCouponFileAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$user = $this->get('security.context')->getToken()->getUser();
$date = date('Y-m-d H:i:s');
$coupon = $em->getRepository('AcmeAdministrationBundle:CouponUsers')->find($id);
if(!$coupon || ($coupon->getUserId()!= $user->getId())){
$response = array("code" => 404, "error" => 'ERROR USER');
return new Response(json_encode($response),200,array('Content-Type'=>'application/json'));
}
if(!file_exists('upload/pdf/'.$user->getId().'/'.$coupon->getCouponId().'/'.$coupon->getId())){
$response = array("code" => 404, "error" => 'ERROR FILE');
return new Response(json_encode($response),200,array('Content-Type'=>'application/json'));
}else{
$path = 'upload/pdf/'.$user->getId().'/'.$coupon->getCouponId().'/'.$coupon->getId();
$file_name = $user->getId().'-'.$coupon->getCouponId().'-'.$coupon->getId().'.pdf';
$response = array("code" => 200, "url" => 'http://site.ru/'.$path.'/'.$file_name);
$resp = new Response();
$resp->headers->set('X-Sendfile', $file_name);
$resp->headers->set('Content-Type', 'application/pdf');
$resp->headers->set('Content-Length', filesize($path.'/'.$file_name));
$resp->headers->set('Content-Disposition', 'attachment; filename="'.$file_name.'";');
$resp->headers->set('Content-Transfer-Encoding', 'binary');
$resp->headers->set('Cache-Control', 'must-revalidate');
$resp->setContent(file_get_contents($path.'/'.$file_name));
// $resp->sendHeaders();
// $resp->sendContent();
return $resp->send();
}
}
And more. In *dev mode, everything works fine. But in *prod, after downloading, the user is automatically changed.
Replaced only by those users who are logged in software.
$firewall_name = 'user_secured_area';
$token = new UsernamePasswordToken($ex_user, null, $firewall_name, $ex_user->getRoles());
$this->container->get('security.context')->setToken($token);
After program authorization and update page, method $user = $this->get('security.context')->getToken()->getUser();
return [error] => token expired
.
Solution
Just was need insert redirect after authentication user.
$firewall_name = 'user_secured_area';
$token = new UsernamePasswordToken($ex_user, null, $firewall_name, $ex_user->getRoles());
$this->container->get('security.context')->setToken($token);
$this->get('security.context')->getToken()->setUser($ex_user);
$this->em->persist($ex_user);
$this->em->flush();
return $this->redirect($this->generateUrl('_front_index'));