1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* @copyright 2014 Mautic Contributors. All rights reserved |
5
|
|
|
* @author Mautic |
6
|
|
|
* |
7
|
|
|
* @link http://mautic.org |
8
|
|
|
* |
9
|
|
|
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Mautic\ApiBundle\Model; |
13
|
|
|
|
14
|
|
|
use Mautic\ApiBundle\ApiEvents; |
15
|
|
|
use Mautic\ApiBundle\Entity\oAuth1\Consumer; |
16
|
|
|
use Mautic\ApiBundle\Entity\oAuth2\Client; |
17
|
|
|
use Mautic\ApiBundle\Event\ClientEvent; |
18
|
|
|
use Mautic\ApiBundle\Form\Type\ClientType; |
19
|
|
|
use Mautic\CoreBundle\Model\FormModel; |
20
|
|
|
use Mautic\UserBundle\Entity\User; |
21
|
|
|
use Symfony\Component\EventDispatcher\Event; |
22
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
23
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
24
|
|
|
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; |
25
|
|
|
|
26
|
|
|
class ClientModel extends FormModel |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $apiMode = 'oauth1a'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Session |
35
|
|
|
*/ |
36
|
|
|
protected $session; |
37
|
|
|
|
38
|
|
|
public function __construct(RequestStack $requestStack) |
39
|
|
|
{ |
40
|
|
|
$request = $requestStack->getCurrentRequest(); |
41
|
|
|
|
42
|
|
|
if ($request) { |
43
|
|
|
$this->apiMode = $request->get('api_mode', $request->getSession()->get('mautic.client.filter.api_mode', 'oauth1a')); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param $apiMode |
49
|
|
|
*/ |
50
|
|
|
public function setApiMode($apiMode) |
51
|
|
|
{ |
52
|
|
|
$this->apiMode = $apiMode; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setSession(Session $session) |
56
|
|
|
{ |
57
|
|
|
$this->session = $session; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
* |
63
|
|
|
* @return \Mautic\ApiBundle\Entity\oAuth1\ConsumerRepository|\Mautic\ApiBundle\Entity\oAuth2\ClientRepository |
64
|
|
|
*/ |
65
|
|
|
public function getRepository() |
66
|
|
|
{ |
67
|
|
|
if ('oauth2' == $this->apiMode) { |
68
|
|
|
return $this->em->getRepository(Client::class); |
69
|
|
|
} else { |
70
|
|
|
return $this->em->getRepository(Consumer::class); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function getPermissionBase() |
78
|
|
|
{ |
79
|
|
|
return 'api:clients'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
* |
85
|
|
|
* @throws MethodNotAllowedHttpException |
86
|
|
|
*/ |
87
|
|
|
public function createForm($entity, $formFactory, $action = null, $options = []) |
88
|
|
|
{ |
89
|
|
|
if (!$entity instanceof Client && !$entity instanceof Consumer) { |
90
|
|
|
throw new MethodNotAllowedHttpException(['Client', 'Consumer']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$params = (!empty($action)) ? ['action' => $action] : []; |
94
|
|
|
|
95
|
|
|
return $formFactory->create(ClientType::class, $entity, $params); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
* |
101
|
|
|
* @return Client|Consumer|null |
102
|
|
|
*/ |
103
|
|
|
public function getEntity($id = null) |
104
|
|
|
{ |
105
|
|
|
if (null === $id) { |
106
|
|
|
return 'oauth2' == $this->apiMode ? new Client() : new Consumer(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return parent::getEntity($id); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
* |
115
|
|
|
* @throws MethodNotAllowedHttpException |
116
|
|
|
*/ |
117
|
|
|
protected function dispatchEvent($action, &$entity, $isNew = false, Event $event = null) |
118
|
|
|
{ |
119
|
|
|
if (!$entity instanceof Client && !$entity instanceof Consumer) { |
120
|
|
|
throw new MethodNotAllowedHttpException(['Client', 'Consumer']); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
switch ($action) { |
124
|
|
|
case 'post_save': |
125
|
|
|
$name = ApiEvents::CLIENT_POST_SAVE; |
126
|
|
|
break; |
127
|
|
|
case 'post_delete': |
128
|
|
|
$name = ApiEvents::CLIENT_POST_DELETE; |
129
|
|
|
break; |
130
|
|
|
default: |
131
|
|
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($this->dispatcher->hasListeners($name)) { |
135
|
|
|
if (empty($event)) { |
136
|
|
|
$event = new ClientEvent($entity, $isNew); |
137
|
|
|
$event->setEntityManager($this->em); |
138
|
|
|
} |
139
|
|
|
$this->dispatcher->dispatch($name, $event); |
140
|
|
|
|
141
|
|
|
return $event; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return null; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
|
|
public function getUserClients(User $user) |
151
|
|
|
{ |
152
|
|
|
return $this->getRepository()->getUserClients($user); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param $entity |
157
|
|
|
* |
158
|
|
|
* @throws MethodNotAllowedHttpException |
159
|
|
|
*/ |
160
|
|
|
public function revokeAccess($entity) |
161
|
|
|
{ |
162
|
|
|
if (!$entity instanceof Client && !$entity instanceof Consumer) { |
163
|
|
|
throw new MethodNotAllowedHttpException(['Client', 'Consumer']); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
//remove the user from the client |
167
|
|
|
if ('oauth2' == $this->apiMode) { |
168
|
|
|
$entity->removeUser($this->userHelper->getUser()); |
|
|
|
|
169
|
|
|
$this->saveEntity($entity); |
170
|
|
|
} else { |
171
|
|
|
$this->getRepository()->deleteAccessTokens($entity, $this->userHelper->getUser()); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.