Example #1
0
 public function testPasswordHash()
 {
     $password = '******';
     $hash = Security::generatePasswordHash($password);
     $this->assertTrue(Security::validatePassword($password, $hash));
     $this->assertFalse(Security::validatePassword('test', $hash));
 }
Example #2
0
 public function testLoginCorrect()
 {
     $model = $this->mockUser(new User(['password_hash' => Security::generatePasswordHash('demo')]));
     $model->username = '******';
     $model->password = '******';
     $this->specify('user should be able to login with correct credentials', function () use($model) {
         expect('model should login user', $model->login())->true();
         expect('error message should not be set', $model->errors)->hasntKey('password');
         expect('user should be logged in', Yii::$app->user->isGuest)->false();
     });
 }
Example #3
0
 public function beforeSave($insert)
 {
     if (parent::beforeSave($insert)) {
         if (($this->isNewRecord || $this->getScenario() === 'resetPassword') && !empty($this->password)) {
             $this->password_hash = Security::generatePasswordHash($this->password);
         }
         if ($this->isNewRecord) {
             $this->auth_key = Security::generateRandomKey();
         }
         return true;
     }
     return false;
 }
Example #4
0
 /**
  * Generates password hash from password and sets it to the model
  *
  * @param string $password
  */
 public function setPassword($password)
 {
     $this->password_hash = Security::generatePasswordHash($password);
 }
Example #5
0
<?php

use yii\helpers\Security;
return ['username' => 'userName', 'auth_key' => function ($fixture, $faker, $index) {
    $fixture['auth_key'] = Security::generateRandomKey();
    return $fixture;
}, 'password_hash' => function ($fixture, $faker, $index) {
    $fixture['password_hash'] = Security::generatePasswordHash('password_' . $index);
    return $fixture;
}, 'password_reset_token' => function ($fixture, $faker, $index) {
    $fixture['password_reset_token'] = Security::generateRandomKey() . '_' . time();
    return $fixture;
}, 'created_at' => function ($fixture, $faker, $index) {
    $fixture['created_at'] = time();
    return $fixture;
}, 'updated_at' => function ($fixture, $faker, $index) {
    $fixture['updated_at'] = time();
    return $fixture;
}, 'email' => 'email'];
Example #6
0
 /**
  * Wrapper for yii security helper method.
  *
  * @param $password
  * @return string
  */
 public static function hash($password)
 {
     return Security::generatePasswordHash($password, \Yii::$app->getModule('user')->cost);
 }