Created
August 31, 2012 10:51
-
-
Save rafops/3551403 to your computer and use it in GitHub Desktop.
OAuth2 Controller Zend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class AdminController extends Zend_Controller_Action | |
{ | |
public function init() | |
{ | |
$this->_googleConfig = Zend_Registry::get('google'); | |
$this->_model = new Application_Model_Manager(); | |
} | |
public function indexAction() | |
{ | |
$this->_redirect('/manager'); | |
} | |
public function loginAction() | |
{ | |
$url = $this->_googleConfig->oauth2_url . '/auth'; | |
$params = array( | |
'client_id' => $this->_googleConfig->client_id, | |
'redirect_uri' => $this->view->serverUrl() . '/admin/callback', | |
'response_type' => 'code', | |
'scope' => $this->_googleConfig->scope | |
); | |
$this->_redirect($url . '?' . http_build_query($params)); | |
} | |
public function callbackAction() | |
{ | |
$url = $this->_googleConfig->oauth2_url . '/token'; | |
$params = array( | |
'code' => $_GET['code'], | |
'client_id' => $this->_googleConfig->client_id, | |
'client_secret' => $this->_googleConfig->client_secret, | |
'redirect_uri' => $this->view->serverUrl() . '/admin/callback', | |
'grant_type' => 'authorization_code' | |
); | |
$client = new Zend_Http_Client($url); | |
$client->setMethod(Zend_Http_Client::POST); | |
$client->setParameterPost($params); | |
$accessToken = null; | |
try { | |
$response = $client->request(); | |
$decoded = Zend_Json::decode($response->getBody()); | |
if(is_array($decoded) && array_key_exists('access_token', $decoded)) { | |
$accessToken = $decoded['access_token']; | |
} | |
} catch (Exception $exception) { | |
$handler = new Application_Model_CustomErrorHandler($exception->getMessage()); | |
$handler->save(); | |
} | |
$url = $this->_googleConfig->api_url . '/userinfo'; | |
$params = array( | |
'access_token' => $accessToken | |
); | |
$client = new Zend_Http_Client($url); | |
$client->setMethod(Zend_Http_Client::GET); | |
$client->setParameterGet($params); | |
$email = null; | |
try { | |
$response = $client->request(); | |
$decoded = Zend_Json::decode($response->getBody()); | |
if(is_array($decoded) && array_key_exists('email', $decoded)) { | |
$email = $decoded['email']; | |
} | |
} catch(Exception $exception) { | |
$handler = new Application_Model_CustomErrorHandler($exception->getMessage()); | |
$handler->save(); | |
} | |
if(($manager = $this->_model->findByEmail($email)) instanceof Zend_Db_Table_Row) { | |
$managerSession = new Zend_Session_Namespace('manager'); | |
$managerSession->email = $manager->email; | |
$this->_redirect('/admin'); | |
} | |
$this->_redirect('/'); | |
} | |
public function logoutAction() | |
{ | |
$managerSession = new Zend_Session_Namespace('manager'); | |
$managerSession->email = null; | |
$this->_redirect('/'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment