1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* tubee.io |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tubee\Rest\v1; |
13
|
|
|
|
14
|
|
|
use Fig\Http\Message\StatusCodeInterface; |
15
|
|
|
use Lcobucci\ContentNegotiation\UnformattedResponse; |
16
|
|
|
use Micro\Auth\Identity; |
17
|
|
|
use Psr\Http\Message\ResponseInterface; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
use Rs\Json\Patch; |
20
|
|
|
use Tubee\AccessRule\Factory as AccessRuleFactory; |
21
|
|
|
use Tubee\Acl; |
22
|
|
|
use Tubee\Rest\Helper; |
23
|
|
|
use Zend\Diactoros\Response; |
24
|
|
|
|
25
|
|
View Code Duplication |
class AccessRules |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* rule factory. |
29
|
|
|
* |
30
|
|
|
* @var AccessRuleFactory |
31
|
|
|
*/ |
32
|
|
|
protected $rule_factory; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Acl. |
36
|
|
|
* |
37
|
|
|
* @var Acl |
38
|
|
|
*/ |
39
|
|
|
protected $acl; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Init. |
43
|
|
|
*/ |
44
|
|
|
public function __construct(AccessRuleFactory $rule_factory, Acl $acl) |
45
|
|
|
{ |
46
|
|
|
$this->rule_factory = $rule_factory; |
47
|
|
|
$this->acl = $acl; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Entrypoint. |
52
|
|
|
*/ |
53
|
|
|
public function getAll(ServerRequestInterface $request, Identity $identity): ResponseInterface |
54
|
|
|
{ |
55
|
|
|
$query = array_merge([ |
56
|
|
|
'offset' => 0, |
57
|
|
|
'limit' => 20, |
58
|
|
|
], $request->getQueryParams()); |
59
|
|
|
|
60
|
|
|
if (isset($query['watch'])) { |
61
|
|
|
$cursor = $this->rule_factory->watch(null, true, $query['query'], $query['offset'], $query['limit'], $query['sort']); |
62
|
|
|
|
63
|
|
|
return Helper::watchAll($request, $identity, $this->acl, $cursor); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$rules = $this->rule_factory->getAll($query['query'], $query['offset'], $query['limit'], $query['sort']); |
67
|
|
|
|
68
|
|
|
return Helper::getAll($request, $identity, $this->acl, $rules); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Entrypoint. |
73
|
|
|
*/ |
74
|
|
|
public function getOne(ServerRequestInterface $request, Identity $identity, string $rule): ResponseInterface |
75
|
|
|
{ |
76
|
|
|
$resource = $this->rule_factory->getOne($rule); |
77
|
|
|
|
78
|
|
|
return Helper::getOne($request, $identity, $resource); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add new access rule. |
83
|
|
|
*/ |
84
|
|
|
public function post(ServerRequestInterface $request, Identity $identity): ResponseInterface |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$body = $request->getParsedBody(); |
87
|
|
|
$query = $request->getQueryParams(); |
88
|
|
|
$id = $this->rule_factory->add($body); |
|
|
|
|
89
|
|
|
$rule = $this->rule_factory->getOne($body['name']); |
90
|
|
|
|
91
|
|
|
return new UnformattedResponse( |
92
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_CREATED), |
93
|
|
|
$rule->decorate($request), |
94
|
|
|
['pretty' => isset($query['pretty'])] |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Create or replace access rule. |
100
|
|
|
*/ |
101
|
|
|
public function put(ServerRequestInterface $request, Identity $identity, string $rule): ResponseInterface |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$body = $request->getParsedBody(); |
104
|
|
|
$query = $request->getQueryParams(); |
105
|
|
|
|
106
|
|
|
if ($this->rule_factory->has($rule)) { |
107
|
|
|
$this->rule_factory->update($rule, $body); |
|
|
|
|
108
|
|
|
$rule = $this->rule_factory->getOne($rule); |
109
|
|
|
|
110
|
|
|
return new UnformattedResponse( |
111
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_OK), |
112
|
|
|
$rule->decorate($request), |
113
|
|
|
['pretty' => isset($query['pretty'])] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$body['name'] = $rule; |
118
|
|
|
$id = $this->rule_factory->add($body); |
|
|
|
|
119
|
|
|
$rule = $this->rule_factory->getOne($body['name']); |
120
|
|
|
|
121
|
|
|
return new UnformattedResponse( |
122
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_CREATED), |
123
|
|
|
$rule->decorate($request), |
124
|
|
|
['pretty' => isset($query['pretty'])] |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Delete access rule. |
130
|
|
|
*/ |
131
|
|
|
public function delete(ServerRequestInterface $request, Identity $identity, string $rule): ResponseInterface |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$body = $request->getParsedBody(); |
|
|
|
|
134
|
|
|
$this->rule_factory->deleteOne($rule); |
135
|
|
|
|
136
|
|
|
return (new Response())->withStatus(StatusCodeInterface::STATUS_NO_CONTENT); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Patch. |
141
|
|
|
*/ |
142
|
|
|
public function patch(ServerRequestInterface $request, Identity $identity, string $rule): ResponseInterface |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$body = $request->getParsedBody(); |
145
|
|
|
$query = $request->getQueryParams(); |
146
|
|
|
$rule = $this->rule_factory->getOne($rule); |
147
|
|
|
$doc = ['data' => $rule->getData()]; |
148
|
|
|
|
149
|
|
|
$patch = new Patch(json_encode($doc), json_encode($body)); |
150
|
|
|
$patched = $patch->apply(); |
151
|
|
|
$update = json_decode($patched, true); |
152
|
|
|
|
153
|
|
|
$this->rule_factory->update($rule, $update); |
154
|
|
|
|
155
|
|
|
return new UnformattedResponse( |
156
|
|
|
(new Response())->withStatus(StatusCodeInterface::STATUS_OK), |
157
|
|
|
$this->rule_factory->getOne($rule->getName())->decorate($request), |
158
|
|
|
['pretty' => isset($query['pretty'])] |
159
|
|
|
); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.