Пример #1
0
 public function callback()
 {
     // from querystring?
     $returnto = Ajde::app()->getRequest()->getParam('returnto', '');
     if (empty($returnto)) {
         $returnto = Ajde_Http_Response::REDIRECT_HOMEPAGE;
     }
     // from session?
     $returntoSession = new Ajde_Session('returnto');
     if ($returntoSession->has('url')) {
         $returnto = $returntoSession->get('url');
         $returntoSession->destroy();
     }
     if (!$this->_provider->isAuthenticated()) {
         Ajde_Session_Flash::alert('Permission request cancelled for ' . ucfirst($this->_providername));
         $this->redirect($returnto);
         return false;
     }
     // We already have a user for this SSO, log that user in and redirect
     if ($user = $this->_provider->getUser()) {
         if ($this->getLoggedInUser()) {
             Ajde_Session_Flash::alert(ucfirst($this->_providername) . ' user ' . $this->_provider->getUsernameSuggestion() . ' is already connected to another account.');
             $this->redirect($returnto);
         } else {
             $user->login();
             $user->storeCookie(false);
             $this->redirect($returnto);
         }
     } else {
         // A user is already logged in, link this account and redirect
         if ($user = $this->getLoggedInUser()) {
             $sso = new SsoModel();
             $sso->populate(['user' => $user->getPK(), 'provider' => $this->_providername, 'username' => $this->_provider->getUsernameSuggestion(), 'avatar' => $this->_provider->getAvatarSuggestion(), 'profile' => $this->_provider->getProfileSuggestion(), 'uid' => $this->_provider->getUidHash(), 'data' => serialize($this->_provider->getData())]);
             $sso->insert();
             $user->copyAvatarFromSso($sso);
             $this->redirect($returnto);
             // No match found, redirect to register page
         } else {
             $username = $this->_provider->getUsernameSuggestion();
             $email = $this->_provider->getEmailSuggestion();
             $fullname = $this->_provider->getNameSuggestion();
             $this->redirect('user/register?provider=' . $this->_providername . '&username='******'&email=' . esc($email) . '&fullname=' . esc($fullname) . '&hidepassword=1&returnto=' . $returnto);
         }
     }
 }
Пример #2
0
 public function registerJson()
 {
     $user = new UserModel();
     $returnto = Ajde::app()->getRequest()->getPostParam('returnto', false);
     $username = Ajde::app()->getRequest()->getPostParam($user->usernameField);
     $password = Ajde::app()->getRequest()->getPostParam('password', '');
     $passwordCheck = Ajde::app()->getRequest()->getPostParam('passwordCheck', '');
     $providername = Ajde::app()->getRequest()->getPostParam('provider', false);
     $email = Ajde::app()->getRequest()->getPostParam('email', false);
     $fullname = Ajde::app()->getRequest()->getPostParam('fullname', false);
     $return = [false];
     $shadowUser = new UserModel();
     $provider = false;
     if ($providername) {
         $sso = config('user.sso.providers');
         if (!in_array($providername, $sso)) {
             Ajde_Http_Response::redirectNotFound();
         }
         $classname = 'Ajde_User_Sso_' . ucfirst($providername);
         /* @var $provider Ajde_User_SSO_Interface */
         $provider = new $classname();
     }
     if (empty($username)) {
         $return = ['success' => false, 'message' => trans('Please provide a ' . $user->usernameField . '')];
     } else {
         if (!$provider && empty($password)) {
             $return = ['success' => false, 'message' => trans('Please provide a password')];
         } else {
             if ($shadowUser->loadByField($shadowUser->usernameField, $username)) {
                 $return = ['success' => false, 'message' => trans(ucfirst($user->usernameField) . ' already exist')];
             } else {
                 if (!$provider && $password !== $passwordCheck) {
                     $return = ['success' => false, 'message' => trans('Passwords do not match')];
                 } else {
                     if (empty($email)) {
                         $return = ['success' => false, 'message' => trans('Please provide an e-mail address')];
                     } else {
                         if (Ajde_Component_String::validEmail($email) === false) {
                             $return = ['success' => false, 'message' => trans('Please provide a valid e-mail address')];
                         } else {
                             if ($shadowUser->loadByField('email', $email)) {
                                 $return = ['success' => false, 'message' => trans('A user with this e-mail address already exist')];
                             } else {
                                 if (empty($fullname)) {
                                     $return = ['success' => false, 'message' => trans('Please provide a full name')];
                                 } else {
                                     if ($provider && !$provider->getData()) {
                                         $return = ['success' => false, 'message' => trans('Something went wrong with fetching your credentials from an external service')];
                                     } else {
                                         $user->set('email', $email);
                                         $user->set('fullname', $fullname);
                                         if ($user->add($username, $password)) {
                                             if ($provider) {
                                                 $sso = new SsoModel();
                                                 $sso->populate(['user' => $user->getPK(), 'provider' => $providername, 'username' => $provider->getUsernameSuggestion(), 'avatar' => $provider->getAvatarSuggestion(), 'profile' => $provider->getProfileSuggestion(), 'uid' => $provider->getUidHash(), 'data' => serialize($provider->getData())]);
                                                 $sso->insert();
                                                 $user->copyAvatarFromSso($sso);
                                             }
                                             $user->login();
                                             $user->storeCookie($this->includeDomain);
                                             Ajde_Session_Flash::alert(sprintf(trans('Welcome %s, you are now logged in'), $fullname));
                                             $return = ['success' => true, 'returnto' => $returnto];
                                         } else {
                                             $return = ['success' => false, 'message' => trans('Something went wrong')];
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     return $return;
 }