Merge pull request #12492 from appwrite/feat/impersonation-location-url-params-v2

feat: embed impersonateUserId in location-type SDK method URLs
This commit is contained in:
Harsh Mahajan authored and GitHub committed 2026-06-07 21:11:41 +05:30
commit 28e4b6cb65
21 files changed
+267 -21

No files matched your search

+3 -3
View File
@@ -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;
+3 -3
View File
@@ -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;
@@ -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', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
@@ -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', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
@@ -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', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
@@ -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', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
@@ -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', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
@@ -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', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
@@ -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', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
@@ -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', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
@@ -52,7 +52,8 @@ class Get extends Action
)
],
contentType: ContentType::ANY,
type: MethodType::LOCATION
type: MethodType::LOCATION,
locationAuth: ['Project', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
))
->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'])
@@ -51,6 +51,7 @@ class Get extends Action
)
],
type: MethodType::LOCATION,
locationAuth: ['Project', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
contentType: ContentType::ANY,
))
->param('siteId', '', fn (Database $dbForProject) => new UID($dbForProject->getAdapter()->getMaxUIDLength()), 'Site ID.', false, ['dbForProject'])
@@ -57,7 +57,8 @@ class Get extends Action
)
],
contentType: ContentType::ANY,
type: MethodType::LOCATION
type: MethodType::LOCATION,
locationAuth: ['Project', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
))
->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.')
@@ -71,6 +71,7 @@ class Get extends Action
)
],
type: MethodType::LOCATION,
locationAuth: ['Project', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
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).')
@@ -58,7 +58,8 @@ class Get extends Action
)
],
contentType: ContentType::ANY,
type: MethodType::LOCATION
type: MethodType::LOCATION,
locationAuth: ['Project', 'ImpersonateUserQuery', 'ImpersonateUserEmailQuery', 'ImpersonateUserPhoneQuery'],
))
->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.')
+54
View File
@@ -181,18 +181,36 @@ class Specs extends Action
'description' => 'Impersonate a user by ID on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.',
'in' => 'header',
],
'ImpersonateUserQuery' => [
'type' => 'apiKey',
'name' => 'impersonateUserId',
'description' => 'Impersonate a user by ID via query param. Used for location-type methods where headers cannot be set.',
'in' => 'query',
],
'ImpersonateUserEmail' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Impersonate-User-Email',
'description' => 'Impersonate a user by email on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.',
'in' => 'header',
],
'ImpersonateUserEmailQuery' => [
'type' => 'apiKey',
'name' => 'impersonateEmail',
'description' => 'Impersonate a user by email via query param. Used for location-type methods where headers cannot be set.',
'in' => 'query',
],
'ImpersonateUserPhone' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Impersonate-User-Phone',
'description' => 'Impersonate a user by phone on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.',
'in' => 'header',
],
'ImpersonateUserPhoneQuery' => [
'type' => 'apiKey',
'name' => 'impersonatePhone',
'description' => 'Impersonate a user by phone via query param. Used for location-type methods where headers cannot be set.',
'in' => 'query',
],
],
APP_SDK_PLATFORM_SERVER => [
'Project' => [
@@ -255,18 +273,36 @@ class Specs extends Action
'description' => 'Impersonate a user by ID on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.',
'in' => 'header',
],
'ImpersonateUserQuery' => [
'type' => 'apiKey',
'name' => 'impersonateUserId',
'description' => 'Impersonate a user by ID via query param. Used for location-type methods where headers cannot be set.',
'in' => 'query',
],
'ImpersonateUserEmail' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Impersonate-User-Email',
'description' => 'Impersonate a user by email on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.',
'in' => 'header',
],
'ImpersonateUserEmailQuery' => [
'type' => 'apiKey',
'name' => 'impersonateEmail',
'description' => 'Impersonate a user by email via query param. Used for location-type methods where headers cannot be set.',
'in' => 'query',
],
'ImpersonateUserPhone' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Impersonate-User-Phone',
'description' => 'Impersonate a user by phone on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.',
'in' => 'header',
],
'ImpersonateUserPhoneQuery' => [
'type' => 'apiKey',
'name' => 'impersonatePhone',
'description' => 'Impersonate a user by phone via query param. Used for location-type methods where headers cannot be set.',
'in' => 'query',
],
],
APP_SDK_PLATFORM_CONSOLE => [
'Project' => [
@@ -329,18 +365,36 @@ class Specs extends Action
'description' => 'Impersonate a user by ID on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.',
'in' => 'header',
],
'ImpersonateUserQuery' => [
'type' => 'apiKey',
'name' => 'impersonateUserId',
'description' => 'Impersonate a user by ID via query param. Used for location-type methods where headers cannot be set.',
'in' => 'query',
],
'ImpersonateUserEmail' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Impersonate-User-Email',
'description' => 'Impersonate a user by email on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.',
'in' => 'header',
],
'ImpersonateUserEmailQuery' => [
'type' => 'apiKey',
'name' => 'impersonateEmail',
'description' => 'Impersonate a user by email via query param. Used for location-type methods where headers cannot be set.',
'in' => 'query',
],
'ImpersonateUserPhone' => [
'type' => 'apiKey',
'name' => 'X-Appwrite-Impersonate-User-Phone',
'description' => 'Impersonate a user by phone on an already user-authenticated request. Requires the current request to be authenticated as a user with impersonator capability; X-Appwrite-Key alone is not sufficient. Impersonator users are intentionally granted users.read so they can discover a target before impersonation begins. Internal audit logs still attribute actions to the original impersonator and record the impersonated target only in internal audit payload data.',
'in' => 'header',
],
'ImpersonateUserPhoneQuery' => [
'type' => 'apiKey',
'name' => 'impersonatePhone',
'description' => 'Impersonate a user by phone via query param. Used for location-type methods where headers cannot be set.',
'in' => 'query',
],
],
];
}
+4 -8
View File
@@ -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<string> $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
@@ -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;
}
@@ -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;
}
@@ -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']);
}
}
@@ -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']);
}
}