Пример #1
0
 /**
  * Oauth Connect
  *
  */
 public function oauthAction()
 {
     $namespace = $this->_getOauthStorage();
     $info = $namespace->info;
     $users = new Users_Model_User_Table();
     if (empty($info->email)) {
         $row = $users->getByTwitterid($info->twitterId);
     } else {
         $row = $users->getByEmail($info->email);
         if (!$row) {
             if (self::OAUTH_FACEBOOK == $this->_getParam('type')) {
                 $row = $users->getByFacebookid($info->facebookId);
             } elseif (self::OAUTH_GOOGLE == $this->_getParam('type')) {
                 $row = $users->getByGoogleid($info->googleId);
             }
         }
     }
     if (!$row) {
         $loginFilter = new Zend_Filter_Alnum();
         $info->login = $loginFilter->filter($info->login);
         if ($users->getByLogin($info->login)) {
             $form = new Users_Form_Auth_RegisterLogin();
             if ($this->getRequest()->isPost() && $form->isValid($this->_getAllParams())) {
                 $info->login = $form->getValue('login');
             } else {
                 $this->view->login = $info->login;
                 $this->view->form = $form;
                 return;
             }
         }
         $row = $users->createRow($info->getArrayCopy());
         $row->role = Users_Model_User::ROLE_USER;
         $row->status = Users_Model_User::STATUS_ACTIVE;
         $row->save();
     }
     $row->login();
     $namespace->unsetAll();
     $this->_helper->flashMessenger->addMessage('Now You\'re Logging!');
     $this->_helper->redirector(false, false, false);
 }
Пример #2
0
 /**
  * @param array $authData
  * @throws Zend_Controller_Action_Exception
  */
 private function _oauthLogin($authData)
 {
     if (isset($authData['auth']['uid'])) {
         $users = new Users_Model_User_Table();
         switch ($authData['auth']['provider']) {
             case 'Facebook':
                 $serviceFieldName = 'facebookId';
                 $row = $users->getByFacebookid($authData['auth']['uid']);
                 if (!$row) {
                     if (isset($authData['auth']['info']['email'])) {
                         //If exist user's email
                         $row = $users->getByEmail($authData['auth']['info']['email']);
                         if ($row) {
                             $row->facebookId = $authData['auth']['uid'];
                             $row->save();
                         }
                     }
                 }
                 break;
             case 'Twitter':
                 $serviceFieldName = 'twitterId';
                 $row = $users->getByTwitterid($authData['auth']['uid']);
                 break;
             case 'Google':
                 $serviceFieldName = 'googleId';
                 $row = $users->getByGoogleid($authData['auth']['uid']);
                 if (!$row) {
                     if (isset($authData['auth']['info']['email'])) {
                         $authData['auth']['info']['nickname'] = $authData['auth']['info']['email'];
                         //If exist user's email
                         $row = $users->getByEmail($authData['auth']['info']['email']);
                         if ($row) {
                             $row->googleId = $authData['auth']['uid'];
                             $row->save();
                         }
                     }
                 }
                 break;
             default:
                 throw new Zend_Controller_Action_Exception('Incorrect provider.');
                 break;
         }
         //Create user
         if (!$row) {
             if ($users->getByLogin($authData['auth']['info']['nickname'])) {
                 //Is not allow nickname
                 throw new Zend_Controller_Action_Exception('Login is occupied.');
             } else {
                 //Is allow nickname
                 $row = $users->createRow();
                 //Insert user data if exist
                 if (isset($authData['auth']['info']['nickname'])) {
                     $row->login = $authData['auth']['info']['nickname'];
                 }
                 if (isset($authData['auth']['info']['email'])) {
                     $row->email = $authData['auth']['info']['email'];
                 }
                 if (isset($authData['auth']['info']['first_name'])) {
                     $row->firstname = $authData['auth']['info']['first_name'];
                 }
                 if (isset($authData['auth']['info']['last_name'])) {
                     $row->lastname = $authData['auth']['info']['last_name'];
                 }
                 //service userId
                 $row->{$serviceFieldName} = $authData['auth']['uid'];
                 $row->role = Users_Model_User::ROLE_USER;
                 $row->status = Users_Model_User::STATUS_ACTIVE;
                 $row->save();
             }
         }
         $row->login();
         $this->_helper->flashMessenger->addMessage('Now You\'re Logging!');
         $this->_helper->redirector(false, false, false);
     } else {
         throw new Zend_Controller_Action_Exception('Invalid auth response.');
     }
 }