Guest User

Untitled

a guest
Jun 20th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. <?php
  2. /**
  3. * dsDynamicCredentialsFilter
  4. *
  5. * This filter is created to add some credentials to user dynamically.
  6. *
  7. * It does following things:
  8. *
  9. * 1. retrieves current route,
  10. * 2. checks whether it is sfObjectRoute
  11. * 3. retrieves an object inside of that sfObjectRoute
  12. * 4. calls $object->getAddedCredentialsNames($user) where $user is instance of myUser
  13. * 5. adds credentials returned by #4 to $user
  14. *
  15. * @author Andrei Dziahel <develop7@develop7.info>
  16. */
  17. class dsDynamicCredentialsFilter extends sfFilter
  18. {
  19. public function execute($filterChain)
  20. {
  21. if ($this->isFirstCall())
  22. {
  23. $context = $this->getContext();
  24.  
  25. /* @var $user sfGuardSecurityUser */
  26. $user = $context->getUser(); //TODO: fix hardcoded user instance
  27. $routename = $context->getRouting()->getCurrentRouteName();
  28. $routes = $context->getRouting()->getRoutes();
  29.  
  30. /* @var $route sfObjectRoute */
  31. $route = $routes[$routename];
  32.  
  33. if ($route instanceof sfObjectRoute && $route->isBound())
  34. {
  35. $object = false;
  36. try
  37. {
  38. $object = $route->getObject();
  39. }
  40. catch (LogicException $e)
  41. {
  42. $object = false;
  43. }
  44.  
  45. if (false !== $object && method_exists($object, 'getAddedCredentialsNames'))
  46. {
  47. $names = call_user_func(array($object, 'getAddedCredentialsNames'), $user); //TODO: fix hardcoded method name
  48. $user->addCredentials($names);
  49. }
  50. else
  51. {
  52. if (sfConfig::get('sf_logging_enabled'))
  53. {
  54. $context->getEventDispatcher()->notify(new sfEvent($this,
  55. 'application.log',
  56. array('Can\'t retrieve object for route "'.$routename.'", skipping')));
  57. }
  58. }
  59. }
  60. }
  61.  
  62. $filterChain->execute();
  63. }
  64. }
Add Comment
Please, Sign In to add comment