Symfony HTTP AUTH


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public function preExecute()
  2. {
  3. $this->checkBasicAuth();
  4. }
  5.  
  6.  
  7. protected function sendHeadersAndExit ()
  8. {
  9. header('WWW-Authenticate: Basic realm="Acces restreint"');
  10. header('HTTP/1.0 401 Unauthorized');
  11. header('Content-Type: text/html; charset=utf-8');
  12. exit();
  13. }
  14.  
  15. protected function checkBasicAuth()
  16. {
  17. if($this->getUser()->isAuthenticated() && $this->getUser()->hasAttribute('user_id')) return true;
  18.  
  19. if (!isset($_SERVER['PHP_AUTH_USER']))
  20. {
  21. $this->sendHeadersAndExit();
  22. }
  23.  
  24. $group = ParameterPeer::get('contributeur_groupe_id');
  25.  
  26. if ( !$member = MemberPeer::authCheckTrickSpace($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'], $group) )
  27. {
  28. $this->sendHeadersAndExit();
  29. }
  30. else
  31. {
  32. $this->getUser()->setAttribute('member_id', $member->getId());
  33. $this->getUser()->setAuthenticated(true);
  34. }
  35. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.