public function testValidation() { \Yii::$app->getModule('user'); $this->user = new User(['scenario' => 'register']); $this->specify('username is valid', function () { verify('username is required', $this->user->validate(['username']))->false(); $this->user->username = Security::generateRandomKey(); verify('username is too long', $this->user->validate(['username']))->false(); $this->user->username = '******'; verify('username contains invalid characters', $this->user->validate(['username']))->false(); $this->user->username = '******'; verify('username is already using', $this->user->validate(['username']))->false(); $this->user->username = '******'; verify('username is ok', $this->user->validate(['username']))->true(); }); $this->specify('email is valid', function () { verify('email is required', $this->user->validate(['email']))->false(); $this->user->email = 'not valid email'; verify('email is not email', $this->user->validate(['email']))->false(); $this->user->email = '*****@*****.**'; verify('email is already using', $this->user->validate(['email']))->false(); $this->user->email = '*****@*****.**'; verify('email is ok', $this->user->validate(['email']))->true(); }); $this->specify('password is valid', function () { verify('password is required', $this->user->validate(['password']))->false(); $this->user->password = '******'; verify('password is too short', $this->user->validate(['password']))->false(); $this->user->password = '******'; verify('password is ok', $this->user->validate(['password']))->true(); }); }
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 new password reset token */ public function generatePasswordResetToken() { $this->password_reset_token = Security::generateRandomKey() . '_' . time(); }
<?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'];
/** EXTENSION MOVIE **/ public function beforeSave($insert) { if (parent::beforeSave($insert)) { if ($this->isNewRecord) { $this->auth_key = Security::generateRandomKey(); } return true; } return false; }
/** * Creates a cookie with a randomly generated CSRF token. * Initial values specified in [[csrfCookie]] will be applied to the generated cookie. * @return Cookie the generated cookie * @see enableCsrfValidation */ protected function createCsrfCookie() { $options = $this->csrfCookie; $options['name'] = $this->csrfParam; $options['value'] = Security::generateRandomKey(); return new Cookie($options); }
private function sendPasswordResetEmail($email) { $user = User::find(array('status' => User::STATUS_ACTIVE, 'email' => $email)); if (!$user) { return false; } $user->password_reset_token = Security::generateRandomKey(); if ($user->save(false)) { $fromEmail = \Yii::$app->params['supportEmail']; $name = '=?UTF-8?B?' . base64_encode(\Yii::$app->name . ' robot') . '?='; $subject = '=?UTF-8?B?' . base64_encode('Password reset for ' . \Yii::$app->name) . '?='; $body = $this->renderPartial('/emails/passwordResetToken', array('user' => $user)); $headers = "From: {$name} <{$fromEmail}>\r\n" . "MIME-Version: 1.0\r\n" . "Content-type: text/plain; charset=UTF-8"; return mail($fromEmail, $subject, $body, $headers); } return false; }
public function generateAuthKey() { $this->auth_key = Security::generateRandomKey(); }
private function sendPasswordResetEmail($email) { $user = User::find(['status' => User::STATUS_ACTIVE, 'email' => $email]); if (!$user) { return false; } $user->password_reset_token = Security::generateRandomKey(); if ($user->save(false)) { return \Yii::$app->mail->compose('passwordResetToken', ['user' => $user])->setFrom([\Yii::$app->params['supportEmail'] => \Yii::$app->name . ' robot'])->setTo($email)->setSubject('Password reset for ' . \Yii::$app->name)->send(); } return false; }
private function sendPasswordResetEmail($phone) { $user = User::find(['status' => User::STATUS_ACTIVE, 'phone' => $phone])->one(); if (!$user) { return false; } $user->password_reset_token = Security::generateRandomKey(); $user->password = User::generatePassword(); if ($user->save(false)) { $result = \Yii::$app->sms->sms_send(preg_replace("/[^0-9]/", '', $user->phone), 'Ваш новый пароль: ' . $user->password, "Kvadro"); return true; } return false; }
/** * @inheritdoc */ public function beforeSave($insert) { if ($insert) { $this->setAttribute('auth_key', Security::generateRandomKey()); $this->setAttribute('role', $this->module->defaultRole); } if (!empty($this->password)) { $this->setAttribute('password_hash', Password::hash($this->password)); } return parent::beforeSave($insert); }
/** * Generates recovery data and sends recovery message to user. */ public function sendRecoveryMessage() { $this->recovery_token = Security::generateRandomKey(); $this->recovery_sent_at = time(); $this->save(false); return $this->sendMessage($this->email, \Yii::t('user', 'Please complete password reset'), 'recovery', ['user' => $this]); }