Commit 1c61c501 by henny flora panjaitan

by hotma

parent 749be554
<?php
use yii\helpers\Html;
/* @var $this \yii\web\View view component instance */
/* @var $message \yii\mail\MessageInterface the message being composed */
/* @var $content string main view render result */
?>
<?php $this->beginPage() ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<?= $content ?>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
/**
* This is the model class for table "akun".
*
* @property int $id
* @property string $username
* @property string $password
* @property string $last_login
*/
class Account extends \yii\db\ActiveRecord implements IdentityInterface
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 't_user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['username', 'password'], 'required'],
//[['last_login'], 'safe'],
[['username', 'password'], 'string', 'max' => 255],
/*[['fullname'], 'string', 'max' => 150],
[['role'], 'string', 'max' => 25],*/
/*[['auth_key'], 'safe'],*/
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
];
}
public static function findByUsername($username)
{
return static::findOne(['username' => $username]);
}
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password);
}
public static function findIdentity($id)
{
return static::findOne(['id' => $id]);
}
public static function findIdentityByAccessToken($token, $type = null)
{
//throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
return static::findOne(['acccess_token' => $token]);
}
public function getId()
{
//return $this->getPrimaryKey();
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomString();
}
public function setPassword($password)
{
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
}
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* ContactForm is the model behind the contact form.
*/
class ContactForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email address
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],
];
}
/**
* @return array customized attribute labels
*/
public function attributeLabels()
{
return [
'verifyCode' => 'Verification Code',
];
}
/**
* Sends an email to the specified email address using the information collected by this model.
* @param string $email the target email address
* @return bool whether the model passes validation
*/
public function contact($email)
{
if ($this->validate()) {
Yii::$app->mailer->compose()
->setTo($email)
->setFrom([Yii::$app->params['senderEmail'] => Yii::$app->params['senderName']])
->setReplyTo([$this->email => $this->name])
->setSubject($this->subject)
->setTextBody($this->body)
->send();
return true;
}
return false;
}
}
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "hasil_voting".
*
* @property int $id_voting
* @property int $id_user
* @property int $pilihan
* @property int $status_voting
* @property string|null $created_at
* @property int|null $created_by
* @property string|null $updated_at
* @property int|null $updated_by
* @property int|null $active
*
* @property TUser $user
* @property MVoting $voting
*/
class HasilVoting extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'hasil_voting';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id_voting', 'id_user', 'pilihan', 'status_voting'], 'required'],
[['id_voting', 'id_user', 'pilihan', 'status_voting', 'created_by', 'updated_by', 'active'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
[['id_voting', 'id_user'], 'unique', 'targetAttribute' => ['id_voting', 'id_user']],
[['id_user'], 'exist', 'skipOnError' => true, 'targetClass' => TUser::className(), 'targetAttribute' => ['id_user' => 'id']],
[['id_voting'], 'exist', 'skipOnError' => true, 'targetClass' => MVoting::className(), 'targetAttribute' => ['id_voting' => 'id_voting']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id_voting' => 'Id Voting',
'id_user' => 'Id User',
'pilihan' => 'Pilihan',
'status_voting' => 'Status Voting',
'created_at' => 'Created At',
'created_by' => 'Created By',
'updated_at' => 'Updated At',
'updated_by' => 'Updated By',
'active' => 'Active',
];
}
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
],
'value' => new Expression('NOW()'),
],
];
}
public function beforeSave($insert)
{
if (!parent::beforeSave($insert)) {
return false;
}
if(ActiveRecord::EVENT_BEFORE_INSERT){
$this->created_by = Yii::$app->user->identity->id;
$this->updated_by = Yii::$app->user->identity->id;
}else if(ActiveRecord::EVENT_BEFORE_UPDATE){
$this->updated_by = Yii::$app->user->identity->id;
}
return true;
}
/**
* Gets query for [[User]].
*
* @return \yii\db\ActiveQuery
*/
public function getUser()
{
return $this->hasOne(TUser::className(), ['id' => 'id_user']);
}
/**
* Gets query for [[Voting]].
*
* @return \yii\db\ActiveQuery
*/
public function getVoting()
{
return $this->hasOne(MVoting::className(), ['id_voting' => 'id_voting']);
}
}
<?php
namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\HasilVoting;
/**
* HasilVotingSearch represents the model behind the search form of `app\models\HasilVoting`.
*/
class HasilVotingSearch extends HasilVoting
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id_voting', 'id_user', 'pilihan', 'status_voting', 'created_by', 'updated_by', 'active'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = HasilVoting::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id_voting' => $this->id_voting,
'id_user' => $this->id_user,
'pilihan' => $this->pilihan,
'status_voting' => $this->status_voting,
'created_at' => $this->created_at,
'created_by' => $this->created_by,
'updated_at' => $this->updated_at,
'updated_by' => $this->updated_by,
'active' => $this->active,
]);
return $dataProvider;
}
public function searchData($model,$params)
{
$query = $model;
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id_voting' => $this->id_voting,
'id_user' => $this->id_user,
'pilihan' => $this->pilihan,
'status_voting' => $this->status_voting,
'created_at' => $this->created_at,
'created_by' => $this->created_by,
'updated_at' => $this->updated_at,
'updated_by' => $this->updated_by,
'active' => $this->active,
]);
return $dataProvider;
}
}
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* LoginForm is the model behind the login form.
*
* @property User|null $user This property is read-only.
*
*/
class LoginForm extends Model
{
public $username;
public $password;
public $rememberMe = true;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password'], 'required'],
// rememberMe must be a boolean value
['rememberMe', 'boolean'],
// password is validated by validatePassword()
['password', 'validatePassword'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
/**
* Logs in a user using the provided username and password.
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 : 0);
}
return false;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "m_voting".
*
* @property int $id_voting
* @property string|null $nama_voting
* @property string|null $periode_awal
* @property string|null $periode_tutup
*
* @property HasilVoting[] $hasilVotings
* @property TUser[] $users
* @property TKandidat[] $tKandidats
*/
class MVoting extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'm_voting';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['periode_awal', 'periode_tutup','status_aktif','nama_voting'], 'required'],
[['periode_awal', 'periode_tutup','created_at','updated_at'], 'safe'],
[['status_aktif','created_by','updated_by'], 'integer'],
[['nama_voting'], 'string', 'max' => 500],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id_voting' => 'Id Voting',
'nama_voting' => 'Deskripsi',
'periode_awal' => 'Tanggal Mulai Voting',
'periode_tutup' => 'Tanggal Akhir Voting',
'status_aktif' => 'Status Voting',
];
}
public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',
],
'value' => new Expression('NOW()'),
],
];
}
public function beforeSave($insert)
{
if (!parent::beforeSave($insert)) {
return false;
}
if(ActiveRecord::EVENT_BEFORE_INSERT){
$this->created_by = Yii::$app->user->identity->id;
$this->updated_by = Yii::$app->user->identity->id;
}else if(ActiveRecord::EVENT_BEFORE_UPDATE){
$this->updated_by = Yii::$app->user->identity->id;
}
return true;
}
/**
* Gets query for [[HasilVotings]].
*
* @return \yii\db\ActiveQuery
*/
public function getHasilVotings()
{
return $this->hasMany(HasilVoting::className(), ['id_voting' => 'id_voting']);
}
/**
* Gets query for [[Users]].
*
* @return \yii\db\ActiveQuery
*/
public function getUsers()
{
return $this->hasMany(TUser::className(), ['id' => 'id_user'])->viaTable('hasil_voting', ['id_voting' => 'id_voting']);
}
/**
* Gets query for [[TKandidats]].
*
* @return \yii\db\ActiveQuery
*/
public function getTKandidats()
{
return $this->hasMany(TKandidat::className(), ['id_voting' => 'id_voting']);
}
}
<?php
namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\MVoting;
/**
* MVotingSearch represents the model behind the search form of `app\models\MVoting`.
*/
class MVotingSearch extends MVoting
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id_voting'], 'integer'],
[['nama_voting', 'periode_awal', 'periode_tutup'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = MVoting::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id_voting' => $this->id_voting,
'periode_awal' => $this->periode_awal,
'periode_tutup' => $this->periode_tutup,
]);
$query->andFilterWhere(['like', 'nama_voting', $this->nama_voting]);
return $dataProvider;
}
}
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use app\models\User;
/**
* LoginForm is the model behind the login form.
*
* @property User|null $user This property is read-only.
*
*/
class RegistrationForm extends Model
{
public $fullname;
public $role;
public $username;
public $password;
private $_user = false;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
// username and password are both required
[['username', 'password','fullname','role'], 'required'],
[['username', 'password'], 'required','on' => 'register'],
[['fullname', 'role'], 'required','on' => 'register'],
];
}
/**
* Validates the password.
* This method serves as the inline validation for password.
*
* @param string $attribute the attribute currently being validated
* @param array $params the additional name-value pairs given in the rule
*/
public function validatePassword($attribute, $params)
{
if (!$this->hasErrors()) {
$user = $this->getUser();
if (!$user || !$user->validatePassword($this->password)) {
$this->addError($attribute, 'Incorrect username or password.');
}
}
}
public function scenarios(){
$scenarios = parent::scenarios();
$scenarios['register'] = ['username','password','fullname','role','last_login'];
return $scenarios;
}
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'password' => 'Password',
'last_login' => 'Last Login',
'role' => 'Role',
'fullname' => 'Full Name',
];
}
public static function hashPassword($_password){
return (Yii::$app->getSecurity()->generatePasswordHash($_password));
}
public function generateAuthKey()
{
return Yii::$app->security->generateRandomString();
}
public function register(){
if ($this->validate()) {
$account = new Account();
$account->username = $this->username;
$account->password = self::hashPassword($this->password);
$account->fullname = $this->fullname;
$account->role = $this->role;
$account->auth_key = self::generateAuthKey();
if($account->save()){
return $account;
}else{
print_r($account->errors);
}
return false;
}
}
/**
* Logs in a user using the provided username and password.
* @return bool whether the user is logged in successfully
*/
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 : 0);
}
return false;
}
/**
* Finds user by [[username]]
*
* @return User|null
*/
public function getUser()
{
if ($this->_user === false) {
$this->_user = User::findByUsername($this->username);
}
return $this->_user;
}
}
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "t_kandidat".
*
* @property int $id_kandidat
* @property string $nama_kandidat
* @property int $id_voting
*
* @property MVoting $voting
*/
class TKandidat extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 't_kandidat';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['nama_kandidat', 'id_voting'], 'required'],
[['id_voting'], 'integer'],
[['nama_kandidat'], 'string', 'max' => 500],
[['id_voting'], 'exist', 'skipOnError' => true, 'targetClass' => MVoting::className(), 'targetAttribute' => ['id_voting' => 'id_voting']],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id_kandidat' => 'Id Kandidat',
'nama_kandidat' => 'Nama Kandidat',
'id_voting' => 'Id Voting',
];
}
/**
* Gets query for [[Voting]].
*
* @return \yii\db\ActiveQuery
*/
public function getVoting()
{
return $this->hasOne(MVoting::className(), ['id_voting' => 'id_voting']);
}
}
<?php
namespace app\models;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\TKandidat;
/**
* TKandidatSearch represents the model behind the search form of `app\models\TKandidat`.
*/
class TKandidatSearch extends TKandidat
{
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['id_kandidat', 'id_voting'], 'integer'],
[['nama_kandidat'], 'safe'],
];
}
/**
* {@inheritdoc}
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = TKandidat::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id_kandidat' => $this->id_kandidat,
'id_voting' => $this->id_voting,
]);
$query->andFilterWhere(['like', 'nama_kandidat', $this->nama_kandidat]);
return $dataProvider;
}
public function searchData($model,$params)
{
$query = $model;
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id_kandidat' => $this->id_kandidat,
'id_voting' => $this->id_voting,
]);
$query->andFilterWhere(['like', 'nama_kandidat', $this->nama_kandidat]);
return $dataProvider;
}
}
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "t_user".
*
* @property int $id
* @property string $username
* @property string|null $auth_key
* @property string $password_hash
* @property string|null $password_reset_token
* @property string $email
* @property int $status
* @property int $created_at
* @property int $updated_at
* @property string|null $id_lokasi
* @property string|null $inisial_user
* @property string|null $nama_user
* @property string|null $telp
* @property string|null $foto
* @property string|null $alamat
*
* @property HasilVoting[] $hasilVotings
* @property MVoting[] $votings
*/
class TUser extends \yii\db\ActiveRecord
{
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 't_user';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
[['username', 'password_hash', 'email', 'created_at', 'updated_at'], 'required'],
[['status', 'created_at', 'updated_at'], 'integer'],
[['username', 'auth_key', 'password_hash', 'password_reset_token', 'email', 'id_lokasi', 'inisial_user', 'nama_user', 'telp', 'foto'], 'string', 'max' => 255],
[['alamat'], 'string', 'max' => 500],
[['username'], 'unique'],
[['email'], 'unique'],
[['password_reset_token'], 'unique'],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'auth_key' => 'Auth Key',
'password_hash' => 'Password Hash',
'password_reset_token' => 'Password Reset Token',
'email' => 'Email',
'status' => 'Status',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
'id_lokasi' => 'Id Lokasi',
'inisial_user' => 'Inisial User',
'nama_user' => 'Nama Pengguna',
'telp' => 'Telp',
'foto' => 'Foto',
'alamat' => 'Alamat',
];
}
/**
* Gets query for [[HasilVotings]].
*
* @return \yii\db\ActiveQuery
*/
public function getHasilVotings()
{
return $this->hasMany(HasilVoting::className(), ['id_user' => 'id']);
}
/**
* Gets query for [[Votings]].
*
* @return \yii\db\ActiveQuery
*/
public function getVotings()
{
return $this->hasMany(MVoting::className(), ['id_voting' => 'id_voting'])->viaTable('hasil_voting', ['id_user' => 'id']);
}
}
<?php
namespace app\models;
use Yii;
use yii\db\Expression;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
//use hscstudio\mimin\components\Mimin;
/**
* This is the model class for table "user".
*
* @property integer $id
* @property string $username
* @property string $auth_key
* @property string $password_hash
* @property string $password_reset_token
* @property string $email
* @property integer $role
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
*/
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_ACTIVE = 10;
const STATUS_NON_ACTIVE = 20;
// const ROLE_ADMIN = 10;
/**
* @inheritdoc
*/
public static function tableName()
{
return 't_user';
}
public function behaviors()
{
return [
TimeStampBehavior::className(),
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username', 'email','nama_user','status','password_hash'], 'required'],
[['username', 'email'], 'filter', 'filter' => 'trim'],
[['username', 'email'], 'unique'],
['status', 'default', 'value' => self::STATUS_ACTIVE],
// ['role', 'default', 'value' => self::ROLE_ADMIN],
['email', 'email'],
[['username','nama_user','password_hash'], 'string', 'min' => 2, 'max' => 255],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'auth_key' => 'Auth Key',
//'plainPassword' => 'Password',
'password_hash' => 'Password',
'password_reset_token' => 'Password Reset Token',
'email' => 'Email',
//'role' => 'Role',
'status' => 'Status',
'created_at' => 'Ditambahkan Pada',
'updated_at' => 'Diupdate Pada',
'nama_user' => 'Nama Lengkap Pengguna',
];
}
public function getStatusOptions()
{
return [
self::STATUS_ACTIVE => 'Active',
self::STATUS_NON_ACTIVE => 'Non Active',
];
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
}
public static function findByEmail($email)
{
return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]);
}
/**
* Finds user by password reset token
*
* @param string $token password reset token
* @return static|null
*/
public static function findByPasswordResetToken($token)
{
if (!static::isPasswordResetTokenValid($token)) {
return null;
}
return static::findOne([
'password_reset_token' => $token,
'status' => self::STATUS_ACTIVE,
]);
}
/**
* Finds out if password reset token is valid
*
* @param string $token password reset token
* @return boolean
*/
public static function isPasswordResetTokenValid($token)
{
if (empty($token)) {
return false;
}
$expire = Yii::$app->params['user.passwordResetTokenExpire'];
$parts = explode('_', $token);
$timestamp = (int) end($parts);
return $timestamp + $expire >= time();
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->getPrimaryKey();
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->auth_key;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->getAuthKey() === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password_hash);
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password)
{
$this->password_hash = Yii::$app->security->generatePasswordHash($password);
}
/**
* Generates "remember me" authentication key
*/
public function generateAuthKey()
{
$this->auth_key = Yii::$app->security->generateRandomString();
}
/**
* Generates new password reset token
*/
public function generatePasswordResetToken()
{
$this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
}
/**
* Removes password reset token
*/
public function removePasswordResetToken()
{
$this->password_reset_token = null;
}
}
*
!.gitignore
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment