Example #1
0
 public function validateAuthentication($nameOrEmail, $password, &$error = '')
 {
     $userId = parent::validateAuthentication($nameOrEmail, $password, $error);
     if (empty($userId) and strpos($nameOrEmail, '@') === false and bdApiConsumer_Option::get('takeOver', 'login')) {
         // try to login with external providers
         $providers = bdApiConsumer_Option::getProviders();
         foreach ($providers as $provider) {
             $externalToken = bdApiConsumer_Helper_Api::getAccessTokenFromUsernamePassword($provider, $nameOrEmail, $password);
             if (empty($externalToken)) {
                 continue;
             }
             $externalVisitor = bdApiConsumer_Helper_Api::getVisitor($provider, $externalToken['access_token']);
             if (empty($externalVisitor)) {
                 continue;
             }
             /** @var bdApiConsumer_XenForo_Model_UserExternal $userExternalModel */
             $userExternalModel = $this->getModelFromCache('XenForo_Model_UserExternal');
             $existingAssoc = $userExternalModel->getExternalAuthAssociation($userExternalModel->bdApiConsumer_getProviderCode($provider), $externalVisitor['user_id']);
             if (!empty($existingAssoc)) {
                 // yay, found an associated user!
                 $error = '';
                 $userExternalModel->bdApiConsumer_updateExternalAuthAssociation($provider, $externalVisitor['user_id'], $existingAssoc['user_id'], $externalVisitor + array('token' => $externalToken));
                 return $existingAssoc['user_id'];
             }
             $existingUser = $this->getUserByEmail($externalVisitor['user_email']);
             if (!empty($existingUser)) {
                 // this is not good, an user with matched email
                 // this user will have to associate manually
                 continue;
             }
             $sameName = $this->getUserByName($externalVisitor['username']);
             if (!empty($sameName)) {
                 // not good
                 continue;
             }
             $data = array('username' => $externalVisitor['username']);
             if (bdApiConsumer_Option::get('autoRegister') === 'id_sync') {
                 // additionally look for user with same ID
                 $sameId = $this->getUserById($externalVisitor['user_id']);
                 if (!empty($sameId)) {
                     // not good
                     continue;
                 }
                 $data['user_id'] = $externalVisitor['user_id'];
             }
             $user = bdApiConsumer_Helper_AutoRegister::createUser($data, $provider, $externalToken, $externalVisitor, $userExternalModel);
             if (!empty($user)) {
                 $error = '';
                 return $user['user_id'];
             }
         }
     }
     return $userId;
 }
Example #2
0
 protected function _handleUserPings(array $provider, array &$pings)
 {
     $userExternalModel = $this->getModelFromCache('XenForo_Model_UserExternal');
     $providerKeys = array();
     foreach ($pings as &$ping) {
         $providerKeys[] = $ping['object_data'];
     }
     $auths = $userExternalModel->bdApiConsumer_getExternalAuthAssociationsForProviderUser($provider, $providerKeys);
     foreach ($auths as $auth) {
         $accessToken = $userExternalModel->bdApiConsumer_getAccessTokenFromAuth($provider, $auth);
         if (empty($accessToken)) {
             continue;
         }
         $externalVisitor = bdApiConsumer_Helper_Api::getVisitor($provider, $accessToken, false);
         if (empty($externalVisitor)) {
             continue;
         }
         $userExternalModel->bdApiConsumer_updateExternalAuthAssociation($provider, $auth['provider_key'], $auth['user_id'], array_merge($auth['extra_data'], $externalVisitor));
         foreach ($pings as &$ping) {
             if ($ping['object_data'] == $auth['provider_key']) {
                 $ping['result'] = 'updated user data';
             }
         }
     }
 }
Example #3
0
 public function actionExternalRegister()
 {
     $this->_assertPostOnly();
     $redirect = $this->_bdApiConsumer_getRedirect();
     $userModel = $this->_getUserModel();
     /** @var bdApiConsumer_XenForo_Model_UserExternal $userExternalModel */
     $userExternalModel = $this->_getUserExternalModel();
     $providerCode = $this->_input->filterSingle('provider', XenForo_Input::STRING);
     $provider = bdApiConsumer_Option::getProviderByCode($providerCode);
     if (empty($provider)) {
         return $this->responseNoPermission();
     }
     $doAssoc = $this->_input->filterSingle('associate', XenForo_Input::STRING) || $this->_input->filterSingle('force_assoc', XenForo_Input::UINT);
     $userId = 0;
     if ($doAssoc) {
         $associate = $this->_input->filter(array('associate_login' => XenForo_Input::STRING, 'associate_password' => XenForo_Input::STRING));
         $loginModel = $this->_getLoginModel();
         if ($loginModel->requireLoginCaptcha($associate['associate_login'])) {
             return $this->responseError(new XenForo_Phrase('your_account_has_temporarily_been_locked_due_to_failed_login_attempts'));
         }
         $userId = $userModel->validateAuthentication($associate['associate_login'], $associate['associate_password'], $error);
         if (!$userId) {
             $loginModel->logLoginAttempt($associate['associate_login']);
             return $this->responseError($error);
         }
     }
     $refreshToken = $this->_input->filterSingle('refresh_token', XenForo_Input::STRING);
     $externalToken = bdApiConsumer_Helper_Api::getAccessTokenFromRefreshToken($provider, $refreshToken);
     if (empty($externalToken)) {
         return $this->responseError(new XenForo_Phrase('bdapi_consumer_error_occurred_while_connecting_with_x', array('provider' => $provider['name'])));
     }
     $externalVisitor = bdApiConsumer_Helper_Api::getVisitor($provider, $externalToken['access_token']);
     if (empty($externalVisitor)) {
         return $this->responseError(new XenForo_Phrase('bdapi_consumer_error_occurred_while_connecting_with_x', array('provider' => $provider['name'])));
     }
     if (empty($externalVisitor['user_email'])) {
         return $this->responseError(new XenForo_Phrase('bdapi_consumer_x_returned_unknown_error', array('provider' => $provider['name'])));
     }
     if (isset($externalVisitor['user_is_valid']) and isset($externalVisitor['user_is_verified'])) {
         if (empty($externalVisitor['user_is_valid']) or empty($externalVisitor['user_is_verified'])) {
             return $this->responseError(new XenForo_Phrase('bdapi_consumer_x_account_not_good_standing', array('provider' => $provider['name'])));
         }
     }
     if ($doAssoc) {
         $userExternalModel->bdApiConsumer_updateExternalAuthAssociation($provider, $externalVisitor['user_id'], $userId, array_merge($externalVisitor, array('token' => $externalToken)));
         XenForo_Application::getSession()->changeUserId($userId);
         XenForo_Visitor::setup($userId);
         return $this->responseRedirect(XenForo_ControllerResponse_Redirect::SUCCESS, $redirect);
     }
     if (bdApiConsumer_Option::get('bypassRegistrationActive')) {
         // do not check for registration active option
     } else {
         $this->_assertRegistrationActive();
     }
     $data = $this->_input->filter(array('username' => XenForo_Input::STRING, 'timezone' => XenForo_Input::STRING));
     // TODO: custom fields
     if (XenForo_Dependencies_Public::getTosUrl() && !$this->_input->filterSingle('agree', XenForo_Input::UINT)) {
         return $this->responseError(new XenForo_Phrase('you_must_agree_to_terms_of_service'));
     }
     $user = bdApiConsumer_Helper_AutoRegister::createUser($data, $provider, $externalToken, $externalVisitor, $this->_getUserExternalModel());
     XenForo_Application::getSession()->changeUserId($user['user_id']);
     XenForo_Visitor::setup($user['user_id']);
     $viewParams = array('user' => $user, 'redirect' => $redirect);
     return $this->responseView('XenForo_ViewPublic_Register_Process', 'register_process', $viewParams, $this->_getRegistrationContainerParams());
 }