added users.read, users.write currently for the scoping

This commit is contained in:
ArnabChatterjee20k
2026-04-13 22:37:17 +05:30
parent 54fa813bc6
commit 0935c60f3c
7 changed files with 65 additions and 24 deletions
@@ -30,7 +30,7 @@ class Delete extends Base
->setHttpPath('/v1/presences/:presenceId')
->desc('Delete presence')
->groups(['api', 'presences'])
->label('scope', 'documents.write')
->label('scope', 'users.write')
->label('sdk', new Method(
namespace: 'presences',
group: 'presences',
@@ -29,7 +29,7 @@ class Get extends Base
->setHttpPath('/v1/presences/:presenceId')
->desc('Get presence')
->groups(['api', 'presences'])
->label('scope', 'documents.read')
->label('scope', 'users.read')
->label('sdk', new Method(
namespace: 'presences',
group: 'presences',
@@ -34,7 +34,7 @@ class Update extends PresenceAction
->setHttpPath('/v1/presences/:presenceId')
->desc('Update presence')
->groups(['api', 'presences'])
->label('scope', 'documents.write')
->label('scope', 'users.write')
->label('sdk', new Method(
namespace: 'presences',
group: 'presences',
@@ -40,7 +40,7 @@ class Upsert extends PresenceAction
->setHttpPath('/v1/presences/:presenceId')
->desc('Upsert presence')
->groups(['api', 'presences'])
->label('scope', 'documents.write')
->label('scope', 'users.write')
->label('sdk', new Method(
namespace: 'presences',
group: 'presences',
@@ -37,7 +37,7 @@ class XList extends Base
->setHttpPath('/v1/presences')
->desc('List presences')
->groups(['api', 'presences'])
->label('scope', 'documents.read')
->label('scope', 'users.read')
->label('sdk', new Method(
namespace: 'presences',
group: 'presences',
+30 -13
View File
@@ -9,6 +9,23 @@ use Utopia\Database\Query;
trait PresenceBase
{
private static array $presenceCache = [];
private static array $presenceApiKeyCache = [];
protected function getPresenceApiKey(): string
{
$projectId = $this->getProject()['$id'];
if (!empty(self::$presenceApiKeyCache[$projectId])) {
return self::$presenceApiKeyCache[$projectId];
}
self::$presenceApiKeyCache[$projectId] = $this->getNewKey([
'users.read',
'users.write',
]);
return self::$presenceApiKeyCache[$projectId];
}
protected function setupPresence(array $overrides = []): array
{
@@ -34,7 +51,7 @@ trait PresenceBase
[
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
'x-appwrite-key' => $this->getPresenceApiKey(),
],
$payload
);
@@ -62,7 +79,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
], $this->getHeaders(false)),
[
'status' => 'online',
'metadata' => ['device' => 'web'],
@@ -81,7 +98,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders())
], $this->getHeaders(false))
);
$this->assertEquals(200, $get['headers']['status-code']);
@@ -99,7 +116,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders())
], $this->getHeaders(false))
);
$this->assertEquals(401, $list['headers']['status-code']);
@@ -114,7 +131,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
], $this->getHeaders(false)),
[
'queries' => [
Query::equal('userId', [$presence['userId']])->toString(),
@@ -138,7 +155,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
], $this->getHeaders(false)),
[
'status' => 'busy',
'metadata' => ['source' => 'update'],
@@ -169,7 +186,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
], $this->getHeaders(false)),
$payload
);
@@ -187,7 +204,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders())
], $this->getHeaders(false))
);
$this->assertEquals(401, $delete['headers']['status-code']);
@@ -205,7 +222,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders())
], $this->getHeaders(false))
);
$this->assertEquals(204, $delete['headers']['status-code']);
@@ -220,7 +237,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
], $this->getHeaders(false)),
[
'status' => 'ghost',
]
@@ -244,7 +261,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
], $this->getHeaders(false)),
$payload
);
@@ -264,7 +281,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
], $this->getHeaders(false)),
[
'userId' => ID::unique(),
'status' => 'online',
@@ -287,7 +304,7 @@ trait PresenceBase
\array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()),
], $this->getHeaders(false)),
[
'status' => 'online',
]
@@ -14,6 +14,24 @@ class PresenceExpiryTest extends Scope
use ProjectCustom;
use SideServer;
private static array $presenceApiKeyCache = [];
private function getPresenceApiKey(): string
{
$projectId = $this->getProject()['$id'];
if (!empty(self::$presenceApiKeyCache[$projectId])) {
return self::$presenceApiKeyCache[$projectId];
}
self::$presenceApiKeyCache[$projectId] = $this->getNewKey([
'users.read',
'users.write',
]);
return self::$presenceApiKeyCache[$projectId];
}
public function testExpiredPresenceDeletedByMaintenance(): void
{
$projectId = $this->getProject()['$id'];
@@ -26,7 +44,7 @@ class PresenceExpiryTest extends Scope
[
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
'x-appwrite-key' => $this->getPresenceApiKey(),
],
[
'userId' => $userId,
@@ -44,7 +62,7 @@ class PresenceExpiryTest extends Scope
[
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
'x-appwrite-key' => $this->getPresenceApiKey(),
],
[
'userId' => $userId,
@@ -53,7 +71,10 @@ class PresenceExpiryTest extends Scope
);
$this->assertEquals(200, $expireServer['headers']['status-code']);
$this->assertEquals($expiredAt, $expireServer['body']['expiry']);
$this->assertEquals(
(new \DateTime($expiredAt))->getTimestamp(),
(new \DateTime($expireServer['body']['expiry']))->getTimestamp()
);
$createClient = $this->client->call(
Client::METHOD_PUT,
@@ -88,7 +109,10 @@ class PresenceExpiryTest extends Scope
);
$this->assertEquals(200, $expireClient['headers']['status-code']);
$this->assertEquals($expiredAt, $expireClient['body']['expiry']);
$this->assertEquals(
(new \DateTime($expiredAt))->getTimestamp(),
(new \DateTime($expireClient['body']['expiry']))->getTimestamp()
);
$stdout = '';
$stderr = '';
@@ -102,7 +126,7 @@ class PresenceExpiryTest extends Scope
[
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
'x-appwrite-key' => $this->getPresenceApiKey(),
]
);
@@ -112,7 +136,7 @@ class PresenceExpiryTest extends Scope
[
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $this->getProject()['apiKey'],
'x-appwrite-key' => $this->getPresenceApiKey(),
]
);