Guest User

Untitled

a guest
Jan 11th, 2018
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. "google/apiclient": "^2.0"
  2.  
  3. 'Google' =>
  4. [
  5. 'googleClientID' => '123456.apps.googleusercontent.com',
  6. 'googleClientSecret' => 'abcdefghi',
  7. 'googleRedirectUrl' => 'http://example.com/oauth2callback'
  8. ]
  9.  
  10. $routes->connect('/oauth2callback', ['controller' => 'Account', 'action' => 'confirmlogin'], ['_name' => 'account-google-redirect-url']);
  11.  
  12. /**
  13. * Gmail login method
  14. */
  15.  
  16. public function googlelogin()
  17. {
  18.  
  19.  
  20. $client = new Google_Client();
  21. $client->setClientId(Configure::read('Google.googleClientID'));
  22. $client->setClientSecret(Configure::read('Google.googleClientSecret'));
  23. $client->setRedirectUri(Configure::read('Google.googleRedirectUrl'));
  24. $client->se
  25.  
  26. tScopes([
  27. "https://www.googleapis.com/auth/userinfo.profile",
  28. 'https://www.googleapis.com/auth/userinfo.email'
  29. ]);
  30. $url = $client->createAuthUrl();
  31. $this->redirect($url);
  32. }
  33.  
  34. public function confirmlogin()
  35. {
  36. $client = new Google_Client();
  37. $client->setClientId(Configure::read('Google.googleClientID'));
  38. $client->setClientSecret(Configure::read('Google.googleClientSecret'));
  39. $client->setRedirectUri(Configure::read('Google.googleRedirectUrl'));
  40. $client->setScopes([
  41. "https://www.googleapis.com/auth/userinfo.profile",
  42. 'https://www.googleapis.com/auth/userinfo.email'
  43. ]);
  44. $client->setApprovalPrompt('auto');
  45. $usersTable = TableRegistry::get('Users');
  46. if (isset($this->request->query['code'])) {
  47. $client->authenticate($this->request->query['code']);
  48. $this->request->Session()->write('access_token', $client->getAccessToken());
  49. }
  50. if ($this->request->Session()->check('access_token') && ($this->request->Session()->read('access_token'))) {
  51. $client->setAccessToken($this->request->Session()->read('access_token'));
  52. }
  53. if ($client->getAccessToken()) {
  54. $this->request->Session()->write('access_token', $client->getAccessToken());
  55. $oauth2 = new Google_Service_Oauth2($client);
  56. $user = $oauth2->userinfo->get();
  57. try {
  58. if (!empty($user)) {
  59. if ((preg_match("/(@example.com)$/", $user['email'])) || (preg_match("/(@example.in)$/", $user['email']))) {
  60. $result = $usersTable->find('all')
  61. ->where(['email' => $user['email']])
  62. ->first();
  63. if (!empty($result)) {
  64. $this->AccessControl->setUser($result->toArray(), false);
  65. $this->Flash->set(__('You have successfuly logged in.'), ['element' => 'success']);
  66. $this->redirect(['_name' => 'dashboard']);
  67. } else {
  68. $data = [];
  69. $data['email'] = $user['email'];
  70. $data['first_name'] = $user['givenName'];
  71. $data['last_name'] = $user['familyName'];
  72. $data['socialId'] = $user['id'];
  73. $data['role_id'] = Configure::read('Role.loginWithGmailUserRole');
  74. //$data matches my Users table
  75. $entity = $usersTable->newEntity($data);
  76. if ($usersTable->save($entity)) {
  77. $data['id'] = $entity->id;
  78. $this->AccessControl->setUser($data, false);
  79. $this->Flash->set(__('You have successfuly logged in.'), ['element' => 'success']);
  80. $this->redirect(['_name' => 'dashboard']);
  81. } else {
  82. $this->Flash->error(__('Invalid login.'));
  83. //redirect to login action
  84. $this->redirect(['_name' => 'account-login']);
  85. }
  86. }
  87. } else {
  88. $this->Flash->error(__('Your email is invalid for this application.'));
  89. //redirect to login action
  90. $this->redirect(['_name' => 'account-login']);
  91. }
  92. } else {
  93. $this->Flash->error(__('Gmail infos not found.'));
  94. //redirect to login action
  95. return $this->redirect(['_name' => 'account-login']);
  96. }
  97. } catch (Exception $e) {
  98. $this->Flash->error(__('Gmail error.'));
  99. return $this->redirect(['_name' => 'account-login']);
  100. }
  101. }
  102. }
Add Comment
Please, Sign In to add comment