Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email confirmation with all options #182

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "webvimark/module-user-management",
"name": "davidias2004/module-user-management",
"description": "User with improved RBAC",
"autoload": {
"psr-4": {
Expand Down
33 changes: 19 additions & 14 deletions controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

}
Expand All @@ -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;

Expand Down
57 changes: 30 additions & 27 deletions models/forms/RegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RegistrationForm extends Model
public $password;
public $repeat_password;
public $captcha;
public $email;

/**
* @inheritdoc
Expand Down Expand Up @@ -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;
Expand All @@ -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'),
];
}

Expand All @@ -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() )
{
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions views/auth/registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
]); ?>

<?= $form->field($model, 'username')->textInput(['maxlength' => 50, 'autocomplete'=>'off', 'autofocus'=>true]) ?>

<?= !Yii::$app->getModule('user-management')->useEmailAsLogin ? $form->field($model, 'email')->textInput(['maxlength' => 128, 'autocomplete'=>'off']) : ''?>

<?= $form->field($model, 'password')->passwordInput(['maxlength' => 255, 'autocomplete'=>'off']) ?>

Expand Down