Beispiel #1
0
 /**
  * Выполним действие
  *
  * @param string $user_name  Имя пользователя
  * @param string $user_email Адрес электронной почты
  *
  * @return array
  *
  * @version 28.11.2016
  * @author Дмитрий Щербаков <*****@*****.**>
  */
 public function run($user_name, $user_email)
 {
     $result = ['error' => true, 'message' => 'Неизвестная ошибка.'];
     // Определим длину пароля
     $length = 10;
     // Создадим временный пароль
     $zxcvbn = new \ZxcvbnPhp\Zxcvbn();
     $password = $this->di->auth->getRandomKey($length);
     while ($zxcvbn->passwordStrength($password)['score'] < intval($this->di->auth->config->password_min_score)) {
         $password = $this->di->auth->getRandomKey($length);
     }
     // Добавим пользователя
     $registerResult = $this->di->auth->register($user_email, $password, $password);
     if ($registerResult['error'] === false) {
         $user_id = $this->di->auth->getUID($user_email);
         if ($user_id !== false) {
             $user_info = \ORM::for_table('users_info')->create();
             $user_info->uid = $user_id;
             $user_info->name = $user_name;
             $user_info->access = 'user';
             $user_info->created_at = Helpers\FormatTime::convert($this->di->cfg->time);
             $user_info->save();
             if (is_object($user_info) and isset($user_info->id)) {
                 // Отправим сообщение на почту
                 $this->di->mail->send($user_email, $user_name . ', добро пожаловать в "' . $this->di->auth->config->site_name . '"', 'USER_REGISTER', ['[[SITE_NAME]]' => $this->di->auth->config->site_name, '[[SITE_URL]]' => $this->di->auth->config->site_url, '[[USER_EMAIL]]' => $user_email, '[[USER_PASSWORD]]' => $password]);
                 // Войдем под этим пользователем
                 $login = new ActionLogin($this->di);
                 $result = $login->run($user_email, $password);
             } else {
                 \ORM::for_table('users')->where_equal('id', $user_id)->delete();
                 $result['message'] = 'Произошла ошибка при добавлении пользователя. Попробуйте ещё раз.';
             }
         } else {
             \ORM::for_table('users')->where_equal('email', $user_email)->delete();
             $result['message'] = 'Пользователь не зарегистрирован. Попробуйте ещё раз.';
         }
     } else {
         $result['message'] = $registerResult['message'];
     }
     return $result;
 }
Beispiel #2
0
 /**
  * Check if a password respects the site's password strength requirements.
  *
  * @param string $password
  *
  * @throws Exception
  */
 public static function validatePasswordStrength($password)
 {
     $zxcvbn = new \ZxcvbnPhp\Zxcvbn();
     $score = $zxcvbn->passwordStrength($password)['score'];
     if ($score < \PHPAuth\Configuration::PASSWORD_MINIMUM_SCORE) {
         throw new \Exception('password_weak');
     }
 }
/**
 * Validates and prepares values for 'register' action
 * @return void
 */
function forms_register_prepare_action_values()
{
    elgg_make_sticky_form('register');
    $first_name = get_input('first_name');
    $last_name = get_input('last_name');
    $email = get_input('email', '');
    $name = get_input('name');
    $username = get_input('username');
    $password = get_input('password');
    list($email_username) = explode('@', $email);
    if (elgg_get_plugin_setting('first_last_name', 'forms_register') && !$name) {
        if (!$first_name || !$last_name) {
            register_error(elgg_echo('actions:register:error:first_last_name'));
            forward(REFERRER);
        }
        set_input('name', "{$first_name} {$last_name}");
    } else {
        if (elgg_get_plugin_setting('autogen_name', 'forms_register') && !$name) {
            set_input('name', $email_username);
        }
    }
    if (elgg_get_plugin_setting('autogen_username', 'forms_register') && !$username) {
        $algo = elgg_get_plugin_setting('autogen_username_algo', 'forms_register', 'first_name_only');
        switch ($algo) {
            case 'first_name_only':
                $username = $first_name ?: $email_username;
                break;
            case 'full_name':
                $username = $first_name && $last_name ? "{$first_name}.{$last_name}" : $email_username;
                break;
            case 'email':
                $username = $email_username;
                break;
            case 'alnum':
                $username = '';
                break;
        }
        $username = forms_register_generate_username($username);
        set_input('username', $username);
    }
    if (elgg_get_plugin_setting('autogen_password', 'forms_register')) {
        $password = generate_random_cleartext_password();
        set_input('password', $password);
        set_input('password2', $password);
    } else {
        if ($min_strength = elgg_get_plugin_setting('min_password_strength', 'forms_register')) {
            // @todo: add other user inputs
            $zxcvbn = new \ZxcvbnPhp\Zxcvbn();
            $strength = $zxcvbn->passwordStrength($password);
            if ($strength < $min_strength) {
                register_error(elgg_echo('actions:register:error:password_strength'));
                forward(REFERER);
            }
        }
        if (elgg_get_plugin_setting('hide_password_repeat', 'forms_register')) {
            set_input('password2', $password);
        }
    }
}