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
|
|
|
/** |
11
|
|
|
* Class PusherChannelVoter |
12
|
|
|
* |
13
|
|
|
* @package IrishDan\NotificationBundle\Security |
14
|
|
|
*/ |
15
|
|
|
class PusherChannelVoter extends Voter |
16
|
|
|
{ |
17
|
|
|
const SUBSCRIBE = 'subscribe'; |
18
|
|
|
private $pusherEnabled = false; |
19
|
|
|
private $pusherConfiguration; |
20
|
|
|
|
21
|
|
|
public function __construct($pusherEnabled = false, $pusherConfiguration = []) |
22
|
|
|
{ |
23
|
|
|
$this->pusherEnabled = $pusherEnabled; |
24
|
|
|
$this->pusherConfiguration = $pusherConfiguration; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function supports($attribute, $subject) |
28
|
|
|
{ |
29
|
|
|
if (!$subject instanceof PusherChannel) { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return true; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
37
|
|
|
{ |
38
|
|
|
if (!$this->pusherEnabled) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (empty($this->pusherConfiguration['channel_name'])) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$user = $token->getUser(); |
47
|
|
|
if (!$user instanceof PusherableInterface) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (!$user->isSubscribedToChannel('pusher')) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
switch ($attribute) { |
56
|
|
|
case self::SUBSCRIBE: |
57
|
|
|
// Compare the channel names to determine if this is the current user's channel. |
58
|
|
|
$channelPrefix = $this->pusherConfiguration['channel_name']; |
59
|
|
|
$channelSuffix = $user->getIdentifier(); |
60
|
|
|
|
61
|
|
|
if ($channelPrefix . $channelSuffix === $subject->getChannelName()) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
} |