From afdf75758658e76f70436e6228f263c3937ef682 Mon Sep 17 00:00:00 2001 From: "enrico.detoma" Date: Thu, 2 Nov 2017 18:36:12 +0100 Subject: [PATCH] Add a new setting (emailAsUsername) to avoid requesting the username and set it equal to the email --- Module.php | 3 +++ models/RegistrationForm.php | 9 ++++++++- models/SettingsForm.php | 9 ++++++++- models/User.php | 15 ++++++++++++++- views/registration/register.php | 2 ++ views/settings/account.php | 5 ++++- 6 files changed, 39 insertions(+), 4 deletions(-) diff --git a/Module.php b/Module.php index e81d30abb..7f225fba5 100644 --- a/Module.php +++ b/Module.php @@ -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 diff --git a/models/RegistrationForm.php b/models/RegistrationForm.php index e7bba8935..ea8658a6c 100644 --- a/models/RegistrationForm.php +++ b/models/RegistrationForm.php @@ -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', @@ -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. * diff --git a/models/SettingsForm.php b/models/SettingsForm.php index e76f4b961..55c4eaabe 100644 --- a/models/SettingsForm.php +++ b/models/SettingsForm.php @@ -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'], @@ -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. * diff --git a/models/User.php b/models/User.php index 74afd957d..2cb739da4 100644 --- a/models/User.php +++ b/models/User.php @@ -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', @@ -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. * diff --git a/views/registration/register.php b/views/registration/register.php index 97e7b494c..c333b05dc 100644 --- a/views/registration/register.php +++ b/views/registration/register.php @@ -36,7 +36,9 @@ field($model, 'email') ?> + emailAsUsername == false): ?> field($model, 'username') ?> + enableGeneratingPassword == false): ?> field($model, 'password')->passwordInput() ?> diff --git a/views/settings/account.php b/views/settings/account.php index dbee38d2b..f4dea5310 100644 --- a/views/settings/account.php +++ b/views/settings/account.php @@ -20,9 +20,10 @@ $this->title = Yii::t('user', 'Account settings'); $this->params['breadcrumbs'][] = $this->title; +$module = Yii::$app->getModule('user'); ?> -render('/_alert', ['module' => Yii::$app->getModule('user')]) ?> +render('/_alert', ['module' => $module]) ?>
@@ -47,7 +48,9 @@ field($model, 'email') ?> + emailAsUsername == false): ?> field($model, 'username') ?> + field($model, 'new_password')->passwordInput() ?>