Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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('/');
- }
- }
Add Comment
Please, Sign In to add comment