public function testPasswordHash() { $password = '******'; $hash = Security::generatePasswordHash($password); $this->assertTrue(Security::validatePassword($password, $hash)); $this->assertFalse(Security::validatePassword('test', $hash)); }
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(); }); }
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; }
/** * Generates password hash from password and sets it to the model * * @param string $password */ public function setPassword($password) { $this->password_hash = Security::generatePasswordHash($password); }
<?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'];
/** * Wrapper for yii security helper method. * * @param $password * @return string */ public static function hash($password) { return Security::generatePasswordHash($password, \Yii::$app->getModule('user')->cost); }