1. sfBasicSecurityFilter.class.php
  2. /** * sfBasicSecurityFilter checks security by calling the getCredential() method * of the action. Once the credential has been acquired, sfBasicSecurityFilter * verifies the user has the same credential by calling the hasCredential() * method of SecurityUser. * * @package symfony * @subpackage filter * @author Sean Kerr * @version SVN: $Id: sfBasicSecurityFilter.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. class sfBasicSecurityFilter extends sfFilter
  4. {
  5. /**
  6. * Executes this filter.
  7. *
  8. * @param sfFilterChain $filterChain A sfFilterChain instance
  9. */
  10. public function execute($filterChain)
  11. {
  12. // disable security on login and secure actions
  13. if (
  14. (sfConfig::get('sf_login_module') == $this->context->getModuleName()) && (sfConfig::get('sf_login_action') == $this->context->getActionName())
  15. ||
  16. (sfConfig::get('sf_secure_module') == $this->context->getModuleName()) && (sfConfig::get('sf_secure_action') == $this->context->getActionName())
  17. )
  18. {
  19. $filterChain->execute();
  20. return;
  21. }
  22. // NOTE: the nice thing about the Action class is that getCredential()
  23. // is vague enough to describe any level of security and can be
  24. // used to retrieve such data and should never have to be altered
  25. if (!$this->context->getUser()->isAuthenticated())
  26. {
  27. if (sfConfig::get('sf_logging_enabled'))
  28. {
  29. $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array(sprintf('Action "%s/%s" requires authentication, forwarding to "%s/%s"', $this->context->getModuleName(), $this->context->getActionName(), sfConfig::get('sf_login_module'), sfConfig::get('sf_login_action')))));
  30. }
  31. // the user is not authenticated
  32. $this->forwardToLoginAction();
  33. }
  34. // the user is authenticated
  35. $credential = $this->getUserCredential();
  36. if (null !== $credential && !$this->context->getUser()->hasCredential($credential))
  37. {
  38. if (sfConfig::get('sf_logging_enabled'))
  39. {
  40. $this->context->getEventDispatcher()->notify(new sfEvent($this, 'application.log', array(sprintf('Action "%s/%s" requires credentials "%s", forwarding to "%s/%s"', $this->context->getModuleName(), $this->context->getActionName(), sfYaml::dump($credential, 0), sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action')))));
  41. }
  42. // the user doesn't have access
  43. $this->forwardToSecureAction();
  44. }
  45. // the user has access, continue
  46. $filterChain->execute();
  47. }
  48. /**
  49. * Forwards the current request to the secure action.
  50. *
  51. * @throws sfStopException
  52. */
  53. protected function forwardToSecureAction()
  54. {
  55. $this->context->getController()->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));
  56. throw new sfStopException();
  57. }
  58. /**
  59. * Forwards the current request to the login action.
  60. *
  61. * @throws sfStopException
  62. */
  63. protected function forwardToLoginAction()
  64. {
  65. $this->context->getController()->forward(sfConfig::get('sf_login_module'), sfConfig::get('sf_login_action'));
  66. throw new sfStopException();
  67. }
  68. /**
  69. * Returns the credential required for this action.
  70. *
  71. * @return mixed The credential required for this action
  72. */
  73. protected function getUserCredential()
  74. {
  75. return $this->context->getController()->getActionStack()->getLastEntry()->getActionInstance()->getCredential();
  76. }
  77. }

Debug toolbar