/** * XUP Signup Method (falls-back to XUP login when existing user is detected). * May be used as a simple XUP login link for existing and non-existing users. */ public function signup($redirectUrl = true, $loginAfterSuccess = true, $emailAfterSuccess = true) { if (!e107::getPref('social_login_active', false)) { throw new Exception("Signup failed! This feature is disabled.", 100); // TODO lan } if (!$this->getProvider()) { throw new Exception("Signup failed! Wrong provider.", 2); // TODO lan } if ($redirectUrl) { if (true === $redirectUrl) { $redirectUrl = SITEURL; } elseif (strpos($redirectUrl, 'http://') !== 0 && strpos($redirectUrl, 'https://') !== 0) { $redirectUrl = e107::getUrl()->create($redirectUrl); } } if (e107::getUser()->isUser()) { if ($redirectUrl) { e107::getRedirect()->redirect($redirectUrl); } return false; // throw new Exception( "Signup failed! User already signed in. ", 1); // TODO lan } $this->adapter = $this->hybridauth->authenticate($this->getProvider()); $profile = $this->adapter->getUserProfile(); // returned back, if success... if ($profile->identifier) { $sql = e107::getDb(); $userMethods = e107::getUserSession(); $plainPwd = $userMethods->generateRandomString('************'); // auto plain passwords // TODO - auto login name, shouldn't be used if system set to user_email login... $userdata['user_loginname'] = $this->getProvider() . $userMethods->generateUserLogin(e107::getPref('predefinedLoginName', '_..#..#..#')); $userdata['user_email'] = $sql->escape($profile->emailVerified ? $profile->emailVerified : $profile->email); $userdata['user_name'] = $sql->escape($profile->displayName); $userdata['user_login'] = $userdata['user_name']; $userdata['user_customtitle'] = ''; // not used $userdata['user_password'] = $userMethods->HashPassword($plainPwd, $userdata['user_loginname']); // pwd $userdata['user_sess'] = ''; // $userdata['user_image'] = $profile->photoURL; // avatar $userdata['user_signature'] = ''; // not used $userdata['user_hideemail'] = 1; // hide it by default $userdata['user_xup'] = $sql->escape($this->userId()); $pref = e107::pref('core'); if (!empty($pref['initial_user_classes'])) { $userdata['user_class'] = $pref['initial_user_classes']; } elseif (!empty($pref['user_new_period'])) { $userdata['user_class'] = e_UC_NEWUSER; } else { $userdata['user_class'] = ''; } // print_a($userdata); // user_name, user_xup, user_email and user_loginname shouldn't match $insert = !empty($userdata['user_email']) ? "OR user_email='" . $userdata['user_email'] . "' " : ""; if ($sql->count("user", "(*)", "user_xup='" . $sql->escape($this->userId()) . "' " . $insert . " OR user_loginname='{$userdata['user_loginname']}' OR user_name='{$userdata['user_name']}'")) { // $this->login($redirectUrl); // auto-login e107::getUser()->loginProvider($this->userId()); if ($redirectUrl) { e107::getRedirect()->redirect($redirectUrl); } return false; // throw new Exception( "Signup failed! User already exists. Please use 'login' instead.", 3); } if (empty($userdata['user_email']) && e107::getPref('disable_emailcheck', 0) == 0) { throw new Exception("Signup failed! Can't access user email - registration without an email is impossible." . print_a($userdata, true), 4); // TODO lan } // other fields $now = time(); $userdata['user_id'] = null; $userdata['user_join'] = $now; $userdata['user_lastvisit'] = 0; $userdata['user_currentvisit'] = 0; $userdata['user_comments'] = 0; $userdata['user_ip'] = e107::getIPHandler()->getIP(FALSE); $userdata['user_ban'] = USER_VALIDATED; $userdata['user_prefs'] = ''; $userdata['user_visits'] = 0; $userdata['user_admin'] = 0; $userdata['user_perms'] = ''; $userdata['user_realm'] = ''; $userdata['user_pwchange'] = $now; $user = e107::getSystemUser(0, false); $user->setData($userdata); $user->getExtendedModel(); // init //$user->setEditor(e107::getSystemUser(1, false)); $user->save(true); // user model error if ($user->hasError()) { throw new Exception($user->renderMessages(), 5); } ### Successful signup! //$user->set('provider', $this->getProvider()); $userdata = $user->getData(); $userdata['provider'] = $this->getProvider(); // e107::getEvent()->trigger('userveri', $userdata); // Trigger New verified user. e107::getEvent()->trigger('user_xup_signup', $userdata); $ret = e107::getEvent()->trigger('usersupprov', $userdata); // XXX - it's time to pass objects instead of array? if (true === $ret) { return $this; } // send email if ($emailAfterSuccess) { $user->set('user_password', $plainPwd)->email('signup'); } e107::getUser()->setProvider($this); // auto login if ($loginAfterSuccess) { e107::getUser()->loginProvider($this->userId()); // if not proper after-login, return true so user can see login screen } if ($redirectUrl) { e107::getRedirect()->redirect($redirectUrl); } return true; } return false; }
/** * Get user record for HybridAuth adapter and try to get associated user record * from your application's database. * * If app user record is not found a 'HybridAuth.newUser' event is dispatched * with profile info from HyridAuth. The event listener should create associated * user record and return user entity as event result. * * @param \Hybrid_Provider_Model $adapter Hybrid auth adapter instance. * @return array User record * @throws \Exception Thrown when a profile cannot be retrieved. * @throws \RuntimeException If profile entity cannot be persisted. */ protected function _getUser($adapter) { try { $providerProfile = $adapter->getUserProfile(); $this->adapter($adapter); $this->profile($providerProfile); } catch (\Exception $e) { $adapter->logout(); throw $e; } $config = $this->_config; $userModel = $this->_userModel; $user = null; $profile = $this->_query($providerProfile->identifier)->first(); if ($profile) { $userId = $profile->get($config['profileModelFkField']); $user = $this->_userModel->find($config['finder'])->where([$userModel->aliasField($userModel->primaryKey()) => $userId])->first(); // User record exists but finder conditions did not match, // so just update social profile record and return false. if (!$user) { $profile = $this->_profileEntity($profile); if (!$this->_profileModel->save($profile)) { throw new \RuntimeException('Unable to save social profile.'); } return false; } } elseif ($providerProfile->email) { $user = $this->_userModel->find($config['finder'])->where([$this->_userModel->aliasField($config['fields']['email']) => $providerProfile->email])->first(); } $profile = $this->_profileEntity($profile); if (!$user) { $user = $this->_newUser($profile); } $profile->{$config['profileModelFkField']} = $user->{$userModel->primaryKey()}; $profile = $this->_profileModel->save($profile); if (!$profile) { throw new \RuntimeException('Unable to save social profile.'); } $user->set('social_profile', $profile); $user->unsetProperty($config['fields']['password']); return $user->toArray(); }