1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Botonomous; |
4
|
|
|
|
5
|
|
|
use Botonomous\client\ApiClient; |
6
|
|
|
use Botonomous\utility\ClassUtility; |
7
|
|
|
|
8
|
|
|
abstract class AbstractAccessList |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Dependencies. |
12
|
|
|
*/ |
13
|
|
|
private $request; |
14
|
|
|
private $dictionary; |
15
|
|
|
private $apiClient; |
16
|
|
|
private $classUtility; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @throws \Exception |
20
|
|
|
* |
21
|
|
|
* @return mixed |
22
|
|
|
*/ |
23
|
18 |
|
protected function getAccessControlList() |
24
|
|
|
{ |
25
|
18 |
|
return $this->getDictionary()->get('access-control'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $sublistKey |
30
|
|
|
* |
31
|
|
|
* @throws \Exception |
32
|
|
|
* |
33
|
|
|
* @return mixed |
34
|
|
|
*/ |
35
|
18 |
|
protected function getSubAccessControlList($sublistKey) |
36
|
|
|
{ |
37
|
18 |
|
$list = $this->getAccessControlList(); |
38
|
|
|
|
39
|
18 |
|
if (!isset($list[$sublistKey])) { |
40
|
|
|
/* @noinspection PhpInconsistentReturnPointsInspection */ |
41
|
1 |
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
18 |
|
return $list[$sublistKey]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $list |
49
|
|
|
* |
50
|
|
|
* @throws \Exception |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
9 |
|
protected function isEmailInList(array $list): bool |
55
|
|
|
{ |
56
|
|
|
// get user info |
57
|
9 |
|
$userInfo = $this->getSlackUserInfo(); |
58
|
|
|
|
59
|
9 |
|
return !empty($userInfo) && in_array($userInfo['profile']['email'], $list['userEmail']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check if email is white listed or black listed |
64
|
|
|
* If userEmail list is not set, return true for whitelist and false for blacklist. |
65
|
|
|
* |
66
|
|
|
* @throws \Exception |
67
|
|
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
12 |
|
protected function checkEmail(): bool |
71
|
|
|
{ |
72
|
|
|
// load the relevant list based on the class name e.g. BlackList or WhiteList |
73
|
12 |
|
$list = $this->getSubAccessControlList($this->getShortClassName()); |
74
|
|
|
|
75
|
12 |
|
if (!isset($list['userEmail'])) { |
76
|
|
|
// if list is not set do not check it |
77
|
3 |
|
return $this->getShortClassName() === 'whitelist' ? true : false; |
78
|
|
|
} |
79
|
|
|
|
80
|
9 |
|
return $this->isEmailInList($list); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $requestKey |
85
|
|
|
* @param string $listKey |
86
|
|
|
* @param string $subListKey |
87
|
|
|
* |
88
|
|
|
* @throws \Exception |
89
|
|
|
* |
90
|
|
|
* @return bool|null |
91
|
|
|
*/ |
92
|
12 |
|
protected function findInListByRequestKey(string $requestKey, string $listKey, string $subListKey) |
93
|
|
|
{ |
94
|
|
|
/** |
95
|
|
|
* load the relevant list to start checking |
96
|
|
|
* The list name is the called class name e.g. WhiteList in lowercase. |
97
|
|
|
*/ |
98
|
12 |
|
$list = $this->getSubAccessControlList($listKey); |
99
|
|
|
|
100
|
|
|
// currently if list key is not set we do not check it |
101
|
12 |
|
if ($list === null || !isset($list[$subListKey])) { |
102
|
|
|
/* @noinspection PhpInconsistentReturnPointsInspection */ |
103
|
8 |
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
11 |
|
return in_array($this->getRequest()[$requestKey], $list[$subListKey]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return mixed |
111
|
|
|
*/ |
112
|
18 |
|
protected function getShortClassName() |
113
|
|
|
{ |
114
|
18 |
|
return $this->getClassUtility()->extractClassNameFromFullName(strtolower(get_called_class())); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
19 |
|
public function getRequest() |
121
|
|
|
{ |
122
|
19 |
|
return $this->request; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param mixed $request |
127
|
|
|
*/ |
128
|
22 |
|
public function setRequest($request) |
129
|
|
|
{ |
130
|
22 |
|
$this->request = $request; |
131
|
22 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return Dictionary |
135
|
|
|
*/ |
136
|
18 |
|
public function getDictionary(): Dictionary |
137
|
|
|
{ |
138
|
18 |
|
if (!isset($this->dictionary)) { |
139
|
5 |
|
$this->setDictionary(new Dictionary()); |
140
|
|
|
} |
141
|
|
|
|
142
|
18 |
|
return $this->dictionary; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param Dictionary $dictionary |
147
|
|
|
*/ |
148
|
18 |
|
public function setDictionary(Dictionary $dictionary) |
149
|
|
|
{ |
150
|
18 |
|
$this->dictionary = $dictionary; |
151
|
18 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return ApiClient |
155
|
|
|
*/ |
156
|
11 |
|
public function getApiClient(): ApiClient |
157
|
|
|
{ |
158
|
11 |
|
if (!isset($this->apiClient)) { |
159
|
5 |
|
$this->setApiClient(new ApiClient()); |
160
|
|
|
} |
161
|
|
|
|
162
|
11 |
|
return $this->apiClient; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* @param ApiClient $apiClient |
167
|
|
|
*/ |
168
|
14 |
|
public function setApiClient(ApiClient $apiClient) |
169
|
|
|
{ |
170
|
14 |
|
$this->apiClient = $apiClient; |
171
|
14 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @throws \Exception |
175
|
|
|
* |
176
|
|
|
* @return array|bool |
177
|
|
|
*/ |
178
|
11 |
|
public function getSlackUserInfo() |
179
|
|
|
{ |
180
|
|
|
// get user id in the request |
181
|
11 |
|
$request = $this->getRequest(); |
182
|
|
|
|
183
|
|
|
// currently if user_id is not set we do not check it |
184
|
11 |
|
if (!isset($request['user_id'])) { |
185
|
1 |
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* email normally does not exist in the request. |
190
|
|
|
* Get it by user_id. For this users:read and users:read.email are needed. |
191
|
|
|
*/ |
192
|
10 |
|
$userInfo = $this->getApiClient()->userInfo(['user' => $request['user_id']]); |
193
|
10 |
|
if (empty($userInfo)) { |
194
|
|
|
/* |
195
|
|
|
* Could not find the user in the team |
196
|
|
|
* Probably there might be some issue with Access token and reading user info but block the access |
197
|
|
|
*/ |
198
|
4 |
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
6 |
|
return $userInfo; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return ClassUtility |
206
|
|
|
*/ |
207
|
18 |
|
public function getClassUtility(): ClassUtility |
208
|
|
|
{ |
209
|
18 |
|
if (!isset($this->classUtility)) { |
210
|
18 |
|
$this->setClassUtility(new ClassUtility()); |
211
|
|
|
} |
212
|
|
|
|
213
|
18 |
|
return $this->classUtility; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param ClassUtility $classUtility |
218
|
|
|
*/ |
219
|
18 |
|
public function setClassUtility(ClassUtility $classUtility) |
220
|
|
|
{ |
221
|
18 |
|
$this->classUtility = $classUtility; |
222
|
18 |
|
} |
223
|
|
|
} |
224
|
|
|
|