1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Ratchet. |
5
|
|
|
* |
6
|
|
|
** (c) 2016 Cees-Jan Kiewiet |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace WyriHaximus\Ratchet\Security; |
13
|
|
|
|
14
|
|
|
use Cake\Event\EventManager; |
15
|
|
|
use function React\Promise\reject; |
16
|
|
|
use Thruway\Event\MessageEvent; |
17
|
|
|
use Thruway\Event\NewRealmEvent; |
18
|
|
|
use Thruway\Message\ErrorMessage; |
19
|
|
|
use Thruway\Module\RealmModuleInterface; |
20
|
|
|
use Thruway\Module\RouterModuleClient; |
21
|
|
|
use WyriHaximus\Ratchet\Event\AuthorizeEvent; |
22
|
|
|
|
23
|
|
|
class AuthorizationManager extends RouterModuleClient implements RealmModuleInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var EventManager |
27
|
|
|
*/ |
28
|
|
|
private $eventManager; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param EventManager $eventManager |
32
|
|
|
*/ |
33
|
|
|
public function setEventManager(EventManager $eventManager) |
34
|
|
|
{ |
35
|
|
|
$this->eventManager = $eventManager; |
36
|
|
|
return $this; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Listen for Router events. |
41
|
|
|
* Required to add the authorization module to the realm |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public static function getSubscribedEvents() |
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'new_realm' => ['handleNewRealm', 10] |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param NewRealmEvent $newRealmEvent |
54
|
|
|
*/ |
55
|
|
|
public function handleNewRealm(NewRealmEvent $newRealmEvent) |
56
|
|
|
{ |
57
|
|
|
$realm = $newRealmEvent->realm; |
58
|
|
|
|
59
|
|
|
if ($realm->getRealmName() === $this->getRealm()) { |
60
|
|
|
$realm->addModule($this); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function getSubscribedRealmEvents() |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
|
|
'PublishMessageEvent' => ['authorize', 100], |
71
|
|
|
'SubscribeMessageEvent' => ['authorize', 100], |
72
|
|
|
'RegisterMessageEvent' => ['authorize', 100], |
73
|
|
|
'CallMessageEvent' => ['authorize', 100], |
74
|
|
|
]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param MessageEvent $msg |
|
|
|
|
79
|
|
|
*/ |
80
|
|
|
public function authorize(MessageEvent $messageEvent) |
81
|
|
|
{ |
82
|
|
|
$event = AuthorizeEvent::create($this->getRealm(), $messageEvent->session, $messageEvent->message); |
83
|
|
|
$event->promise()->otherwise(function () use ($messageEvent) { |
|
|
|
|
84
|
|
|
$messageEvent->session->sendMessage(ErrorMessage::createErrorMessageFromMessage($messageEvent->message, "wamp.error.not_authorized")); |
|
|
|
|
85
|
|
|
$messageEvent->stopPropagation(); |
86
|
|
|
}); |
87
|
|
|
$this->eventManager->dispatch($event); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.