Skip to content

Commit

Permalink
Merge pull request #21 from ably/fix/capability-validation
Browse files Browse the repository at this point in the history
Refactor capability key for a channel
  • Loading branch information
sacOO7 authored Dec 6, 2022
2 parents 91c7a41 + bc98ad4 commit cec4791
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,20 @@ npm run dev
## Configure advanced features

**1. Modify private/presence channel capability. Default: Full capability**
- Channel access can be changed as per [Channel Capabilities](https://ably.com/docs/core-features/authentication#capability-operations)
- Channel access control rights are granted for each individual user separately using `ably-capability`. It defines list of access claims as per [Channel Capabilities](https://ably.com/docs/core-features/authentication#capability-operations).

```php
// file - routes/channels.php

// for private channel (Access is allowed for truthy values and denied for falsy values)
// User authentication is allowed for private/presence channel returning truthy values and denied for falsy values.

// for private channel
Broadcast::channel('channel1', function ($user) {
return ['capability' => ["subscribe", "history"]];
return ['ably-capability' => ["subscribe", "history"]];
});

// for presence channel
Broadcast::channel('channel2', function ($user) {
return ['id' => $user->id, 'name' => $user->name, 'capability' => ["subscribe", "presence"]];
return ['id' => $user->id, 'name' => $user->name, 'ably-capability' => ["subscribe", "presence"]];
});
```

Expand Down
5 changes: 4 additions & 1 deletion src/AblyBroadcaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ public function auth($request)
}
try {
$userChannelMetaData = parent::verifyUserCanAccessChannel($request, $normalizedChannelName);
if (is_array($userChannelMetaData) && array_key_exists('capability', $userChannelMetaData)) {
if (is_array($userChannelMetaData) && array_key_exists('ably-capability', $userChannelMetaData)) {
$guardedChannelCapability = $userChannelMetaData['ably-capability'];
unset($userChannelMetaData['ably-capability']);
} else if (is_array($userChannelMetaData) && array_key_exists('capability', $userChannelMetaData)) { // deprecated, will be removed in future versions
$guardedChannelCapability = $userChannelMetaData['capability'];
unset($userChannelMetaData['capability']);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/AblyBroadcasterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ public function testCustomChannelCapability()

$this->broadcaster->shouldReceive('validAuthenticationResponse')
->times(1)
->andReturn(['userid' => 'user1234', 'info' => 'Hello there', 'capability' => ['publish', 'subscribe', 'presence']]);
->andReturn(['userid' => 'user1234', 'info' => 'Hello there', 'ably-capability' => ['publish', 'subscribe', 'presence']]);

$response = $this->broadcaster->auth(
$this->getMockRequestWithUserForChannel('private:test1', null)
Expand Down

0 comments on commit cec4791

Please sign in to comment.