antonyz89 / yii2-password-behavior
Behavior for manage password
Package info
github.com/AntonyZ89/yii2-password-behavior
Type:yii2-extension
pkg:composer/antonyz89/yii2-password-behavior
Requires
- yiisoft/yii2: ~2.0.0
Requires (Dev)
None
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Behavior for manage password
Installation
The preferred way to install this extension is through composer.
Either run
php composer.phar require --prefer-dist antonyz89/yii2-password-behavior "*"
or add
"antonyz89/yii2-password-behavior": "*"
to the require section of your composer.json file.
Usage
Just add PasswordBehavior::class on Model's behavior function:
public function behaviors() { return [ PasswordBehavior::class, ]; }
_form.php:
<div class="row">
<div class="col-md-4">
<?= $form->field($model, 'email')->textInput(['maxlength' => true]) ?>
<?php if ($model->isNewRecord): ?>
<?= $form->field($model, 'password_hash')->passwordInput() ?> <!-- required on create -->
<?= $form->field($model, 'confirm_password')->passwordInput() ?>
<?php else: ?>
<?= $form->field($model, 'old_password')->passwordInput() ?>
<?= $form->field($model, 'new_password')->passwordInput() ?> <!-- required on update (replaces password_hash) -->
<?= $form->field($model, 'confirm_password')->passwordInput() ?>
<?php endif; ?>
</div> <!-- /.col-md-4 -->
</div> <!-- /.row -->
To work properly, your Model need have this four variables:
$password_hash, $new_password, $confirm_password, $old_password
If you have this variables, but the name is different, just do:
/** * @property string $password */ class ExampleModel extends ActiveRecord implements IdentityInterface, UserCredentialsInterface { public $new_password, $confirmPsw, $oldPsw, $authorizationKey; // new_password can be skipped because they already exists /** * @inheritDoc */ public function behaviors() { return [ [ 'class' => PasswordBehavior::class, 'password_hash' => 'password', 'confirm_password' => 'confirmPsw', 'auth_key' => 'authorizationKey', 'old_password' => 'oldPsw' ] ]; } }
If want to ignore $confirm_password, $old_password and/or $auth_key, just do:
/** * @property string $password */ class ExampleModel extends ActiveRecord implements IdentityInterface, UserCredentialsInterface { public $new_password; // new_password can be skipped because they already exists /** * @inheritDoc */ public function behaviors() { return [ [ 'class' => PasswordBehavior::class, 'password_hash' => 'password', /* * Will be ignored and comparison between * `$confirm_password` and `$new_password` or `$password_hash` will not happen */ 'confirm_password' => false, /* * Will be ignored and comparison between * `old_password` and `$new_password` will not happen */ 'old_password' => false, /* * Will be ignored and when a new password is defined * a new Authorization Key will not generated */ 'auth_key' => false ] ]; } }
i18n
add in common\config\main.php
'components' => [ ... 'i18n' => [ 'translations' => [ 'psw' => [ 'class' => PhpMessageSource::class, 'basePath' => '@antonyz89/password_behaviour/messages', ] ], ] ];