123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?php
- namespace app\models;
- class User extends \yii\base\Object implements \yii\web\IdentityInterface
- {
- public $id;
- public $username;
- public $password;
- public $authKey;
- public $accessToken;
- public $forename;
- public $surname;
- public $email;
- public $phone;
- private static $users = [
- '100' => [
- 'id' => '100',
- 'username' => 'admin',
- 'password' => 'test',
- 'authKey' => 'test100key',
- 'accessToken' => '100-token',
- //'role' => 'admin',
- ],
- '101' => [
- 'id' => '101',
- 'username' => 'hr',
- 'password' => 'test',
- 'authKey' => 'test101key',
- 'accessToken' => '101-token',
- //'role' => 'hr',
- ],
- '102' => [
- 'id' => '102',
- 'username' => 'employee',
- 'password' => 'test',
- 'authKey' => 'test102key',
- 'accessToken' => '102-token',
- //'role' => 'employee',
- ],
- ];
- public static function tableName()
- {
- return 'testUsers';
- }
- /**
- * @inheritdoc
- */
- public static function findIdentity($id)
- {
- return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;/*
- $dbUser = DbUser::find()
- ->where([
- "id" => $id
- ])
- ->one();
- if (!count($dbUser)) {
- return null;
- }
- return new static($dbUser);*/
- }
- /**
- * @inheritdoc
- */
- public static function findIdentityByAccessToken($token, $type = null)
- {
- foreach (self::$users as $user) {
- if ($user['accessToken'] === $token) {
- return new static($user);
- }
- }
- return null;
- }
- /**
- * Finds user by username
- *
- * @param string $username
- * @return static|null
- */
- public static function findByUsername($username)
- {
- foreach (self::$users as $user) {
- if (strcasecmp($user['username'], $username) === 0) {
- return new static($user);
- }
- }
- return null;
- }
- /**
- * @inheritdoc
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * @inheritdoc
- */
- public function getAuthKey()
- {
- return $this->authKey;
- }
- /**
- * @inheritdoc
- */
- public function validateAuthKey($authKey)
- {
- return $this->authKey === $authKey;
- }
- /**
- * Validates password
- *
- * @param string $password password to validate
- * @return bool if password provided is valid for current user
- */
- public function validatePassword($password)
- {
- return $this->password === $password;
- }
- }
|