diff --git a/app/init/realtime/connection.php b/app/init/realtime/connection.php index a090635bb5..ea00b7f356 100644 --- a/app/init/realtime/connection.php +++ b/app/init/realtime/connection.php @@ -329,9 +329,9 @@ return function (Container $container): void { // Query params mirror the header fallback pattern used by ?project= and ?devKey=, // allowing Console to embed impersonation in direct file/image URLs where headers cannot be set. - $impersonateUserId = $request->getHeader('x-appwrite-impersonate-user-id', (string)$request->getParam('impersonateUserId', '')); - $impersonateEmail = $request->getHeader('x-appwrite-impersonate-user-email', (string)$request->getParam('impersonateEmail', '')); - $impersonatePhone = $request->getHeader('x-appwrite-impersonate-user-phone', (string)$request->getParam('impersonatePhone', '')); + $impersonateUserId = $request->getHeader('x-appwrite-impersonate-user-id', (string)($request->getParam('impersonateuserid', '') ?: $request->getParam('impersonateUserId', ''))); + $impersonateEmail = $request->getHeader('x-appwrite-impersonate-user-email', (string)($request->getParam('impersonateemail', '') ?: $request->getParam('impersonateEmail', ''))); + $impersonatePhone = $request->getHeader('x-appwrite-impersonate-user-phone', (string)($request->getParam('impersonatephone', '') ?: $request->getParam('impersonatePhone', ''))); if (!$user->isEmpty() && $user->getAttribute('impersonator', false)) { $userDb = ($mode === APP_MODE_ADMIN || $project->getId() === 'console') ? $dbForPlatform : $dbForProject; diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 416c294140..f7a98e0f8f 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -491,9 +491,9 @@ return function (Container $context): void { // Impersonation: if current user has impersonator capability and headers/params are set, act as another user // Query params mirror the header fallback pattern used by ?project= and ?devKey=, // allowing Console to embed impersonation in direct file/image URLs where headers cannot be set. - $impersonateUserId = $request->getHeader('x-appwrite-impersonate-user-id', (string)$request->getParam('impersonateUserId', '')); - $impersonateEmail = $request->getHeader('x-appwrite-impersonate-user-email', (string)$request->getParam('impersonateEmail', '')); - $impersonatePhone = $request->getHeader('x-appwrite-impersonate-user-phone', (string)$request->getParam('impersonatePhone', '')); + $impersonateUserId = $request->getHeader('x-appwrite-impersonate-user-id', (string)($request->getParam('impersonateuserid', '') ?: $request->getParam('impersonateUserId', ''))); + $impersonateEmail = $request->getHeader('x-appwrite-impersonate-user-email', (string)($request->getParam('impersonateemail', '') ?: $request->getParam('impersonateEmail', ''))); + $impersonatePhone = $request->getHeader('x-appwrite-impersonate-user-phone', (string)($request->getParam('impersonatephone', '') ?: $request->getParam('impersonatePhone', ''))); if (!$user->isEmpty() && $user->getAttribute('impersonator', false)) { $userDb = (APP_MODE_ADMIN === $mode || $project->getId() === 'console') ? $dbForPlatform : $dbForProject; $targetUser = null; diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Browsers/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Browsers/Get.php index cc30f014bc..24529759d9 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Browsers/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Browsers/Get.php @@ -42,6 +42,7 @@ class Get extends Action description: '/docs/references/avatars/get-browser.md', auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::KEY, AuthType::JWT], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/CreditCards/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/CreditCards/Get.php index 3bae32c862..28149b3945 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/CreditCards/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/CreditCards/Get.php @@ -42,6 +42,7 @@ class Get extends Action description: '/docs/references/avatars/get-credit-card.md', auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::KEY, AuthType::JWT], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php index 6faa0fe223..de02f59873 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Favicon/Get.php @@ -54,6 +54,7 @@ class Get extends Action description: '/docs/references/avatars/get-favicon.md', auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::KEY, AuthType::JWT], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Flags/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Flags/Get.php index ef9f8c0e47..7f32cdaa45 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Flags/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Flags/Get.php @@ -42,6 +42,7 @@ class Get extends Action description: '/docs/references/avatars/get-flag.md', auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::KEY, AuthType::JWT], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php index 315202d4d5..ad4ed2ff37 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Image/Get.php @@ -45,6 +45,7 @@ class Get extends Action description: '/docs/references/avatars/get-image.md', auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::KEY, AuthType::JWT], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Initials/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Initials/Get.php index c73d20fb0c..4808cb7267 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Initials/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Initials/Get.php @@ -44,6 +44,7 @@ class Get extends Action description: '/docs/references/avatars/get-initials.md', auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::KEY, AuthType::JWT], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/QR/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/QR/Get.php index f3448f5264..ac08e060f2 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/QR/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/QR/Get.php @@ -42,6 +42,7 @@ class Get extends Action description: '/docs/references/avatars/get-qr.md', auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::KEY, AuthType::JWT], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, diff --git a/src/Appwrite/Platform/Modules/Avatars/Http/Screenshots/Get.php b/src/Appwrite/Platform/Modules/Avatars/Http/Screenshots/Get.php index 51d4df2a49..15b9408616 100644 --- a/src/Appwrite/Platform/Modules/Avatars/Http/Screenshots/Get.php +++ b/src/Appwrite/Platform/Modules/Avatars/Http/Screenshots/Get.php @@ -57,6 +57,7 @@ class Get extends Action description: '/docs/references/avatars/get-screenshot.md', auth: [AuthType::ADMIN, AuthType::SESSION, AuthType::KEY, AuthType::JWT], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], responses: [ new SDKResponse( code: Response::STATUS_CODE_OK, diff --git a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php index c92e3c1406..d6475b4914 100644 --- a/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Functions/Http/Deployments/Download/Get.php @@ -52,7 +52,8 @@ class Get extends Action ) ], contentType: ContentType::ANY, - type: MethodType::LOCATION + type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], )) ->param('functionId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Function ID.', false, ['dbForProject']) ->param('deploymentId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Deployment ID.', false, ['dbForProject']) diff --git a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php index 0ab998292d..6fbafd908b 100644 --- a/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Sites/Http/Deployments/Download/Get.php @@ -51,6 +51,7 @@ class Get extends Action ) ], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], contentType: ContentType::ANY, )) ->param('siteId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Site ID.', false, ['dbForProject']) diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php index 7e5d7d6879..160fd30c37 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Download/Get.php @@ -57,7 +57,8 @@ class Get extends Action ) ], contentType: ContentType::ANY, - type: MethodType::LOCATION + type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], )) ->param('bucketId', '', new UID(), 'Storage bucket ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).') ->param('fileId', '', new UID(), 'File ID.') diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php index 9c316a3b57..2a2686b544 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/Preview/Get.php @@ -71,6 +71,7 @@ class Get extends Action ) ], type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], contentType: ContentType::IMAGE )) ->param('bucketId', '', new UID(), 'Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).') diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php index b78d582c47..4ff3360eab 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Files/View/Get.php @@ -58,7 +58,8 @@ class Get extends Action ) ], contentType: ContentType::ANY, - type: MethodType::LOCATION + type: MethodType::LOCATION, + locationAuth: ['Project', 'ImpersonateUserId'], )) ->param('bucketId', '', new UID(), 'Storage bucket unique ID. You can create a new storage bucket using the Storage service [server integration](https://appwrite.io/docs/server/storage#createBucket).') ->param('fileId', '', new UID(), 'File ID.') diff --git a/src/Appwrite/SDK/Method.php b/src/Appwrite/SDK/Method.php index 5f6ccf9a0f..f1b327513f 100644 --- a/src/Appwrite/SDK/Method.php +++ b/src/Appwrite/SDK/Method.php @@ -32,7 +32,7 @@ class Method * @param array $additionalParameters * @param string $desc * @param bool $public Whether this method should be rendered on the website/documentation - * @param string $projectAuth Project authentication security scheme to use in specs + * @param array $locationAuth Security scheme keys injected for location-type methods (includes project auth) */ public function __construct( protected string $namespace, @@ -51,16 +51,12 @@ class Method protected array $additionalParameters = [], protected string $desc = '', protected bool $public = true, - protected string $projectAuth = 'Project' + protected array $locationAuth = [] ) { $this->validateMethod($name, $namespace); $this->validateAuthTypes($auth); $this->validateDesc($description); - if (!\in_array($projectAuth, ['Project', 'ProjectQuery'], true)) { - self::$errors[] = "Error with {$this->getRouteName()} method: Invalid project auth scheme"; - } - foreach ($responses as $response) { $this->validateResponseModel($response->getModel()); $this->validateNoContent($response); @@ -229,9 +225,9 @@ class Method return $this->additionalParameters; } - public function getProjectAuth(): string + public function getLocationAuth(): array { - return $this->projectAuth; + return $this->locationAuth; } public function setNamespace(string $namespace): self diff --git a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php index dfa38b0bf5..d9c2bf301e 100644 --- a/src/Appwrite/SDK/Specification/Format/OpenAPI3.php +++ b/src/Appwrite/SDK/Specification/Format/OpenAPI3.php @@ -188,7 +188,7 @@ class OpenAPI3 extends Format continue; } - $methodSecurities = [$methodObj->getProjectAuth() => []]; + $methodSecurities = [($methodObj->getLocationAuth()[0] ?? 'Project') => []]; foreach ($methodObj->getAuth() as $security) { if (\array_key_exists($security->value, $this->keys)) { $methodSecurities[$security->value] = []; @@ -374,7 +374,7 @@ class OpenAPI3 extends Format } if (!empty($scope)) { - $securities = [$sdk->getProjectAuth() => []]; + $securities = [($sdk->getLocationAuth()[0] ?? 'Project') => []]; foreach ($sdk->getAuth() as $security) { /** @var AuthType $security */ @@ -384,6 +384,16 @@ class OpenAPI3 extends Format } $temp['x-appwrite']['auth'] = array_slice($securities, 0, $this->authCount); + + if ($sdk->getType() === MethodType::LOCATION) { + foreach ($sdk->getLocationAuth() as $key) { + if (\array_key_exists($key, $this->keys)) { + $securities[$key] = []; + $temp['x-appwrite']['auth'][$key] = []; + } + } + } + $temp['security'][] = $securities; } diff --git a/src/Appwrite/SDK/Specification/Format/Swagger2.php b/src/Appwrite/SDK/Specification/Format/Swagger2.php index 20b1fb9b94..b1541d5ddb 100644 --- a/src/Appwrite/SDK/Specification/Format/Swagger2.php +++ b/src/Appwrite/SDK/Specification/Format/Swagger2.php @@ -188,7 +188,7 @@ class Swagger2 extends Format continue; } - $methodSecurities = [$methodObj->getProjectAuth() => []]; + $methodSecurities = [($methodObj->getLocationAuth()[0] ?? 'Project') => []]; foreach ($methodObj->getAuth() as $security) { /** @var AuthType $security */ if (\array_key_exists($security->value, $this->keys)) { @@ -365,7 +365,7 @@ class Swagger2 extends Format } if (!empty($scope)) { - $securities = [$sdk->getProjectAuth() => []]; + $securities = [($sdk->getLocationAuth()[0] ?? 'Project') => []]; foreach ($sdk->getAuth() as $security) { if (\array_key_exists($security->value, $this->keys)) { @@ -374,6 +374,16 @@ class Swagger2 extends Format } $temp['x-appwrite']['auth'] = \array_slice($securities, 0, $this->authCount); + + if ($sdk->getType() === MethodType::LOCATION) { + foreach ($sdk->getLocationAuth() as $key) { + if (\array_key_exists($key, $this->keys)) { + $securities[$key] = []; + $temp['x-appwrite']['auth'][$key] = []; + } + } + } + $temp['security'][] = $securities; } diff --git a/tests/e2e/Services/Storage/StorageConsoleClientTest.php b/tests/e2e/Services/Storage/StorageConsoleClientTest.php index 9d2a639631..aca2717170 100644 --- a/tests/e2e/Services/Storage/StorageConsoleClientTest.php +++ b/tests/e2e/Services/Storage/StorageConsoleClientTest.php @@ -10,6 +10,8 @@ use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; use Tests\E2E\Scopes\SideConsole; use Utopia\Database\Helpers\ID; +use Utopia\Database\Helpers\Permission; +use Utopia\Database\Helpers\Role; final class StorageConsoleClientTest extends Scope { @@ -198,4 +200,82 @@ final class StorageConsoleClientTest extends Scope ], $this->getHeaders())); $this->assertEquals(204, $response['headers']['status-code']); } + + public function testFilePreviewWithImpersonation(): void + { + $projectId = $this->getProject()['$id']; + $adminHeaders = array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()); + + $targetId = ID::unique(); + $target = $this->client->call(Client::METHOD_POST, '/users', $adminHeaders, [ + 'userId' => $targetId, + 'email' => 'impersonation-storage-target-' . $targetId . '@example.com', + 'password' => 'password123', + 'name' => 'Storage Target', + ]); + $this->assertEquals(201, $target['headers']['status-code']); + + $actorId = ID::unique(); + $this->client->call(Client::METHOD_POST, '/users', $adminHeaders, [ + 'userId' => $actorId, + 'email' => 'impersonation-storage-actor-' . $actorId . '@example.com', + 'password' => 'password123', + 'name' => 'Storage Actor', + ]); + $this->client->call(Client::METHOD_PATCH, '/users/' . $actorId . '/impersonator', $adminHeaders, [ + 'impersonator' => true, + ]); + + $bucket = $this->client->call(Client::METHOD_POST, '/storage/buckets', $adminHeaders, [ + 'bucketId' => ID::unique(), + 'name' => 'Impersonation Test Bucket', + 'fileSecurity' => true, + 'permissions' => [ + Permission::read(Role::user($targetId)), + Permission::create(Role::any()), + ], + ]); + $this->assertEquals(201, $bucket['headers']['status-code']); + $bucketId = $bucket['body']['$id']; + + $file = $this->client->call(Client::METHOD_POST, '/storage/buckets/' . $bucketId . '/files', array_merge( + $adminHeaders, + ['content-type' => 'multipart/form-data'] + ), [ + 'fileId' => ID::unique(), + 'file' => new CURLFile(realpath(__DIR__ . '/../../../resources/logo.png'), 'image/png', 'logo.png'), + 'permissions' => [ + Permission::read(Role::user($targetId)), + ], + ]); + $this->assertEquals(201, $file['headers']['status-code']); + $fileId = $file['body']['$id']; + + $session = $this->client->call(Client::METHOD_POST, '/users/' . $actorId . '/sessions', $adminHeaders); + $this->assertEquals(201, $session['headers']['status-code']); + + $sessionHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'x-appwrite-session' => $session['body']['secret'], + ]; + + $denied = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $fileId . '/preview', $sessionHeaders); + $this->assertEquals(404, $denied['headers']['status-code']); + + $preview = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $fileId . '/preview', $sessionHeaders, [ + 'impersonateuserid' => $targetId, + ]); + $this->assertEquals(200, $preview['headers']['status-code']); + $this->assertEquals('image/png', $preview['headers']['content-type']); + $this->assertNotEmpty($preview['body']); + + $view = $this->client->call(Client::METHOD_GET, '/storage/buckets/' . $bucketId . '/files/' . $fileId . '/view', $sessionHeaders, [ + 'impersonateuserid' => $targetId, + ]); + $this->assertEquals(200, $view['headers']['status-code']); + } } diff --git a/tests/e2e/Services/Users/UsersConsoleClientTest.php b/tests/e2e/Services/Users/UsersConsoleClientTest.php index 1ac901bf58..373b941fb4 100644 --- a/tests/e2e/Services/Users/UsersConsoleClientTest.php +++ b/tests/e2e/Services/Users/UsersConsoleClientTest.php @@ -83,4 +83,87 @@ final class UsersConsoleClientTest extends Scope $this->assertEquals($userId, $set['body']['$id']); $this->assertNotEmpty($set['body']['password']); } + + public function testImpersonateQueryParams(): void + { + $projectId = $this->getProject()['$id']; + $headers = array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + ], $this->getHeaders()); + + $actorId = ID::unique(); + $actor = $this->client->call(Client::METHOD_POST, '/users', $headers, [ + 'userId' => $actorId, + 'email' => 'impersonator-queryparam-' . $actorId . '@example.com', + 'password' => 'password123', + 'name' => 'Impersonator', + ]); + $this->assertEquals(201, $actor['headers']['status-code']); + + $targetId = ID::unique(); + $targetEmail = 'target-queryparam-' . $targetId . '@example.com'; + $targetPhone = '+1' . rand(2000000000, 2999999999); + + $target = $this->client->call(Client::METHOD_POST, '/users', $headers, [ + 'userId' => $targetId, + 'email' => $targetEmail, + 'password' => 'password123', + 'name' => 'Target', + ]); + $this->assertEquals(201, $target['headers']['status-code']); + + $this->client->call(Client::METHOD_PATCH, '/users/' . $targetId . '/phone', $headers, [ + 'number' => $targetPhone, + ]); + + $this->client->call(Client::METHOD_PATCH, '/users/' . $actorId . '/impersonator', $headers, [ + 'impersonator' => true, + ]); + + $session = $this->client->call(Client::METHOD_POST, '/users/' . $actorId . '/sessions', $headers); + $this->assertEquals(201, $session['headers']['status-code']); + + $sessionHeaders = [ + 'origin' => 'http://localhost', + 'content-type' => 'application/json', + 'x-appwrite-project' => $projectId, + 'x-appwrite-session' => $session['body']['secret'], + ]; + + $byHeader = $this->client->call(Client::METHOD_GET, '/account', array_merge($sessionHeaders, [ + 'x-appwrite-impersonate-user-id' => $targetId, + ])); + $this->assertEquals(200, $byHeader['headers']['status-code']); + $this->assertEquals($targetId, $byHeader['body']['$id']); + $this->assertEquals($actorId, $byHeader['body']['impersonatorUserId']); + + $byUserId = $this->client->call(Client::METHOD_GET, '/account', $sessionHeaders, [ + 'impersonateuserid' => $targetId, + ]); + $this->assertEquals(200, $byUserId['headers']['status-code']); + $this->assertEquals($targetId, $byUserId['body']['$id']); + + $byEmail = $this->client->call(Client::METHOD_GET, '/account', $sessionHeaders, [ + 'impersonateemail' => $targetEmail, + ]); + $this->assertEquals(200, $byEmail['headers']['status-code']); + $this->assertEquals($targetId, $byEmail['body']['$id']); + + $byPhone = $this->client->call(Client::METHOD_GET, '/account', $sessionHeaders, [ + 'impersonatephone' => $targetPhone, + ]); + $this->assertEquals(200, $byPhone['headers']['status-code']); + $this->assertEquals($targetId, $byPhone['body']['$id']); + + // header takes priority over query param + $priority = $this->client->call( + Client::METHOD_GET, + '/account', + array_merge($sessionHeaders, ['x-appwrite-impersonate-user-id' => $targetId]), + ['impersonateuserid' => $actorId] + ); + $this->assertEquals(200, $priority['headers']['status-code']); + $this->assertEquals($targetId, $priority['body']['$id']); + } }