request->session()->put($this->sessionKey, $data); $this->startData = $data; return; } # Load data if (is_null($this->startData)) { $this->startData = $this->request->session()->get($this->sessionKey, []); if (! empty($this->startData['id'])) { $this->startData['user'] = $this->userDataById($this->startData['id']); } } # Get data value by key if (is_string($data)) { return $this->startData[$data] ?? $default; } return $this->startData; } /** * Load user data by id * @param int $userId * @return array */ public function userDataById($userId) { return Users::model()->userData($userId, [ 'user_id', 'name', 'activated', 'activate_key', 'blocked', 'blocked_reason', 'password_salt', 'email', 'email_verified', 'phone_number', 'phone_number_verified', ]); } /** * User id (started registration) * @return int */ public function userId() { $data = $this->startData(); return !empty($data['id']) ? $data['id'] : 0; } /** * User data (started registration) * @param string|null $key * @param mixed $default * @return mixed */ public function user(?string $key = null, $default = null) { $data = $this->startData(); if (is_string($key)) { return $data['user'][$key] ?? $default; } return (!empty($data['user']) ? $data['user'] : []); } /** * Registration url * @param string $step * @param array $query * @return string */ public function registrationUrl(string $step, array $query = []) { if ($step !== 'start') { $query['step'] = $step; } return Users::url('register', $query); } /** * Login url * @param array $query * @return string */ public function loginUrl(array $query = []) { return Users::url('login', $query); } /** * Forgot url * @param array $query * @return string */ public function forgotUrl(array $query = []) { return Users::url('forgot', $query); } /** * Remove temporary registration data from session */ public function finishRegistration() { $this->request->session()->pull($this->sessionKey); } /** * Show simple title & message page * @param string $title * @param string $message * @return string */ public function authMessage(string $title, string $message) { # Mark such system page as "noindex" $this->getParentPage()->seo->index(false, 'users auth form page: system message'); # Every page should have title $this->app->setMeta($title); return Users::template('auth/message', [ 'title' => $title, 'message' => $message, ]); } /** * Only Email* is required for registration * @return bool */ public function emailOnly() { return Users::registerPhone(Users::REGISTER_TYPE_EMAIL); } /** * Phone* and Email* are required for registration * @return bool */ public function phoneAndEmail() { return Users::registerPhone(Users::REGISTER_TYPE_BOTH); } /** * Only Phone* is required for registration * @return bool */ public function phoneOnly() { return Users::registerPhone(Users::REGISTER_TYPE_PHONE); } /** * Validate user name * @param string $name * @return string */ public function validateName(string $name) { $clean = ['name' => &$name]; Users::i()->cleanUserData($clean, ['name'], ['name_length' => mb_strlen($name)]); return $name; } /** * Validate email address * @param string $email * @param string $fieldName * @return bool */ public function validateEmail(string &$email, $fieldName = 'email') { if (! $this->input->isEmail($email)) { $this->errors->set(_t('users', 'Incorrect email'), $fieldName); return false; } if (Users::isEmailTemporary($email)) { $this->errors->set(_t('', 'The email address you provided is in the list of forbidden ones, use for example @gmail.com'), $fieldName); return false; } if ($banned = Users::checkBan($this->request->remoteAddress(), $email)) { $this->errors->set(_t('users', 'Access denied due to: [reason]', [ 'reason' => $banned, ])); return false; } return true; } /** * Check if user account with same email already exists * @param string $email * @param int $exceptUserId * @return bool */ public function isEmailExists(string $email, $exceptUserId = 0) { return Users::model()->userEmailExists($email, $exceptUserId); } /** * Validate phone number * @param string $phone * @param string $fieldName * @return bool */ public function validatePhone(string &$phone, $fieldName = 'phone_number') { if (! $this->input->isPhoneNumber($phone)) { $this->errors->set(_t('users', 'Incorrect phone number'), $fieldName); return false; } return true; } /** * Use phone number field in form * @return bool */ public function hasPhoneNumber() { return ( $this->phoneOnly() || $this->phoneAndEmail() ); } /** * Check if user account with same phone number already exists * @param string $phone * @param int $exceptUserId * @param string $fieldName * @return bool */ public function isPhoneExists(string $phone, $exceptUserId = 0, $fieldName = 'phone_number') { return Users::model()->userPhoneExists($phone, $exceptUserId, $fieldName); } /** * Задействовать: пользовательское соглашение * @return bool */ public function hasAgreement() { return $this->config('users.register.agreement', true, TYPE_BOOL); } /** * Validate agreement (if enabled) * @param mixed $agreement * @param string $fieldName * @return bool */ public function validateAgreement($agreement, $fieldName = 'agreement') { if ($this->hasAgreement() && ! $agreement) { $this->errors->set(_t('users', 'Please confirm that you agree with the user agreement'), $fieldName); return false; } return true; } /** * Update registering user password * @param int $userId * @param string $newPassword * @return string */ public function updateUserPassword($userId, $newPassword) { Users::model()->userSave($userId, [ 'password' => $this->security->passwordHash($newPassword, $this->user('password_salt')), ]); return $newPassword; } }