Guest User

Untitled

a guest
Dec 15th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. <?php
  2.  
  3. class AdminController extends Zend_Controller_Action
  4. {
  5.  
  6. public function init()
  7. {
  8. $this->_googleConfig = Zend_Registry::get('google');
  9. $this->_model = new Application_Model_Manager();
  10. }
  11.  
  12. public function indexAction()
  13. {
  14. $this->_redirect('/manager');
  15. }
  16.  
  17. public function loginAction()
  18. {
  19. $url = $this->_googleConfig->oauth2_url . '/auth';
  20. $params = array(
  21. 'client_id' => $this->_googleConfig->client_id,
  22. 'redirect_uri' => $this->view->serverUrl() . '/admin/callback',
  23. 'response_type' => 'code',
  24. 'scope' => $this->_googleConfig->scope
  25. );
  26. $this->_redirect($url . '?' . http_build_query($params));
  27. }
  28.  
  29. public function callbackAction()
  30. {
  31. $url = $this->_googleConfig->oauth2_url . '/token';
  32. $params = array(
  33. 'code' => $_GET['code'],
  34. 'client_id' => $this->_googleConfig->client_id,
  35. 'client_secret' => $this->_googleConfig->client_secret,
  36. 'redirect_uri' => $this->view->serverUrl() . '/admin/callback',
  37. 'grant_type' => 'authorization_code'
  38. );
  39.  
  40. $client = new Zend_Http_Client($url);
  41. $client->setMethod(Zend_Http_Client::POST);
  42. $client->setParameterPost($params);
  43. $accessToken = null;
  44.  
  45. try {
  46. $response = $client->request();
  47. $decoded = Zend_Json::decode($response->getBody());
  48.  
  49. if(is_array($decoded) && array_key_exists('access_token', $decoded)) {
  50. $accessToken = $decoded['access_token'];
  51. }
  52. } catch (Exception $exception) {
  53. $handler = new Application_Model_CustomErrorHandler($exception->getMessage());
  54. $handler->save();
  55. }
  56.  
  57. $url = $this->_googleConfig->api_url . '/userinfo';
  58. $params = array(
  59. 'access_token' => $accessToken
  60. );
  61.  
  62. $client = new Zend_Http_Client($url);
  63. $client->setMethod(Zend_Http_Client::GET);
  64. $client->setParameterGet($params);
  65. $email = null;
  66.  
  67. try {
  68. $response = $client->request();
  69. $decoded = Zend_Json::decode($response->getBody());
  70.  
  71. if(is_array($decoded) && array_key_exists('email', $decoded)) {
  72. $email = $decoded['email'];
  73. }
  74. } catch(Exception $exception) {
  75. $handler = new Application_Model_CustomErrorHandler($exception->getMessage());
  76. $handler->save();
  77. }
  78.  
  79. if(($manager = $this->_model->findByEmail($email)) instanceof Zend_Db_Table_Row) {
  80. $managerSession = new Zend_Session_Namespace('manager');
  81. $managerSession->email = $manager->email;
  82. $this->_redirect('/admin');
  83. }
  84.  
  85. $this->_redirect('/');
  86. }
  87.  
  88. public function logoutAction()
  89. {
  90. $managerSession = new Zend_Session_Namespace('manager');
  91. $managerSession->email = null;
  92. $this->_redirect('/');
  93. }
  94. }
Add Comment
Please, Sign In to add comment