diff --git a/composer.json b/composer.json index ede8ce9c..ee414056 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "webvimark/module-user-management", + "name": "davidias2004/module-user-management", "description": "User with improved RBAC", "autoload": { "psr-4": { diff --git a/controllers/AuthController.php b/controllers/AuthController.php index d80233fb..aadd9489 100644 --- a/controllers/AuthController.php +++ b/controllers/AuthController.php @@ -151,19 +151,24 @@ public function actionRegistration() if ( Yii::$app->getModule('user-management')->useEmailAsLogin AND Yii::$app->getModule('user-management')->emailConfirmationRequired ) { return $this->renderIsAjax('registrationWaitForEmailConfirmation', compact('user')); - } - else - { - $roles = (array)$this->module->rolesAfterRegistration; - - foreach ($roles as $role) - { - User::assignRole($user->id, $role); - } - - Yii::$app->user->login($user); - - return $this->redirect(Yii::$app->user->returnUrl); + }else{ + if (Yii::$app->getModule('user-management')->emailConfirmationRequired ) + { + return $this->renderIsAjax('registrationWaitForEmailConfirmation', compact('user')); + } + else + { + $roles = (array)$this->module->rolesAfterRegistration; + + foreach ($roles as $role) + { + User::assignRole($user->id, $role); + } + + Yii::$app->user->login($user); + + return $this->redirect(Yii::$app->user->returnUrl); + } } } @@ -186,7 +191,7 @@ public function actionRegistration() */ public function actionConfirmRegistrationEmail($token) { - if ( Yii::$app->getModule('user-management')->useEmailAsLogin AND Yii::$app->getModule('user-management')->emailConfirmationRequired ) + if (Yii::$app->getModule('user-management')->emailConfirmationRequired ) { $model = new $this->module->registrationFormClass; diff --git a/models/forms/RegistrationForm.php b/models/forms/RegistrationForm.php index 8f547597..238183cf 100644 --- a/models/forms/RegistrationForm.php +++ b/models/forms/RegistrationForm.php @@ -13,6 +13,7 @@ class RegistrationForm extends Model public $password; public $repeat_password; public $captcha; + public $email; /** * @inheritdoc @@ -45,9 +46,14 @@ public function rules() else { $rules[] = ['username', 'string', 'max' => 50]; - + $rules[] = ['email', 'string', 'max' => 128]; + $rules[] = ['email', 'email']; $rules[] = ['username', 'match', 'pattern'=>Yii::$app->getModule('user-management')->registrationRegexp]; $rules[] = ['username', 'match', 'not'=>true, 'pattern'=>Yii::$app->getModule('user-management')->registrationBlackRegexp]; + //verification to make email field required ir not + if(Yii::$app->getModule('user-management')->emailConfirmationRequired){ + $rules[] = [['email'], 'required']; + } } return $rules; @@ -69,10 +75,11 @@ public function purgeXSS($attribute) public function attributeLabels() { return [ - 'username' => Yii::$app->getModule('user-management')->useEmailAsLogin ? 'E-mail' : UserManagementModule::t('front', 'Login'), + 'username' => Yii::$app->getModule('user-management')->useEmailAsLogin ? 'E-mail' : UserManagementModule::t('front', 'Username'), 'password' => UserManagementModule::t('front', 'Password'), 'repeat_password' => UserManagementModule::t('front', 'Repeat password'), 'captcha' => UserManagementModule::t('front', 'Captcha'), + 'email' => UserManagementModule::t('front', 'E-mail'), ]; } @@ -90,40 +97,37 @@ public function registerUser($performValidation = true) $user = new User(); $user->password = $this->password; - - if ( Yii::$app->getModule('user-management')->useEmailAsLogin ) + $user->username = $this->username; + + if ( Yii::$app->getModule('user-management')->useEmailAsLogin ) { $user->email = $this->username; - // If email confirmation required then we save user with "inactive" status // and without username (username will be filled with email value after confirmation) - if ( Yii::$app->getModule('user-management')->emailConfirmationRequired ) + }else + { + //If doesn't use email as login it needs email to confirm account + $user->email = $this->email; + } + + //In this way its possible to use emailConfirmation without restriction of use emailAsLogin + if ( Yii::$app->getModule('user-management')->emailConfirmationRequired ) + { + $user->status = User::STATUS_INACTIVE; + $user->generateConfirmationToken(); + $user->save(false); + + $this->saveProfile($user); + + if ( $this->sendConfirmationEmail($user) ) { - $user->status = User::STATUS_INACTIVE; - $user->generateConfirmationToken(); - $user->save(false); - - $this->saveProfile($user); - - if ( $this->sendConfirmationEmail($user) ) - { - return $user; - } - else - { - $this->addError('username', UserManagementModule::t('front', 'Could not send confirmation email')); - } + return $user; } else { - $user->username = $this->username; + $this->addError('username', UserManagementModule::t('front', 'Could not send confirmation email')); } } - else - { - $user->username = $this->username; - } - if ( $user->save() ) { @@ -174,7 +178,6 @@ public function checkConfirmationToken($token) if ( $user ) { - $user->username = $user->email; $user->status = User::STATUS_ACTIVE; $user->email_confirmed = 1; $user->removeConfirmationToken(); diff --git a/views/auth/registration.php b/views/auth/registration.php index 0b65edb6..b166e8a2 100644 --- a/views/auth/registration.php +++ b/views/auth/registration.php @@ -25,6 +25,8 @@ ]); ?> field($model, 'username')->textInput(['maxlength' => 50, 'autocomplete'=>'off', 'autofocus'=>true]) ?> + + getModule('user-management')->useEmailAsLogin ? $form->field($model, 'email')->textInput(['maxlength' => 128, 'autocomplete'=>'off']) : ''?> field($model, 'password')->passwordInput(['maxlength' => 255, 'autocomplete'=>'off']) ?>