IsGrantedPreviousAdminExtension.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace Base\OAuthClientBundle\Twig;
  3. use WebserviceBundle\Services\Webservice;
  4. class IsGrantedPreviousAdminExtension extends \Twig_Extension
  5. {
  6. /**
  7. * @var Webservice
  8. */
  9. protected $webservice;
  10. /**
  11. * @var string
  12. */
  13. protected $urlBase;
  14. /**
  15. * @param Webservice $webservice
  16. * @param string $urlBase
  17. */
  18. public function __construct(Webservice $webservice, $urlBase)
  19. {
  20. $this->webservice = $webservice;
  21. $this->urlBase = $urlBase;
  22. }
  23. /**
  24. * @return array
  25. */
  26. public function getFunctions()
  27. {
  28. return array(
  29. new \Twig_SimpleFunction('is_granted_previous_admin', array($this, 'isGrantedPreviousAdmin')),
  30. new \Twig_SimpleFunction('switch_user_exit', array($this, 'switchUserExit')),
  31. );
  32. }
  33. /**
  34. * @return boolean
  35. */
  36. public function isGrantedPreviousAdmin()
  37. {
  38. try {
  39. return $this->webservice->makeGetRequest($this->urlBase . '/admin/user/is_granted');
  40. } catch (\Exception $e) {
  41. return false;
  42. }
  43. }
  44. /**
  45. * @return boolean
  46. */
  47. public function switchUserExit()
  48. {
  49. return 'https://' . $this->urlBase . '/?_switch_user=_exit';
  50. }
  51. /**
  52. * @return string
  53. */
  54. public function getName()
  55. {
  56. return 'is_granted_previous_admin_extension';
  57. }
  58. }