|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace IrishDan\NotificationBundle\Security; |
|
4
|
|
|
|
|
5
|
|
|
use IrishDan\NotificationBundle\PusherableInterface; |
|
6
|
|
|
use IrishDan\NotificationBundle\PusherChannel; |
|
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
8
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
|
9
|
|
|
|
|
10
|
|
|
class PusherChannelVoter extends Voter |
|
11
|
|
|
{ |
|
12
|
|
|
const SUBSCRIBE = 'subscribe'; |
|
13
|
|
|
private $pusherEnabled = false; |
|
14
|
|
|
private $pusherConfiguration; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct($pusherEnabled = false, $pusherConfiguration = []) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->pusherEnabled = $pusherEnabled; |
|
19
|
|
|
$this->pusherConfiguration = $pusherConfiguration; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
protected function supports($attribute, $subject) |
|
23
|
|
|
{ |
|
24
|
|
|
if (!$subject instanceof PusherChannel) { |
|
25
|
|
|
return false; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
return true; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
|
32
|
|
|
{ |
|
33
|
|
|
if (!$this->pusherEnabled) { |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if (empty($this->pusherConfiguration['channel_name'])) { |
|
38
|
|
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$user = $token->getUser(); |
|
42
|
|
|
if (!$user instanceof PusherableInterface) { |
|
43
|
|
|
return false; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Check if user is subscribed to pusher channel. |
|
47
|
|
|
if (!$user->isSubscribedToChannel('pusher')) { |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
switch ($attribute) { |
|
52
|
|
|
case self::SUBSCRIBE: |
|
53
|
|
|
// Compare the channel names to determine if this is the current user's channel. |
|
54
|
|
|
$channelPrefix = $this->pusherConfiguration['channel_name']; |
|
55
|
|
|
$channelSuffix = $user->getId(); // @TODO: use the interface method instead |
|
56
|
|
|
|
|
57
|
|
|
if ($channelPrefix . $channelSuffix === $subject->getChannelName()) { |
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
break; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
} |