Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class Module extends BaseModule
/** @var int Email changing strategy. */
public $emailChangeStrategy = self::STRATEGY_DEFAULT;

/** @var bool Avoid asking username during registration and set it equal to email */
public $emailAsUsername = false;

/** @var int The time you want the user will be remembered without asking for credentials. */
public $rememberFor = 1209600; // two weeks

Expand Down
9 changes: 8 additions & 1 deletion models/RegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function rules()
// username rules
'usernameTrim' => ['username', 'trim'],
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255],
'usernamePattern' => ['username', 'match', 'pattern' => $user::$usernameRegexp],
'usernamePattern' => ($this->module->emailAsUsername ? ['username', 'email'] : ['username', 'match', 'pattern' => $user::$usernameRegexp]),
'usernameRequired' => ['username', 'required'],
'usernameUnique' => [
'username',
Expand Down Expand Up @@ -93,6 +93,13 @@ public function formName()
return 'register-form';
}

public function beforeValidate() {
if ($this->module->emailAsUsername) {
$this->username = $this->email;
}
return parent::beforeValidate();
}

/**
* Registers a new user account. If registration was successful it will set flash message.
*
Expand Down
9 changes: 8 additions & 1 deletion models/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function rules()
'usernameTrim' => ['username', 'trim'],
'usernameRequired' => ['username', 'required'],
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255],
'usernamePattern' => ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/'],
'usernamePattern' => ($this->module->emailAsUsername ? ['username', 'email'] : ['username', 'match', 'pattern' => '/^[-a-zA-Z0-9_\.@]+$/']),
'emailTrim' => ['email', 'trim'],
'emailRequired' => ['email', 'required'],
'emailPattern' => ['email', 'email'],
Expand Down Expand Up @@ -109,6 +109,13 @@ public function formName()
return 'settings-form';
}

public function beforeValidate() {
if ($this->module->emailAsUsername) {
$this->username = $this->email;
}
return parent::beforeValidate();
}

/**
* Saves new account settings.
*
Expand Down
15 changes: 14 additions & 1 deletion models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public function rules()
// username rules
'usernameTrim' => ['username', 'trim'],
'usernameRequired' => ['username', 'required', 'on' => ['register', 'create', 'connect', 'update']],
'usernameMatch' => ['username', 'match', 'pattern' => static::$usernameRegexp],
'usernameMatch' => ($this->module->emailAsUsername ? ['username', 'email'] : ['username', 'match', 'pattern' => static::$usernameRegexp]),
'usernameLength' => ['username', 'string', 'min' => 3, 'max' => 255],
'usernameUnique' => [
'username',
Expand Down Expand Up @@ -269,6 +269,19 @@ public function validateAuthKey($authKey)
return $this->getAttribute('auth_key') === $authKey;
}

/**
* This method is invoked before validation starts.
* If emailAsUsername setting is true, it copies email to username
*
* @return bool
*/
public function beforeValidate() {
if ($this->module->emailAsUsername) {
$this->username = $this->email;
}
return parent::beforeValidate();
}

/**
* Creates new user account. It generates password if it is not provided by user.
*
Expand Down
2 changes: 2 additions & 0 deletions views/registration/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@

<?= $form->field($model, 'email') ?>

<?php if ($module->emailAsUsername == false): ?>
<?= $form->field($model, 'username') ?>
<?php endif ?>

<?php if ($module->enableGeneratingPassword == false): ?>
<?= $form->field($model, 'password')->passwordInput() ?>
Expand Down
5 changes: 4 additions & 1 deletion views/settings/account.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@

$this->title = Yii::t('user', 'Account settings');
$this->params['breadcrumbs'][] = $this->title;
$module = Yii::$app->getModule('user');
?>

<?= $this->render('/_alert', ['module' => Yii::$app->getModule('user')]) ?>
<?= $this->render('/_alert', ['module' => $module]) ?>

<div class="row">
<div class="col-md-3">
Expand All @@ -47,7 +48,9 @@

<?= $form->field($model, 'email') ?>

<?php if ($module->emailAsUsername == false): ?>
<?= $form->field($model, 'username') ?>
<?php endif ?>

<?= $form->field($model, 'new_password')->passwordInput() ?>

Expand Down