Example #1
0
 /**
  * Searchs for an user with the given user id of the social media platform.
  * If there is no user, create one and directly log in.
  *
  * @param string $oauth_id the user id of the social media platform
  * @return bool
  * @throws OAuth_Exception
  */
 protected function getContrexxUser($oauth_id)
 {
     global $sessionObj;
     //\DBG::activate();
     $arrSettings = \User_Setting::getSettings();
     $provider = $this::OAUTH_PROVIDER;
     $FWUser = \FWUser::getFWUserObject();
     $objUser = $FWUser->objUser->getByNetwork($provider, $oauth_id);
     if (!$objUser) {
         // check whether the user is already logged in
         // if the user is logged in just add a new network to the user object
         if ($FWUser->objUser->login()) {
             $objUser = $FWUser->objUser;
             $this->addProviderToUserObject($provider, $oauth_id, $objUser);
             $objUser->getNetworks()->save();
             return true;
         }
         // create a new user with the default profile attributes
         $objUser = new \User();
         $objUser->setEmail($this->getEmail());
         $objUser->setAdminStatus(0);
         $objUser->setProfile(array('firstname' => array($this->getFirstname()), 'lastname' => array($this->getLastname())));
         $registrationRedirectNeeded = $arrSettings['sociallogin_show_signup']['status'];
         // if user_account_verification is true (1), then we need to do checkMandatoryCompliance(), because
         // the required fields must be set.
         if ($registrationRedirectNeeded == false && $arrSettings['user_account_verification']['value'] === 1) {
             $registrationRedirectNeeded = !$objUser->checkMandatoryCompliance();
         }
         $objUser->setActiveStatus(!$registrationRedirectNeeded);
         if ($registrationRedirectNeeded) {
             $objUser->setRestoreKey();
             $objUser->setRestoreKeyTime(intval($arrSettings['sociallogin_activation_timeout']['value']) * 60);
         }
         if (!empty($arrSettings['sociallogin_assign_to_groups']['value'])) {
             $groups = $arrSettings['sociallogin_assign_to_groups']['value'];
         } else {
             $groups = $arrSettings['assigne_to_groups']['value'];
         }
         $objUser->setGroups(explode(',', $groups));
         // if we can create the user without sign up page
         if (!$objUser->store()) {
             // if the email address already exists but not with the given oauth-provider
             throw new OAuth_Exception();
         }
         // add the social network to user
         $this->addProviderToUserObject($provider, $oauth_id, $objUser);
         $objUser->getNetworks()->save();
         // check whether there are empty mandatory fields or the setting to show sign up everytime
         if ($registrationRedirectNeeded) {
             // start session if no session is open
             if (!isset($sessionObj) || !is_object($sessionObj)) {
                 $sessionObj = \cmsSession::getInstance();
             }
             // write the user id to session so we can pre-fill the sign up form
             $_SESSION['user_id'] = $objUser->getId();
             // generate url for sign up page and redirect
             $signUpPageUri = \Cx\Core\Routing\Url::fromModuleAndCmd('Access', 'signup');
             \Cx\Core\Csrf\Controller\Csrf::header('Location: ' . $signUpPageUri->__toString());
             exit;
         }
     }
     $FWUser->loginUser($objUser);
 }