Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace appmodels;
- use Yii;
- use yiibaseException;
- use yiibaseNotSupportedException;
- use yiibaseSecurity;
- use yiiwebIdentityInterface;
- /**
- * This is the model class for table "users".
- *
- * @property int $id
- * @property string $email
- * @property string $password
- * @property string $username
- * @property string $authKey
- */
- class Blogger extends yiidbActiveRecord implements yiiwebIdentityInterface
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'users';
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['email', 'password', 'username'], 'required'],
- [['email', 'password', 'username'], 'string', 'max' => 50],
- [['authKey'], 'string', 'max' => 50],
- [['authKey'], 'unique'],
- ];
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'email' => 'Email',
- 'password' => 'Password',
- 'username' => 'Username',
- ];
- }
- public function getId()
- {
- return $this->id;
- }
- public static function findIdentity($id)
- {
- return self::findOne($id);
- }
- public static function findByUsername($username)
- {
- return self::findOne(['username' => $username]);
- }
- public function validatePassword($password)
- {
- return $this->password === $password;
- }
- public static function findIdentityByAccessToken($token, $type = null)
- {
- throw new NotSupportedException();
- }
- public function getAuthKey()
- {
- return $this->authKey;
- }
- public function validateAuthKey($authKey)
- {
- return $this->authKey === $authKey;
- }
- }
- namespace appmodels;
- use Yii;
- use yiibaseModel;
- /**
- * 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*24*30 : 0);
- }
- return false;
- }
- /**
- * Finds user by [[username]]
- *
- * @return Blogger|null
- */
- public function getUser()
- {
- if ($this->_user === false) {
- $this->_user = Blogger::findByUsername($this->username);
- }
- return $this->_user;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement