Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Commit

Permalink
Log user in to query/encode entities on publishes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hperrin committed Mar 7, 2019
1 parent 01b4c56 commit 9608fd5
Showing 1 changed file with 62 additions and 41 deletions.
103 changes: 62 additions & 41 deletions src/MessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,18 @@ private function handleSubscriptionQuery(ConnectionInterface $from, $data) {
}
$guidArgs = $args;
$guidArgs[0]['return'] = 'guid';
$guidArgs[0]['source'] = 'pubsub';
$guidArgs[0]['source'] = 'client';
if ($this->sessions->contains($from)) {
$guidArgs[0]['token'] = $this->sessions[$from];
$token = $this->sessions[$from];
} else {
$guidArgs[0]['token'] = null;
$token = null;
}
if (class_exists('\Tilmeld\Tilmeld') && isset($token)) {
$user = \Tilmeld\Tilmeld::extractToken($token);
if ($user) {
// Log in the user for access controls.
\Tilmeld\Tilmeld::fillSession($user);
}
}
$this->querySubs[$serialArgs]->attach($from, [
'current' => call_user_func_array(
Expand All @@ -210,6 +217,10 @@ private function handleSubscriptionQuery(ConnectionInterface $from, $data) {
'query' => $data['query'],
'count' => !!$data['count']
]);
if (class_exists('\Tilmeld\Tilmeld') && isset($token)) {
// Clear the user that was temporarily logged in.
\Tilmeld\Tilmeld::clearSession();
}
$this->logger->notice(
"Client subscribed to a query! ".
"($serialArgs, {$from->resourceId})"
Expand Down Expand Up @@ -406,14 +417,20 @@ private function handlePublishEntity(ConnectionInterface $from, $data) {
// Update currents list.
$queryArgs = unserialize($curQuery);
$this->prepareSelectors($queryArgs);
$queryArgs[0]['source'] = 'pubsub';
$queryArgs[0]['source'] = 'client';
$queryArgs[] = ['&', 'guid' => $data['guid']];
if ($this->sessions->contains($curClient)) {
$token = $this->sessions[$curClient];
} else {
$token = null;
}
$queryArgs[0]['token'] = $token;
$queryArgs[] = ['&', 'guid' => $data['guid']];
if (class_exists('\Tilmeld\Tilmeld') && isset($token)) {
$user = \Tilmeld\Tilmeld::extractToken($token);
if ($user) {
// Log in the user for access controls.
\Tilmeld\Tilmeld::fillSession($user);
}
}
$current = call_user_func_array(
"\Nymph\Nymph::getEntity",
$queryArgs
Expand All @@ -425,14 +442,8 @@ private function handlePublishEntity(ConnectionInterface $from, $data) {
"Notifying client of update! ".
"({$curClient->resourceId})"
);
if (is_callable([$current, 'updateDataProtection'])
&& class_exists('\Tilmeld\Tilmeld')
&& isset($token)
) {
$user = \Tilmeld\Tilmeld::extractToken($token);
if ($user) {
$current->updateDataProtection($user);
}
if (is_callable([$current, 'updateDataProtection'])) {
$current->updateDataProtection();
}
$curClient->send(json_encode([
'query' => $curData['query'],
Expand All @@ -457,6 +468,11 @@ private function handlePublishEntity(ConnectionInterface $from, $data) {
]));
}

if (class_exists('\Tilmeld\Tilmeld') && isset($token)) {
// Clear the user that was temporarily logged in.
\Tilmeld\Tilmeld::clearSession();
}

$updatedClients->attach($curClient);
}
}
Expand Down Expand Up @@ -485,52 +501,57 @@ private function handlePublishEntity(ConnectionInterface $from, $data) {
// It does match the query.
foreach ($curClients as $curClient) {
if ($updatedClients->contains($curClient)) {
// The user was already notified. (Of an update.)
continue;
}

// Check that the user can access the entity.
$queryArgs = unserialize($curQuery);
$this->prepareSelectors($queryArgs);
$queryArgs[0]['source'] = 'pubsub';
$queryArgs[0]['source'] = 'client';
$queryArgs[] = ['&', 'guid' => $data['guid']];
if ($this->sessions->contains($curClient)) {
$token = $this->sessions[$curClient];
} else {
$token = null;
}
$queryArgs[0]['token'] = $token;
$queryArgs[] = ['&', 'guid' => $data['guid']];
if (class_exists('\Tilmeld\Tilmeld') && isset($token)) {
$user = \Tilmeld\Tilmeld::extractToken($token);
if ($user) {
// Log in the user for access controls.
\Tilmeld\Tilmeld::fillSession($user);
}
}
$current = call_user_func_array(
"\Nymph\Nymph::getEntity",
$queryArgs
);
if (!isset($current)) {
continue;
}

// Update the currents list.
$curData = $curClients[$curClient];
$curData['current'][] = $data['guid'];
$curClients->attach($curClient, $curData);
if (isset($current)) {
// Update the currents list.
$curData = $curClients[$curClient];
$curData['current'][] = $data['guid'];
$curClients->attach($curClient, $curData);

// Notify client.
$this->logger->notice(
"Notifying client of new match! ".
"({$curClient->resourceId})"
);
if (is_callable([$current, 'updateDataProtection'])
&& class_exists('\Tilmeld\Tilmeld')
&& isset($token)
) {
$user = \Tilmeld\Tilmeld::extractToken($token);
if ($user) {
$current->updateDataProtection($user);
// Notify client.
$this->logger->notice(
"Notifying client of new match! ".
"({$curClient->resourceId})"
);
if (is_callable([$current, 'updateDataProtection'])) {
$current->updateDataProtection();
}
$curClient->send(json_encode([
'query' => $curData['query'],
'added' => $data['guid'],
'data' => $current
]));
}

if (class_exists('\Tilmeld\Tilmeld') && isset($token)) {
// Clear the user that was temporarily logged in.
\Tilmeld\Tilmeld::clearSession();
}
$curClient->send(json_encode([
'query' => $curData['query'],
'added' => $data['guid'],
'data' => $current
]));
}
}
}
Expand Down

0 comments on commit 9608fd5

Please sign in to comment.