/** * Finds user by [[username]] * * @return Admin|null */ public function getUser() { if ($this->_user === false) { $this->_user = Admin::findByUsername($this->username); } return $this->_user; }
/** * Creates a form model given a token. * * @param string $token * @param array $config name-value pairs that will be used to initialize the object properties * @throws \yii\base\InvalidParamException if token is empty or not valid */ public function __construct($token, $config = []) { parent::__construct($config); if (empty($token) || !is_string($token)) { throw new InvalidParamException(Yii::t('admin', 'Missing password reset token.')); } $this->_user = Admin::findByPasswordResetToken($token); if (!$this->_user) { throw new InvalidParamException(Yii::t('admin', 'Invalid password reset token.')); } }
public function validateUnique($attribute, $params) { $query = Admin::find(); /* @var $query \yii\db\ActiveQuery */ $query->where([$attribute => $this->{$attribute}]); if (!$this->_user->isNewRecord) { $query->andWhere(['!=', 'id', $this->_user->id]); } if ($query->exists()) { $this->addError($attribute, Yii::t('admin', '{attribute} "{value}" is already in use.', ['attribute' => $this->getAttributeLabel($attribute), 'value' => $this->{$attribute}])); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params, $options = []) { $query = Admin::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => isset($options['pageSize']) ? $options['pageSize'] : 20]]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email]); return $dataProvider; }
/** * Sends an email with a link, for resetting the password. * * @return boolean whether the email was send */ public function sendEmail() { /* @var $admin Admin */ $admin = Admin::findOne(['email' => $this->email]); if ($admin) { if (!Admin::isPasswordResetTokenValid($admin->pwd_reset_token)) { $admin->generatePasswordResetToken(); } if ($admin->save()) { return \Yii::$app->mailer->compose(['html' => 'admin/password_reset.html.php', 'text' => 'admin/password_reset.txt.php'], ['user' => $admin])->setFrom([Yii::$app->params['from.email'] => Yii::$app->params['from.name']])->setTo($this->email)->setSubject(Yii::t('admin', 'Password reset for {appName}'), ['appName' => Yii::$app->name])->send(); } } return false; }
/** * Finds the Admin model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Admin the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Admin::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }