1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This Driver is based entirely on official documentation of the Mattermost Web
|
4
|
|
|
* Services API and you can extend it by following the directives of the documentation.
|
5
|
|
|
*
|
6
|
|
|
* God bless this mess.
|
7
|
|
|
*
|
8
|
|
|
* @author Luca Agnello <[email protected]>
|
9
|
|
|
* @link https://api.mattermost.com/
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
namespace Gnello\Mattermost;
|
13
|
|
|
|
14
|
|
|
use Gnello\Mattermost\Models\BotModel;
|
15
|
|
|
use Gnello\Mattermost\Models\BrandModel;
|
16
|
|
|
use Gnello\Mattermost\Models\ChannelModel;
|
17
|
|
|
use Gnello\Mattermost\Models\ClusterModel;
|
18
|
|
|
use Gnello\Mattermost\Models\CommandModel;
|
19
|
|
|
use Gnello\Mattermost\Models\ComplianceModel;
|
20
|
|
|
use Gnello\Mattermost\Models\DataRetentionModel;
|
21
|
|
|
use Gnello\Mattermost\Models\ElasticsearchModel;
|
22
|
|
|
use Gnello\Mattermost\Models\EmojiModel;
|
23
|
|
|
use Gnello\Mattermost\Models\FileModel;
|
24
|
|
|
use Gnello\Mattermost\Models\GroupModel;
|
25
|
|
|
use Gnello\Mattermost\Models\IntegrationActionsModel;
|
26
|
|
|
use Gnello\Mattermost\Models\JobModel;
|
27
|
|
|
use Gnello\Mattermost\Models\LDAPModel;
|
28
|
|
|
use Gnello\Mattermost\Models\OAuthModel;
|
29
|
|
|
use Gnello\Mattermost\Models\PluginModel;
|
30
|
|
|
use Gnello\Mattermost\Models\PostModel;
|
31
|
|
|
use Gnello\Mattermost\Models\PreferenceModel;
|
32
|
|
|
use Gnello\Mattermost\Models\ReactionModel;
|
33
|
|
|
use Gnello\Mattermost\Models\RoleModel;
|
34
|
|
|
use Gnello\Mattermost\Models\SAMLModel;
|
35
|
|
|
use Gnello\Mattermost\Models\SchemeModel;
|
36
|
|
|
use Gnello\Mattermost\Models\SystemModel;
|
37
|
|
|
use Gnello\Mattermost\Models\TeamModel;
|
38
|
|
|
use Gnello\Mattermost\Models\UserModel;
|
39
|
|
|
use Gnello\Mattermost\Models\WebhookModel;
|
40
|
|
|
use GuzzleHttp\Psr7\Response;
|
41
|
|
|
use Pimple\Container;
|
42
|
|
|
use Psr\Http\Message\ResponseInterface;
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* Class Driver
|
46
|
|
|
*
|
47
|
|
|
* @package Gnello\Mattermost
|
48
|
|
|
*/
|
49
|
|
|
class Driver
|
50
|
|
|
{
|
51
|
|
|
/**
|
52
|
|
|
* Default options of the Driver
|
53
|
|
|
*
|
54
|
|
|
* @var array
|
55
|
|
|
*/
|
56
|
|
|
private $defaultOptions = [
|
57
|
|
|
'scheme' => 'https',
|
58
|
|
|
'basePath' => '/api/v4',
|
59
|
|
|
'url' => 'localhost',
|
60
|
|
|
'login_id' => null,
|
61
|
|
|
'password' => null,
|
62
|
|
|
'token' => null,
|
63
|
|
|
];
|
64
|
|
|
|
65
|
|
|
/**
|
66
|
|
|
* @var Container
|
67
|
|
|
*/
|
68
|
|
|
private $container;
|
69
|
|
|
|
70
|
|
|
/**
|
71
|
|
|
* @var array
|
72
|
|
|
*/
|
73
|
|
|
private $models = [];
|
74
|
|
|
|
75
|
|
|
/**
|
76
|
|
|
* Driver constructor.
|
77
|
|
|
*
|
78
|
|
|
* @param Container $container
|
79
|
|
|
*/
|
80
|
|
|
public function __construct(Container $container)
|
81
|
|
|
{
|
82
|
|
|
$driverOptions = $this->defaultOptions;
|
83
|
|
|
|
84
|
|
|
if (isset($container['driver'])) {
|
85
|
|
|
$driverOptions = array_merge($driverOptions, $container['driver']);
|
86
|
|
|
}
|
87
|
|
|
|
88
|
|
|
$container['driver'] = $driverOptions;
|
89
|
|
|
$container['client'] = new Client($container);
|
90
|
|
|
|
91
|
|
|
$this->container = $container;
|
92
|
|
|
}
|
93
|
|
|
|
94
|
|
|
/**
|
95
|
|
|
* @return ResponseInterface
|
96
|
|
|
*/
|
97
|
|
|
public function authenticate()
|
98
|
|
|
{
|
99
|
|
|
$driverOptions = $this->container['driver'];
|
100
|
|
|
|
101
|
|
|
if (isset($driverOptions['token'])) {
|
102
|
|
|
|
103
|
|
|
$this->container['client']->setToken($driverOptions['token']);
|
104
|
|
|
$response = $this->getUserModel()->getAuthenticatedUser();
|
105
|
|
|
|
106
|
|
|
} else if (isset($driverOptions['login_id']) && isset($driverOptions['password'])) {
|
107
|
|
|
|
108
|
|
|
$response = $this->getUserModel()->loginToUserAccount([
|
109
|
|
|
'login_id' => $driverOptions['login_id'],
|
110
|
|
|
'password' => $driverOptions['password']
|
111
|
|
|
]);
|
112
|
|
|
|
113
|
|
|
if ($response->getStatusCode() == 200) {
|
114
|
|
|
$token = $response->getHeader('Token')[0];
|
115
|
|
|
$this->container['client']->setToken($token);
|
116
|
|
|
}
|
117
|
|
|
|
118
|
|
|
} else {
|
119
|
|
|
|
120
|
|
|
$response = new Response(401, [], json_encode([
|
121
|
|
|
"id" => "missing.credentials.",
|
122
|
|
|
"message" => "You must provide a login_id and password or a valid token.",
|
123
|
|
|
"detailed_error" => "",
|
124
|
|
|
"request_id" => "",
|
125
|
|
|
"status_code" => 401,
|
126
|
|
|
]));
|
127
|
|
|
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
return $response;
|
131
|
|
|
}
|
132
|
|
|
|
133
|
|
|
/**
|
134
|
|
|
* @param $className
|
135
|
|
|
* @return mixed
|
136
|
|
|
*/
|
137
|
|
|
private function getModel($className)
|
138
|
|
|
{
|
139
|
|
|
if (!isset($this->models[$className])) {
|
140
|
|
|
$this->models[$className] = new $className($this->container['client']);
|
141
|
|
|
}
|
142
|
|
|
|
143
|
|
|
return $this->models[$className];
|
144
|
|
|
}
|
145
|
|
|
|
146
|
|
|
/**
|
147
|
|
|
* @return UserModel
|
148
|
|
|
*/
|
149
|
|
|
public function getUserModel()
|
150
|
|
|
{
|
151
|
|
|
return $this->getModel(UserModel::class);
|
152
|
|
|
}
|
153
|
|
|
|
154
|
|
|
/**
|
155
|
|
|
* @return TeamModel
|
156
|
|
|
*/
|
157
|
|
|
public function getTeamModel()
|
158
|
|
|
{
|
159
|
|
|
return $this->getModel(TeamModel::class);
|
160
|
|
|
}
|
161
|
|
|
|
162
|
|
|
/**
|
163
|
|
|
* @return ChannelModel
|
164
|
|
|
*/
|
165
|
|
|
public function getChannelModel()
|
166
|
|
|
{
|
167
|
|
|
return $this->getModel(ChannelModel::class);
|
168
|
|
|
}
|
169
|
|
|
|
170
|
|
|
/**
|
171
|
|
|
* @return PostModel
|
172
|
|
|
*/
|
173
|
|
|
public function getPostModel()
|
174
|
|
|
{
|
175
|
|
|
return $this->getModel(PostModel::class);
|
176
|
|
|
}
|
177
|
|
|
|
178
|
|
|
/**
|
179
|
|
|
* @return FileModel
|
180
|
|
|
*/
|
181
|
|
|
public function getFileModel()
|
182
|
|
|
{
|
183
|
|
|
return $this->getModel(FileModel::class);
|
184
|
|
|
}
|
185
|
|
|
|
186
|
|
|
/**
|
187
|
|
|
* @param $userId
|
188
|
|
|
* @return PreferenceModel
|
189
|
|
|
*/
|
190
|
|
|
public function getPreferenceModel($userId)
|
191
|
|
|
{
|
192
|
|
|
if (!isset($this->models[PreferenceModel::class])) {
|
193
|
|
|
$this->models[PreferenceModel::class] = new PreferenceModel($this->container['client'], $userId);
|
194
|
|
|
}
|
195
|
|
|
|
196
|
|
|
return $this->models[PreferenceModel::class];
|
197
|
|
|
}
|
198
|
|
|
|
199
|
|
|
/**
|
200
|
|
|
* @return WebhookModel
|
201
|
|
|
*/
|
202
|
|
|
public function getWebhookModel()
|
203
|
|
|
{
|
204
|
|
|
return $this->getModel(WebhookModel::class);
|
205
|
|
|
}
|
206
|
|
|
|
207
|
|
|
/**
|
208
|
|
|
* @return SystemModel
|
209
|
|
|
*/
|
210
|
|
|
public function getSystemModel()
|
211
|
|
|
{
|
212
|
|
|
return $this->getModel(SystemModel::class);
|
213
|
|
|
}
|
214
|
|
|
|
215
|
|
|
/**
|
216
|
|
|
* @return ComplianceModel
|
217
|
|
|
*/
|
218
|
|
|
public function getComplianceModel()
|
219
|
|
|
{
|
220
|
|
|
return $this->getModel(ComplianceModel::class);
|
221
|
|
|
}
|
222
|
|
|
|
223
|
|
|
/**
|
224
|
|
|
* @return CommandModel
|
225
|
|
|
*/
|
226
|
|
|
public function getCommandModel()
|
227
|
|
|
{
|
228
|
|
|
return $this->getModel(CommandModel::class);
|
229
|
|
|
}
|
230
|
|
|
|
231
|
|
|
/**
|
232
|
|
|
* @return ClusterModel
|
233
|
|
|
*/
|
234
|
|
|
public function getClusterModel()
|
235
|
|
|
{
|
236
|
|
|
return $this->getModel(ClusterModel::class);
|
237
|
|
|
}
|
238
|
|
|
|
239
|
|
|
/**
|
240
|
|
|
* @return BrandModel
|
241
|
|
|
*/
|
242
|
|
|
public function getBrandModel()
|
243
|
|
|
{
|
244
|
|
|
return $this->getModel(BrandModel::class);
|
245
|
|
|
}
|
246
|
|
|
|
247
|
|
|
/**
|
248
|
|
|
* @return LDAPModel
|
249
|
|
|
*/
|
250
|
|
|
public function getLDAPModel()
|
251
|
|
|
{
|
252
|
|
|
return $this->getModel(LDAPModel::class);
|
253
|
|
|
}
|
254
|
|
|
|
255
|
|
|
/**
|
256
|
|
|
* @return OAuthModel
|
257
|
|
|
*/
|
258
|
|
|
public function getOAuthModel()
|
259
|
|
|
{
|
260
|
|
|
return $this->getModel(OAuthModel::class);
|
261
|
|
|
}
|
262
|
|
|
|
263
|
|
|
/**
|
264
|
|
|
* @return SAMLModel
|
265
|
|
|
*/
|
266
|
|
|
public function getSAMLModel()
|
267
|
|
|
{
|
268
|
|
|
return $this->getModel(SAMLModel::class);
|
269
|
|
|
}
|
270
|
|
|
|
271
|
|
|
/**
|
272
|
|
|
* @return ElasticsearchModel
|
273
|
|
|
*/
|
274
|
|
|
public function getElasticsearchModel()
|
275
|
|
|
{
|
276
|
|
|
return $this->getModel(ElasticsearchModel::class);
|
277
|
|
|
}
|
278
|
|
|
|
279
|
|
|
/**
|
280
|
|
|
* @return EmojiModel
|
281
|
|
|
*/
|
282
|
|
|
public function getEmojiModel()
|
283
|
|
|
{
|
284
|
|
|
return $this->getModel(EmojiModel::class);
|
285
|
|
|
}
|
286
|
|
|
|
287
|
|
|
/**
|
288
|
|
|
* @return ReactionModel
|
289
|
|
|
*/
|
290
|
|
|
public function getReactionModel()
|
291
|
|
|
{
|
292
|
|
|
return $this->getModel(ReactionModel::class);
|
293
|
|
|
}
|
294
|
|
|
|
295
|
|
|
/**
|
296
|
|
|
* @return DataRetentionModel
|
297
|
|
|
*/
|
298
|
|
|
public function getDataRetentionModel()
|
299
|
|
|
{
|
300
|
|
|
return $this->getModel(DataRetentionModel::class);
|
301
|
|
|
}
|
302
|
|
|
|
303
|
|
|
/**
|
304
|
|
|
* @return JobModel
|
305
|
|
|
*/
|
306
|
|
|
public function getJobModel()
|
307
|
|
|
{
|
308
|
|
|
return $this->getModel(JobModel::class);
|
309
|
|
|
}
|
310
|
|
|
|
311
|
|
|
/**
|
312
|
|
|
* @return PluginModel
|
313
|
|
|
*/
|
314
|
|
|
public function getPluginModel()
|
315
|
|
|
{
|
316
|
|
|
return $this->getModel(PluginModel::class);
|
317
|
|
|
}
|
318
|
|
|
|
319
|
|
|
/**
|
320
|
|
|
* @return RoleModel
|
321
|
|
|
*/
|
322
|
|
|
public function getRoleModel()
|
323
|
|
|
{
|
324
|
|
|
return $this->getModel(RoleModel::class);
|
325
|
|
|
}
|
326
|
|
|
|
327
|
|
|
/**
|
328
|
|
|
* @return SchemeModel
|
329
|
|
|
*/
|
330
|
|
|
public function getSchemeModel()
|
331
|
|
|
{
|
332
|
|
|
return $this->getModel(SchemeModel::class);
|
333
|
|
|
}
|
334
|
|
|
|
335
|
|
|
/**
|
336
|
|
|
* @return BotModel
|
337
|
|
|
*/
|
338
|
|
|
public function getBotModel()
|
339
|
|
|
{
|
340
|
|
|
return $this->getModel(BotModel::class);
|
341
|
|
|
}
|
342
|
|
|
|
343
|
|
|
/**
|
344
|
|
|
* @return GroupModel
|
345
|
|
|
*/
|
346
|
|
|
public function getGroupModel()
|
347
|
|
|
{
|
348
|
|
|
return $this->getModel(GroupModel::class);
|
349
|
|
|
}
|
350
|
|
|
|
351
|
|
|
/**
|
352
|
|
|
* @return IntegrationActionsModel
|
353
|
|
|
*/
|
354
|
|
|
public function getIntegrationActionsModel()
|
355
|
|
|
{
|
356
|
|
|
return $this->getModel(IntegrationActionsModel::class);
|
357
|
|
|
}
|
358
|
|
|
}
|
359
|
|
|
|