Jelajahi Sumber

rest api WIP

Qiang Xue 12 tahun lalu
induk
melakukan
e9148837ee
1 mengubah file dengan 16 tambahan dan 0 penghapusan
  1. 16 0
      models/User.php

+ 16 - 0
models/User.php

@@ -8,6 +8,7 @@ class User extends \yii\base\Object implements \yii\web\IdentityInterface
 	public $username;
 	public $password;
 	public $authKey;
+	public $apiKey;
 
 	private static $users = [
 		'100' => [
@@ -15,12 +16,14 @@ class User extends \yii\base\Object implements \yii\web\IdentityInterface
 			'username' => 'admin',
 			'password' => 'admin',
 			'authKey' => 'test100key',
+			'apiKey' => '100-apikey',
 		],
 		'101' => [
 			'id' => '101',
 			'username' => 'demo',
 			'password' => 'demo',
 			'authKey' => 'test101key',
+			'apiKey' => '101-apikey',
 		],
 	];
 
@@ -33,6 +36,19 @@ class User extends \yii\base\Object implements \yii\web\IdentityInterface
 	}
 
 	/**
+	 * @inheritdoc
+	 */
+	public static function findIdentityByToken($token)
+	{
+		foreach (self::$users as $user) {
+			if ($user['apiKey'] === $token) {
+				return new static($user);
+			}
+		}
+		return null;
+	}
+
+	/**
 	 * Finds user by username
 	 *
 	 * @param string $username