Compare commits

...
Author SHA1 Message Date
Bradley Schofield 46ec868e8b Continue working 2023-02-13 15:52:23 +00:00
Torsten Dittmann e16912d6ef experiment: multiple events 2023-01-27 11:51:26 +01:00
3 changed files with 88 additions and 2 deletions
+7 -2
View File
@@ -514,6 +514,8 @@ App::get('/v1/account/sessions/oauth2/:provider/redirect')
'memberships' => null,
'search' => implode(' ', [$userId, $email, $name])
])));
$events->addAdditionalEvent('users.[userId].create', ['userId' => $user->getId()], $response->output($user, Response::MODEL_USER));
} catch (Duplicate $th) {
throw new Exception(Exception::USER_ALREADY_EXISTS);
}
@@ -1898,7 +1900,7 @@ App::delete('/v1/account/sessions')
$protocol = $request->getProtocol();
$sessions = $user->getAttribute('sessions', []);
foreach ($sessions as $session) {/** @var Document $session */
foreach ($sessions as $key => $session) {/** @var Document $session */
$dbForProject->deleteDocument('sessions', $session->getId());
if (!Config::getParam('domainVerification')) {
@@ -1920,7 +1922,10 @@ App::delete('/v1/account/sessions')
->addCookie(Auth::$cookieName, '', \time() - 3600, '/', Config::getParam('cookieDomain'), ('https' == $protocol), true, Config::getParam('cookieSamesite'));
// Use current session for events.
$events->setPayload($response->output($session, Response::MODEL_SESSION));
$events->addAdditionalEvent('users.[userId].sessions.[sessionId].delete', ['userId' => $user->getId(), 'sessionId' => $session->getId()], $response->output($session, Response::MODEL_SESSION));
if ($key === array_key_last($sessions)) {
$events->setPayload($response->output($session, Response::MODEL_SESSION));
}
}
}
+22
View File
@@ -41,6 +41,7 @@ class Event
protected array $params = [];
protected array $payload = [];
protected array $context = [];
protected array $additionalEvents = [];
protected ?Document $project = null;
protected ?Document $user = null;
@@ -260,6 +261,16 @@ class Event
*/
public function trigger(): string|bool
{
foreach ($this->additionalEvents as $event) {
Resque::enqueue($this->queue, $this->class, [
'project' => $this->project,
'user' => $this->user,
'payload' => $event['payload'],
'context' => $this->context,
'events' => Event::generateEvents($event['event'], $event['params'])
]);
}
return Resque::enqueue($this->queue, $this->class, [
'project' => $this->project,
'user' => $this->user,
@@ -281,6 +292,17 @@ class Event
return $this;
}
public function addAdditionalEvent(string $event, array $params = [], array $payload = []): self
{
$this->additionalEvents[] = [
'event' => $event,
'params' => $params,
'payload' => $payload
];
return $this;
}
/**
* Parses event pattern and returns the parts in their respective section.
*
@@ -124,6 +124,65 @@ class RealtimeConsoleClientTest extends Scope
$client->close();
}
public function testOAuthAuthentication()
{
$projectId = 'console';
$client = $this->getWebsocket(['console'], [
'origin' => 'http://localhost',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
], $projectId);
$response = json_decode($client->receive(), true);
$this->assertArrayHasKey('type', $response);
$this->assertArrayHasKey('data', $response);
$this->assertEquals('connected', $response['type']);
$this->assertNotEmpty($response['data']);
$this->assertCount(1, $response['data']['channels']);
$this->assertContains('console', $response['data']['channels']);
$this->assertNotEmpty($response['data']['user']);
/**
* Create a new user using OAuth
*/
$response = $this->client->call(Client::METHOD_PATCH, '/projects/' . $this->getProject()['$id'] . '/oauth2', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => 'console',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
]), [
'provider' => 'mock',
'appId' => '1',
'secret' => '123456',
]);
$this->assertEquals($response['headers']['status-code'], 200);
$response = $this->client->call(Client::METHOD_GET, '/account/sessions/oauth2/mock', array_merge([
'origin' => 'http://localhost',
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
]), [
'success' => 'http://localhost/v1/mock/tests/general/oauth2/success',
'failure' => 'http://localhost/v1/mock/tests/general/oauth2/failure',
]);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('success', $response['body']['result']);
$client = $this->getWebsocket(['console'], [
'origin' => 'http://localhost',
'cookie' => 'a_session_console=' . $this->getRoot()['session'],
], $projectId);
$response = json_decode($client->receive(), true);
var_dump($response);
$client->close();
}
public function testAttributes()
{
$user = $this->getUser();