| 1 |
efrain |
1 |
<?php
|
|
|
2 |
declare(strict_types=1);
|
|
|
3 |
|
|
|
4 |
namespace LeadersLinked;
|
|
|
5 |
|
|
|
6 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
7 |
use Laminas\ModuleManager\ModuleEvent;
|
|
|
8 |
use Laminas\ModuleManager\ModuleManager;
|
|
|
9 |
use Laminas\Mvc\MvcEvent;
|
|
|
10 |
use Laminas\Config\Reader\Ini;
|
|
|
11 |
use Laminas\Permissions\Acl\Acl;
|
|
|
12 |
use Laminas\Permissions\Acl\Role\GenericRole;
|
|
|
13 |
use LeadersLinked\Plugin\CurrentUserPlugin;
|
|
|
14 |
use LeadersLinked\Mapper\UserMapper;
|
|
|
15 |
use LeadersLinked\Authentication\AuthTokenAdapter;
|
|
|
16 |
use Laminas\Authentication\AuthenticationService;
|
|
|
17 |
use Laminas\Permissions\Acl\Resource\GenericResource;
|
|
|
18 |
use LeadersLinked\Model\UserType;
|
|
|
19 |
use LeadersLinked\Plugin\CurrentNetworkPlugin;
|
|
|
20 |
use LeadersLinked\Model\Network;
|
|
|
21 |
use LeadersLinked\Model\User;
|
|
|
22 |
use LeadersLinked\Mapper\CompanyUserMapper;
|
|
|
23 |
use LeadersLinked\Model\CompanyUser;
|
|
|
24 |
use LeadersLinked\Mapper\CompanyMapper;
|
|
|
25 |
use LeadersLinked\Mapper\CompanyServiceMapper;
|
|
|
26 |
use LeadersLinked\Model\Service;
|
|
|
27 |
|
|
|
28 |
use LeadersLinked\Library\Functions;
|
|
|
29 |
use LeadersLinked\Mapper\DailyPulseMapper;
|
|
|
30 |
use LeadersLinked\Model\DailyPulse;
|
|
|
31 |
use LeadersLinked\Mapper\OrganizationPositionMapper;
|
|
|
32 |
use LeadersLinked\Mapper\KnowledgeAreaCategoryJobDescriptionMapper;
|
|
|
33 |
use LeadersLinked\Mapper\MyCoachCategoryJobDescriptionMapper;
|
|
|
34 |
use LeadersLinked\Mapper\KnowledgeAreaCategoryUserMapper;
|
|
|
35 |
use LeadersLinked\Mapper\MyCoachCategoryUserMapper;
|
| 23 |
efrain |
36 |
use Firebase\JWT\JWT;
|
|
|
37 |
use Firebase\JWT\Key;
|
|
|
38 |
use LeadersLinked\Mapper\JwtTokenMapper;
|
|
|
39 |
use LeadersLinked\Authentication\AuthUserIdAdapter;
|
| 95 |
efrain |
40 |
use LeadersLinked\Model\JwtToken;
|
| 154 |
efrain |
41 |
use Laminas\Http\Response;
|
| 1 |
efrain |
42 |
|
|
|
43 |
class Module
|
|
|
44 |
{
|
|
|
45 |
/**
|
|
|
46 |
*
|
|
|
47 |
* @var Acl
|
|
|
48 |
*/
|
|
|
49 |
private $acl;
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
*
|
|
|
53 |
* @var AdapterInterface
|
|
|
54 |
*/
|
|
|
55 |
private $adapter;
|
|
|
56 |
|
|
|
57 |
/**
|
|
|
58 |
*
|
|
|
59 |
* @var CurrentUserPlugin
|
|
|
60 |
*/
|
|
|
61 |
private $currentUserPlugin;
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
*
|
|
|
65 |
* @var CurrentNetworkPlugin
|
|
|
66 |
*/
|
|
|
67 |
private $currentNetworkPlugin;
|
|
|
68 |
|
|
|
69 |
/**
|
|
|
70 |
*
|
|
|
71 |
* @var array
|
|
|
72 |
*/
|
|
|
73 |
private $routesAuthorized = [];
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
*
|
|
|
77 |
* @var boolean
|
|
|
78 |
*/
|
| 23 |
efrain |
79 |
private $authByOTP = false;
|
| 1 |
efrain |
80 |
|
| 23 |
efrain |
81 |
/**
|
|
|
82 |
*
|
|
|
83 |
* @var boolean
|
|
|
84 |
*/
|
|
|
85 |
private $authByJWT = false;
|
| 1 |
efrain |
86 |
|
| 102 |
efrain |
87 |
/**
|
|
|
88 |
*
|
|
|
89 |
* @var int
|
|
|
90 |
*/
|
|
|
91 |
private $jwtID = 0;
|
| 23 |
efrain |
92 |
|
| 102 |
efrain |
93 |
|
| 1 |
efrain |
94 |
/**
|
|
|
95 |
*
|
| 95 |
efrain |
96 |
* @var JwtToken
|
|
|
97 |
*/
|
|
|
98 |
private $jwtToken;
|
|
|
99 |
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
*
|
| 1 |
efrain |
103 |
* @var array
|
|
|
104 |
*/
|
|
|
105 |
private $config;
|
|
|
106 |
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
public function init(ModuleManager $moduleManager)
|
|
|
110 |
{
|
| 195 |
efrain |
111 |
|
|
|
112 |
|
| 1 |
efrain |
113 |
$events = $moduleManager->getEventManager();
|
|
|
114 |
$events->attach(ModuleEvent::EVENT_MERGE_CONFIG, array($this, 'onMergeConfig'));
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public function onMergeConfig(ModuleEvent $event)
|
|
|
118 |
{
|
|
|
119 |
$configListener = $event->getConfigListener();
|
|
|
120 |
$this->config = $configListener->getMergedConfig(false);
|
|
|
121 |
|
|
|
122 |
$reader = new Ini();
|
|
|
123 |
$data = $reader->fromFile('config/leaderslinked.ini');
|
|
|
124 |
|
|
|
125 |
$prefix = 'leaderslinked';
|
|
|
126 |
foreach($data as $section => $pairs)
|
|
|
127 |
{
|
|
|
128 |
foreach($pairs as $key => $value)
|
|
|
129 |
{
|
|
|
130 |
$this->config[$prefix . '.' . $section . '.' . $key] = $value;
|
|
|
131 |
}
|
|
|
132 |
}
|
|
|
133 |
$configListener->setMergedConfig($this->config);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
public function getConfig() : array
|
|
|
138 |
{
|
|
|
139 |
return include __DIR__ . '/../config/module.config.php';
|
|
|
140 |
}
|
|
|
141 |
|
|
|
142 |
public function onBootstrap(MvcEvent $event)
|
|
|
143 |
{
|
| 195 |
efrain |
144 |
|
|
|
145 |
|
|
|
146 |
|
| 164 |
efrain |
147 |
header('Access-Control-Allow-Origin: *');
|
|
|
148 |
header('Access-Control-Allow-Headers: *');
|
|
|
149 |
header('Access-Control-Allow-Method: POST, GET, HEAD, OPTIONS');
|
|
|
150 |
header('Access-Control-Max-Age: 86400');
|
|
|
151 |
|
| 86 |
efrain |
152 |
|
| 96 |
efrain |
153 |
$response = $event->getResponse();
|
|
|
154 |
Functions::addCrossSiteToResponse($response);
|
|
|
155 |
$event->setResponse($response);
|
|
|
156 |
|
| 68 |
efrain |
157 |
$serviceManager = $event->getApplication()->getServiceManager();
|
| 55 |
efrain |
158 |
|
|
|
159 |
|
| 68 |
efrain |
160 |
$eventManager = $event->getApplication()->getEventManager();
|
|
|
161 |
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this,'onDispatchError'], 0);
|
|
|
162 |
$eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, [$this,'onRenderError'], 0);
|
|
|
163 |
|
| 1 |
efrain |
164 |
$adapter = $serviceManager->get('leaders-linked-db');
|
|
|
165 |
|
|
|
166 |
$session = $serviceManager->get('leaders-linked-session');
|
|
|
167 |
$session->start();
|
| 231 |
efrain |
168 |
$session->regenerateId(true);
|
| 1 |
efrain |
169 |
|
|
|
170 |
|
|
|
171 |
$translator = $serviceManager->get('MvcTranslator');
|
|
|
172 |
$translator->addTranslationFile(
|
|
|
173 |
'phpArray',
|
|
|
174 |
__DIR__ . '/i18n/validate.php',
|
|
|
175 |
'default'
|
|
|
176 |
);
|
|
|
177 |
|
|
|
178 |
$translator->addTranslationFile(
|
|
|
179 |
'phpArray',
|
|
|
180 |
__DIR__ . '/i18n/spanish.php',
|
|
|
181 |
'default'
|
|
|
182 |
);
|
|
|
183 |
|
| 97 |
efrain |
184 |
|
|
|
185 |
|
|
|
186 |
|
|
|
187 |
|
| 1 |
efrain |
188 |
\Laminas\Validator\AbstractValidator::setDefaultTranslator($translator);
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
$headers = $event->getRequest()->getHeaders();
|
|
|
192 |
if($headers->has('token')) {
|
|
|
193 |
$device_uuid = Functions::sanitizeFilterString($headers->get('token')->getFieldValue());
|
|
|
194 |
} else {
|
|
|
195 |
$device_uuid = '';
|
|
|
196 |
}
|
|
|
197 |
if($headers->has('secret')) {
|
|
|
198 |
$password = Functions::sanitizeFilterString($headers->get('secret')->getFieldValue());
|
|
|
199 |
} else {
|
|
|
200 |
$password = '';
|
|
|
201 |
}
|
|
|
202 |
if($headers->has('rand')) {
|
|
|
203 |
$rand = Functions::sanitizeFilterString($headers->get('rand')->getFieldValue());
|
|
|
204 |
} else {
|
|
|
205 |
$rand = 0;
|
|
|
206 |
}
|
|
|
207 |
if($headers->has('created')) {
|
|
|
208 |
$timestamp = Functions::sanitizeFilterString($headers->get('created')->getFieldValue());
|
|
|
209 |
} else {
|
|
|
210 |
$timestamp = 0;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
|
| 4 |
efrain |
214 |
|
| 1 |
efrain |
215 |
$this->currentNetworkPlugin = new CurrentNetworkPlugin($adapter);
|
|
|
216 |
if(!$this->currentNetworkPlugin->hasNetwork()) {
|
| 43 |
efrain |
217 |
|
| 1 |
efrain |
218 |
$response = $event->getResponse();
|
| 45 |
efrain |
219 |
$code = 200;
|
|
|
220 |
$content = json_encode(['success' => false, 'data' => '200 Unauthorized - Private network - not found', 'fatal' => true]);
|
| 43 |
efrain |
221 |
$this->sendResponse($response, $code, $content);
|
|
|
222 |
|
| 1 |
efrain |
223 |
}
|
|
|
224 |
|
|
|
225 |
if($this->currentNetworkPlugin->getNetwork()->status == Network::STATUS_INACTIVE) {
|
| 43 |
efrain |
226 |
|
| 1 |
efrain |
227 |
$response = $event->getResponse();
|
| 45 |
efrain |
228 |
$code = 200;
|
|
|
229 |
$content = json_encode(['success' => false, 'data' => '200 Unauthorized - Private network - inactive', 'fatal' => true]);
|
| 43 |
efrain |
230 |
$this->sendResponse($response, $code, $content);
|
|
|
231 |
|
| 1 |
efrain |
232 |
}
|
|
|
233 |
|
| 192 |
efrain |
234 |
|
| 1 |
efrain |
235 |
|
| 192 |
efrain |
236 |
|
| 23 |
efrain |
237 |
$this->authByOTP = false;
|
| 1 |
efrain |
238 |
if($device_uuid && $password && $rand && $timestamp) {
|
| 23 |
efrain |
239 |
$this->authByOTP = true;
|
| 1 |
efrain |
240 |
|
|
|
241 |
|
|
|
242 |
$tokenAuthAdapter = new AuthTokenAdapter($adapter);
|
|
|
243 |
$tokenAuthAdapter->setData($device_uuid, $password, $timestamp, $rand);
|
|
|
244 |
|
|
|
245 |
$authService = new AuthenticationService();
|
|
|
246 |
$result = $authService->authenticate($tokenAuthAdapter);
|
|
|
247 |
if($result->getCode() != \Laminas\Authentication\Result::SUCCESS) {
|
| 43 |
efrain |
248 |
|
| 1 |
efrain |
249 |
$response = $event->getResponse();
|
| 43 |
efrain |
250 |
$code = 200;
|
|
|
251 |
$content = json_encode(['success' => false, 'data' => $result->getMessages()[0], 'fatal' => true]);
|
|
|
252 |
$this->sendResponse($response, $code, $content);
|
|
|
253 |
|
| 1 |
efrain |
254 |
}
|
|
|
255 |
|
|
|
256 |
}
|
| 23 |
efrain |
257 |
|
| 102 |
efrain |
258 |
$this->jwtID = 0;
|
| 23 |
efrain |
259 |
$this->authByJWT = false;
|
|
|
260 |
$headers = getallheaders();
|
| 34 |
efrain |
261 |
|
|
|
262 |
if(!empty($headers['authorization']) || !empty($headers['Authorization'])) {
|
| 23 |
efrain |
263 |
|
| 34 |
efrain |
264 |
$token = trim(empty($headers['authorization']) ? $headers['Authorization'] : $headers['authorization']);
|
|
|
265 |
|
|
|
266 |
|
| 23 |
efrain |
267 |
if (substr($token, 0, 6 ) == 'Bearer') {
|
| 43 |
efrain |
268 |
|
| 23 |
efrain |
269 |
|
|
|
270 |
$token = trim(substr($token, 7));
|
|
|
271 |
|
|
|
272 |
if(!empty($this->config['leaderslinked.jwt.key'])) {
|
|
|
273 |
$key = $this->config['leaderslinked.jwt.key'];
|
|
|
274 |
|
|
|
275 |
|
|
|
276 |
try {
|
|
|
277 |
$payload = JWT::decode($token, new Key($key, 'HS256'));
|
|
|
278 |
|
|
|
279 |
|
|
|
280 |
if(empty($payload->iss) || $payload->iss != $_SERVER['HTTP_HOST']) {
|
| 43 |
efrain |
281 |
|
| 23 |
efrain |
282 |
$response = $event->getResponse();
|
| 45 |
efrain |
283 |
$code = 200;
|
| 43 |
efrain |
284 |
$content = json_encode(['success' => false, 'data' => 'Unauthorized - JWT - Wrong server', 'fatal' => true]);
|
|
|
285 |
$this->sendResponse($response, $code, $content);
|
| 23 |
efrain |
286 |
|
| 43 |
efrain |
287 |
|
|
|
288 |
|
| 23 |
efrain |
289 |
}
|
|
|
290 |
|
|
|
291 |
$uuid = empty($payload->uuid) ? '' : $payload->uuid;
|
|
|
292 |
if($uuid) {
|
|
|
293 |
$jwtTokenMapper = JwtTokenMapper::getInstance($adapter);
|
| 100 |
efrain |
294 |
$jwtToken = $jwtTokenMapper->fetchOneByUuid($uuid);
|
|
|
295 |
if($jwtToken) {
|
| 23 |
efrain |
296 |
|
| 102 |
efrain |
297 |
$this->jwtID = $jwtToken->id;
|
|
|
298 |
|
| 100 |
efrain |
299 |
$_SESSION['aes'] = $jwtToken->aes;
|
| 23 |
efrain |
300 |
|
| 100 |
efrain |
301 |
if($jwtToken->user_id) {
|
| 23 |
efrain |
302 |
$authByUserId = new AuthUserIdAdapter($adapter);
|
| 100 |
efrain |
303 |
$authByUserId->setData($jwtToken->user_id);
|
| 23 |
efrain |
304 |
|
|
|
305 |
$authService = new AuthenticationService();
|
|
|
306 |
$result = $authService->authenticate($authByUserId);
|
|
|
307 |
if($result->getCode() != \Laminas\Authentication\Result::SUCCESS) {
|
| 43 |
efrain |
308 |
|
| 23 |
efrain |
309 |
$response = $event->getResponse();
|
| 45 |
efrain |
310 |
$code = 200;
|
| 43 |
efrain |
311 |
$content = json_encode(['success' => false, 'data' => $result->getMessages()[0], 'fatal' => true]);
|
|
|
312 |
$this->sendResponse($response, $code, $content);
|
| 23 |
efrain |
313 |
|
|
|
314 |
}
|
| 24 |
efrain |
315 |
|
| 102 |
efrain |
316 |
|
| 23 |
efrain |
317 |
}
|
|
|
318 |
|
|
|
319 |
|
|
|
320 |
} else {
|
|
|
321 |
$response = $event->getResponse();
|
| 45 |
efrain |
322 |
$code = 200;
|
| 43 |
efrain |
323 |
$content = json_encode(['success' => false, 'data' => 'Unauthorized - JWT - Expired', 'fatal' => true]);
|
|
|
324 |
$this->sendResponse($response, $code, $content);
|
|
|
325 |
|
| 23 |
efrain |
326 |
}
|
|
|
327 |
|
|
|
328 |
}
|
|
|
329 |
} catch(\Exception $e) {
|
| 43 |
efrain |
330 |
|
| 23 |
efrain |
331 |
$response = $event->getResponse();
|
| 45 |
efrain |
332 |
$code = 200;
|
| 43 |
efrain |
333 |
$content = json_encode(['success' => false, 'data' => 'Unauthorized - JWT - Wrong key', 'fatal' => true]);
|
|
|
334 |
$this->sendResponse($response, $code, $content);
|
|
|
335 |
|
| 23 |
efrain |
336 |
}
|
|
|
337 |
}
|
|
|
338 |
}
|
|
|
339 |
}
|
| 1 |
efrain |
340 |
|
|
|
341 |
|
|
|
342 |
|
|
|
343 |
if(empty($_SERVER['REDIRECT_URL'])) {
|
|
|
344 |
if(empty($_SERVER['REQUEST_URI'])) {
|
|
|
345 |
$routeName = '';
|
|
|
346 |
|
|
|
347 |
} else {
|
|
|
348 |
$routeName = $_SERVER['REQUEST_URI'];
|
|
|
349 |
}
|
|
|
350 |
|
|
|
351 |
} else {
|
|
|
352 |
$routeName = $_SERVER['REDIRECT_URL'];
|
|
|
353 |
|
|
|
354 |
}
|
|
|
355 |
|
|
|
356 |
|
|
|
357 |
$routeName = strtolower(trim($routeName));
|
|
|
358 |
if(strlen($routeName) > 0 && substr($routeName, 0, 1) == '/') {
|
|
|
359 |
$routeName = substr($routeName, 1);
|
|
|
360 |
}
|
|
|
361 |
|
|
|
362 |
$this->currentUserPlugin = new CurrentUserPlugin($adapter);
|
| 192 |
efrain |
363 |
if($this->currentUserPlugin->hasIdentity()) {
|
|
|
364 |
|
| 195 |
efrain |
365 |
|
|
|
366 |
|
| 192 |
efrain |
367 |
if(User::STATUS_BANNED == $this->currentUserPlugin->getUser()->status) {
|
|
|
368 |
|
|
|
369 |
$code = 200;
|
|
|
370 |
$content = json_encode(['success' => false, 'data' => '403 Forbidden - Banned', 'fatal' => true]);
|
|
|
371 |
$this->sendResponse($response, $code, $content);
|
|
|
372 |
}
|
|
|
373 |
|
|
|
374 |
}
|
| 1 |
efrain |
375 |
|
| 23 |
efrain |
376 |
if($this->authByOTP && substr($routeName, 0, 8) == 'services') {
|
| 1 |
efrain |
377 |
$checkUserForNetwork = false;
|
|
|
378 |
} else {
|
|
|
379 |
if($this->currentUserPlugin->hasIdentity()) {
|
|
|
380 |
|
|
|
381 |
$checkUserForNetwork = true;
|
|
|
382 |
} else {
|
|
|
383 |
$checkUserForNetwork = false;
|
|
|
384 |
}
|
|
|
385 |
}
|
|
|
386 |
|
|
|
387 |
if($checkUserForNetwork) {
|
|
|
388 |
if(!$routeName || in_array($routeName, ['signout', 'signin', 'home'])) {
|
|
|
389 |
$checkUserForNetwork = false;
|
|
|
390 |
}
|
|
|
391 |
}
|
|
|
392 |
|
|
|
393 |
if($checkUserForNetwork) {
|
|
|
394 |
|
| 154 |
efrain |
395 |
|
| 1 |
efrain |
396 |
|
|
|
397 |
if($this->currentUserPlugin->getUser()->network_id != $this->currentNetworkPlugin->getNetworkId()) {
|
| 154 |
efrain |
398 |
|
| 1 |
efrain |
399 |
$response = $event->getResponse();
|
| 154 |
efrain |
400 |
$content = json_encode(['success' => false, 'data' => '200 Unauthorized - The user is not part of this private network', 'fatal' => true]);
|
|
|
401 |
$this->sendResponse($response, 200, $content);
|
| 1 |
efrain |
402 |
exit;
|
|
|
403 |
}
|
|
|
404 |
}
|
|
|
405 |
|
|
|
406 |
|
|
|
407 |
|
|
|
408 |
$this->initAcl($event);
|
| 68 |
efrain |
409 |
|
| 88 |
efrain |
410 |
|
| 1 |
efrain |
411 |
|
|
|
412 |
$sharedManager = $eventManager->getSharedManager();
|
|
|
413 |
$sharedManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, [$this, 'authPreDispatch'], 100);
|
|
|
414 |
$sharedManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, [$this, 'authPosDispatch'], -100);
|
|
|
415 |
}
|
|
|
416 |
|
|
|
417 |
public function initAcl(MvcEvent $event)
|
|
|
418 |
{
|
|
|
419 |
|
|
|
420 |
$serviceManager = $event->getApplication()->getServiceManager();
|
|
|
421 |
$adapter = $serviceManager->get('leaders-linked-db');
|
|
|
422 |
|
|
|
423 |
|
|
|
424 |
require_once (dirname(__DIR__) . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'acl.config.php');
|
|
|
425 |
|
|
|
426 |
|
|
|
427 |
$this->acl = new Acl();
|
|
|
428 |
$resources = getAclResources();
|
|
|
429 |
|
|
|
430 |
foreach($resources as $resourceName)
|
|
|
431 |
{
|
|
|
432 |
$this->acl->addResource(new GenericResource($resourceName));
|
|
|
433 |
}
|
|
|
434 |
|
|
|
435 |
$usertypes = getAclUsertype();
|
|
|
436 |
foreach($usertypes as $usertype => $resources)
|
|
|
437 |
{
|
|
|
438 |
$this->acl->addRole(new GenericRole($usertype));
|
|
|
439 |
foreach ($resources as $resourceName)
|
|
|
440 |
{
|
|
|
441 |
$this->acl->allow($usertype, $resourceName);
|
|
|
442 |
}
|
|
|
443 |
}
|
|
|
444 |
|
|
|
445 |
|
|
|
446 |
|
| 167 |
efrain |
447 |
if($this->currentUserPlugin->hasIdentity()) {
|
|
|
448 |
$user_id = $this->currentUserPlugin->getUserId();
|
|
|
449 |
|
|
|
450 |
if($this->currentUserPlugin->getUser()->is_super_user == User::IS_SUPER_USER_YES) {
|
|
|
451 |
|
|
|
452 |
|
|
|
453 |
$resources = getAclSuperAdmin();
|
|
|
454 |
foreach($resources as $resourceName)
|
|
|
455 |
{
|
|
|
456 |
$this->acl->allow(UserType::ADMIN, $resourceName);
|
|
|
457 |
}
|
| 1 |
efrain |
458 |
}
|
| 167 |
efrain |
459 |
} else {
|
|
|
460 |
$user_id = 0;
|
| 1 |
efrain |
461 |
}
|
|
|
462 |
|
|
|
463 |
$allowMyCoach = false;
|
|
|
464 |
$allowKnowledgeArea = false;
|
|
|
465 |
$allowDailyPulse = false;
|
|
|
466 |
|
|
|
467 |
|
| 167 |
efrain |
468 |
if( $user_id) {
|
|
|
469 |
$allowMicrolearning = $this->isMicroLeargningAccessGranted($adapter, $user_id);
|
|
|
470 |
} else {
|
|
|
471 |
$allowMicrolearning = false;
|
|
|
472 |
}
|
| 1 |
efrain |
473 |
|
| 167 |
efrain |
474 |
|
|
|
475 |
|
|
|
476 |
|
|
|
477 |
|
|
|
478 |
|
| 1 |
efrain |
479 |
$companyMapper = CompanyMapper::getInstance($adapter);
|
|
|
480 |
$company = $companyMapper->fetchDefaultForNetworkByNetworkId($this->currentNetworkPlugin->getNetwork()->id);
|
|
|
481 |
|
|
|
482 |
|
|
|
483 |
if($company) {
|
|
|
484 |
|
|
|
485 |
$companyServiceMapper = CompanyServiceMapper::getInstance($adapter);
|
|
|
486 |
$companyService = $companyServiceMapper->fetchOneActiveByCompanyIdAndServiceId($company->id, Service::DAILY_PULSE);
|
|
|
487 |
|
|
|
488 |
|
|
|
489 |
$companyUserMapper = CompanyUserMapper::getInstance($adapter);
|
|
|
490 |
$companyUser = $companyUserMapper->fetchOneAcceptedByCompanyIdAndUserId($company->id, $this->currentUserPlugin->getUserId());
|
|
|
491 |
|
|
|
492 |
|
|
|
493 |
|
|
|
494 |
|
|
|
495 |
if($companyService) {
|
|
|
496 |
|
|
|
497 |
$dailyPulseMapper = DailyPulseMapper::getInstance($adapter);
|
|
|
498 |
$dailyPulse = $dailyPulseMapper->fetchOneByCompanyId($company->id);
|
|
|
499 |
|
|
|
500 |
if($dailyPulse) {
|
|
|
501 |
$privacy = $dailyPulse->privacy;
|
|
|
502 |
|
|
|
503 |
} else {
|
|
|
504 |
$privacy = DailyPulse::PRIVACY_COMPANY;
|
|
|
505 |
}
|
|
|
506 |
|
|
|
507 |
if($privacy == DailyPulse::PRIVACY_PUBLIC) {
|
|
|
508 |
$allowDailyPulse = true;
|
|
|
509 |
} else {
|
|
|
510 |
$allowDailyPulse = !empty($companyUser);
|
|
|
511 |
}
|
|
|
512 |
|
|
|
513 |
|
|
|
514 |
}
|
|
|
515 |
|
|
|
516 |
$job_description_ids = [];
|
|
|
517 |
|
|
|
518 |
$organizationPositionMapper = OrganizationPositionMapper::getInstance($adapter);
|
|
|
519 |
$records = $organizationPositionMapper->fetchAllByCompanyIdAndEmployeeId($company->id, $this->currentUserPlugin->getUserId());
|
|
|
520 |
foreach($records as $record)
|
|
|
521 |
{
|
|
|
522 |
array_push($job_description_ids, $record->job_description_id);
|
|
|
523 |
}
|
|
|
524 |
|
|
|
525 |
|
|
|
526 |
$companyService = $companyServiceMapper->fetchOneActiveByCompanyIdAndServiceId($company->id, Service::KNOWLEDGE_AREA);
|
|
|
527 |
if($companyService) {
|
|
|
528 |
if($job_description_ids) {
|
|
|
529 |
|
|
|
530 |
|
|
|
531 |
$knowledgeAreaCategoryJobDescriptionMapper = KnowledgeAreaCategoryJobDescriptionMapper::getInstance($adapter);
|
|
|
532 |
$records = $knowledgeAreaCategoryJobDescriptionMapper->fetchAllByCompanyIdAndJobDescriptionIds($company->id, $job_description_ids);
|
|
|
533 |
|
|
|
534 |
if(!empty($records)) {
|
|
|
535 |
$allowKnowledgeArea = true;
|
|
|
536 |
}
|
|
|
537 |
|
|
|
538 |
}
|
|
|
539 |
|
|
|
540 |
if($companyUser && !$allowKnowledgeArea) {
|
|
|
541 |
$knowledgeAreaCategoryUserMapper = KnowledgeAreaCategoryUserMapper::getInstance($adapter);
|
|
|
542 |
$records = $knowledgeAreaCategoryUserMapper->fetchAllByUserId($companyUser->user_id);
|
|
|
543 |
if(!empty($records)) {
|
|
|
544 |
$allowKnowledgeArea = true;
|
|
|
545 |
}
|
|
|
546 |
}
|
|
|
547 |
}
|
|
|
548 |
|
|
|
549 |
$companyService = $companyServiceMapper->fetchOneActiveByCompanyIdAndServiceId($company->id, Service::MY_COACH);
|
|
|
550 |
if($companyService) {
|
|
|
551 |
|
|
|
552 |
|
|
|
553 |
if($job_description_ids) {
|
|
|
554 |
|
|
|
555 |
|
|
|
556 |
$myCoachCategoryJobDescriptionMapper = MyCoachCategoryJobDescriptionMapper::getInstance($adapter);
|
|
|
557 |
$records = $myCoachCategoryJobDescriptionMapper->fetchAllByCompanyIdAndJobDescriptionIds($company->id, $job_description_ids);
|
|
|
558 |
|
|
|
559 |
if(!empty($records)) {
|
|
|
560 |
$allowKnowledgeArea = true;
|
|
|
561 |
}
|
|
|
562 |
|
|
|
563 |
}
|
|
|
564 |
|
|
|
565 |
if($companyUser && !$allowMyCoach) {
|
|
|
566 |
$myCoachCategoryUserMapper = MyCoachCategoryUserMapper::getInstance($adapter);
|
|
|
567 |
$records = $myCoachCategoryUserMapper->fetchAllByUserId($companyUser->user_id);
|
|
|
568 |
if(!empty($records)) {
|
|
|
569 |
$allowMyCoach = true;
|
|
|
570 |
}
|
|
|
571 |
|
|
|
572 |
|
|
|
573 |
}
|
|
|
574 |
}
|
|
|
575 |
|
|
|
576 |
} else {
|
|
|
577 |
$companyUser = '';
|
|
|
578 |
}
|
|
|
579 |
|
|
|
580 |
|
|
|
581 |
$usertype = $this->currentUserPlugin->getUserTypeId();
|
|
|
582 |
if($allowDailyPulse) {
|
|
|
583 |
$resources = getAclDailyPulse();
|
|
|
584 |
foreach($resources as $resourceName)
|
|
|
585 |
{
|
|
|
586 |
$this->acl->allow($usertype, $resourceName);
|
|
|
587 |
}
|
|
|
588 |
}
|
|
|
589 |
|
| 167 |
efrain |
590 |
if($allowMicrolearning) {
|
|
|
591 |
$resources = getAclMicrolearning();
|
|
|
592 |
foreach($resources as $resourceName)
|
|
|
593 |
{
|
|
|
594 |
$this->acl->allow($usertype, $resourceName);
|
|
|
595 |
}
|
|
|
596 |
}
|
|
|
597 |
|
|
|
598 |
|
| 1 |
efrain |
599 |
if($allowKnowledgeArea) {
|
|
|
600 |
$resources = getAclKnowledgeArea();
|
|
|
601 |
foreach($resources as $resourceName)
|
|
|
602 |
{
|
|
|
603 |
$this->acl->allow($usertype, $resourceName);
|
|
|
604 |
}
|
|
|
605 |
}
|
|
|
606 |
|
|
|
607 |
if($allowMyCoach) {
|
|
|
608 |
$resources = getAclMyCoach();
|
|
|
609 |
|
|
|
610 |
|
|
|
611 |
|
|
|
612 |
foreach($resources as $resourceName)
|
|
|
613 |
{
|
|
|
614 |
$this->acl->allow($usertype, $resourceName);
|
|
|
615 |
}
|
|
|
616 |
|
|
|
617 |
}
|
|
|
618 |
|
|
|
619 |
|
|
|
620 |
|
|
|
621 |
if($this->currentNetworkPlugin->getNetwork()->default == Network::DEFAULT_YES) {
|
|
|
622 |
|
|
|
623 |
$usertypes = getAclUsertypeDefaultNetwork();
|
|
|
624 |
foreach($usertypes as $usertype => $resources)
|
|
|
625 |
{
|
|
|
626 |
|
|
|
627 |
|
|
|
628 |
|
|
|
629 |
foreach ($resources as $resourceName)
|
|
|
630 |
{
|
|
|
631 |
$this->acl->allow($usertype, $resourceName);
|
|
|
632 |
}
|
|
|
633 |
}
|
|
|
634 |
|
|
|
635 |
|
|
|
636 |
} else {
|
|
|
637 |
|
|
|
638 |
if($this->currentUserPlugin->hasIdentity()) {
|
|
|
639 |
|
|
|
640 |
|
|
|
641 |
if($company) {
|
|
|
642 |
|
|
|
643 |
|
|
|
644 |
if($companyUser) {
|
|
|
645 |
$usertype = $this->currentUserPlugin->getUserTypeId();
|
|
|
646 |
|
|
|
647 |
if($companyUser->creator == CompanyUser::CREATOR_YES) {
|
|
|
648 |
|
|
|
649 |
$resources = getAclUsertypeOtherNetworkCreator();
|
|
|
650 |
foreach($resources as $resourceName)
|
|
|
651 |
{
|
|
|
652 |
$this->acl->allow($usertype, $resourceName);
|
|
|
653 |
}
|
|
|
654 |
|
|
|
655 |
}
|
|
|
656 |
if($companyUser->creator == CompanyUser::CREATOR_NO) {
|
|
|
657 |
$resources = getAclUsertypeOtherNetworkNonCreator();
|
|
|
658 |
foreach($resources as $resourceName)
|
|
|
659 |
{
|
|
|
660 |
$this->acl->allow($usertype, $resourceName);
|
|
|
661 |
}
|
|
|
662 |
}
|
|
|
663 |
}
|
|
|
664 |
}
|
|
|
665 |
}
|
|
|
666 |
}
|
|
|
667 |
|
|
|
668 |
|
|
|
669 |
$event->getViewModel()->setVariable('acl', $this->acl);
|
| 88 |
efrain |
670 |
|
|
|
671 |
|
| 1 |
efrain |
672 |
|
|
|
673 |
}
|
|
|
674 |
|
|
|
675 |
public function onDispatchError(MvcEvent $event)
|
|
|
676 |
{
|
|
|
677 |
$this->processError($event);
|
|
|
678 |
}
|
|
|
679 |
|
|
|
680 |
public function onRenderError(MvcEvent $event)
|
|
|
681 |
{
|
|
|
682 |
$this->processError($event);
|
|
|
683 |
}
|
|
|
684 |
|
| 43 |
efrain |
685 |
/**
|
|
|
686 |
*
|
|
|
687 |
* @param \Laminas\Http\Response $response
|
|
|
688 |
* @param int $code
|
|
|
689 |
* @param string $content
|
|
|
690 |
*/
|
|
|
691 |
public function sendResponse($response, $code, $content)
|
| 1 |
efrain |
692 |
{
|
| 94 |
efrain |
693 |
|
| 43 |
efrain |
694 |
|
| 44 |
efrain |
695 |
|
| 1 |
efrain |
696 |
$headers = $response->getHeaders();
|
|
|
697 |
$headers->clearHeaders();
|
|
|
698 |
$headers->addHeaderLine('Content-type', 'application/json; charset=UTF-8');
|
|
|
699 |
|
| 43 |
efrain |
700 |
Functions::addCrossSiteToResponse($response);
|
|
|
701 |
|
|
|
702 |
$response->setStatusCode($code);
|
|
|
703 |
$response->setContent($content); //json_encode($data));
|
| 1 |
efrain |
704 |
$response->send();
|
|
|
705 |
exit;
|
|
|
706 |
}
|
|
|
707 |
|
|
|
708 |
public function processError(MvcEvent $event)
|
|
|
709 |
{
|
|
|
710 |
$error = $event->getError();
|
|
|
711 |
if (!$error) {
|
|
|
712 |
return;
|
|
|
713 |
}
|
|
|
714 |
|
|
|
715 |
$response = $event->getResponse();
|
|
|
716 |
if('error-exception' == $error) {
|
| 195 |
efrain |
717 |
|
|
|
718 |
|
| 1 |
efrain |
719 |
$exception = $event->getParam('exception');
|
|
|
720 |
error_log($exception->getCode() . ' ' . $exception->getMessage());
|
|
|
721 |
error_log($exception->getTraceAsString());
|
| 43 |
efrain |
722 |
|
| 1 |
efrain |
723 |
$response = $event->getResponse();
|
| 45 |
efrain |
724 |
$code = 200;
|
| 43 |
efrain |
725 |
$content = json_encode(['success' => false, 'data' => $exception->getCode() . ' ' . $exception->getMessage(), 'fatal' => true]);
|
|
|
726 |
$this->sendResponse($response, $code, $content);
|
| 1 |
efrain |
727 |
|
|
|
728 |
} else if('error-router-no-match' == $error) {
|
| 43 |
efrain |
729 |
|
| 1 |
efrain |
730 |
$response = $event->getResponse();
|
| 43 |
efrain |
731 |
$code = 404;
|
|
|
732 |
$content = json_encode(['success' => false, 'data' => 'error-router-no-match', 'fatal' => true]);
|
|
|
733 |
$this->sendResponse($response, $code, $content);
|
| 1 |
efrain |
734 |
|
|
|
735 |
|
|
|
736 |
|
|
|
737 |
} else if(' error-controller-not-found' == $error) {
|
| 43 |
efrain |
738 |
|
| 1 |
efrain |
739 |
$response = $event->getResponse();
|
| 43 |
efrain |
740 |
$code = 404;
|
|
|
741 |
$content = json_encode(['success' => false, 'data' => 'error-controller-not-found', 'fatal' => true]);
|
|
|
742 |
$this->sendResponse($response, $code, $content);
|
| 1 |
efrain |
743 |
|
| 43 |
efrain |
744 |
|
| 1 |
efrain |
745 |
} else {
|
|
|
746 |
$response = $event->getResponse();
|
| 45 |
efrain |
747 |
$code = 200;
|
| 43 |
efrain |
748 |
$content = json_encode(['success' => false, 'data' => $error, 'fatal' => true]);
|
|
|
749 |
$this->sendResponse($response, $code, $content);
|
| 1 |
efrain |
750 |
|
| 43 |
efrain |
751 |
|
| 1 |
efrain |
752 |
}
|
|
|
753 |
|
|
|
754 |
exit;
|
|
|
755 |
|
|
|
756 |
}
|
|
|
757 |
|
|
|
758 |
|
|
|
759 |
public function authPreDispatch(MvcEvent $event)
|
|
|
760 |
{
|
|
|
761 |
|
| 195 |
efrain |
762 |
|
| 1 |
efrain |
763 |
|
|
|
764 |
|
|
|
765 |
$serviceManager = $event->getApplication()->getServiceManager();
|
|
|
766 |
$adapter = $serviceManager->get('leaders-linked-db');
|
|
|
767 |
|
|
|
768 |
$routeName = $event->getRouteMatch()->getMatchedRouteName();
|
|
|
769 |
|
|
|
770 |
|
|
|
771 |
$requestMethod = isset($_SERVER['REQUEST_METHOD']) ? trim(strtoupper($_SERVER['REQUEST_METHOD'])) : '';
|
|
|
772 |
|
| 95 |
efrain |
773 |
if($requestMethod == 'POST') {
|
| 1 |
efrain |
774 |
|
|
|
775 |
|
| 23 |
efrain |
776 |
if($this->authByOTP && substr($routeName, 0, 8) == 'services') {
|
| 1 |
efrain |
777 |
$exclude = true;
|
|
|
778 |
} else {
|
|
|
779 |
$exclude = false;
|
|
|
780 |
|
|
|
781 |
$usertypes = getAclUsertype();
|
|
|
782 |
|
|
|
783 |
|
|
|
784 |
foreach($usertypes[UserType::GUEST] as $resourceName)
|
|
|
785 |
{
|
|
|
786 |
if($routeName == $resourceName) {
|
|
|
787 |
$exclude = true;
|
|
|
788 |
break;
|
|
|
789 |
}
|
|
|
790 |
}
|
|
|
791 |
}
|
|
|
792 |
|
| 95 |
efrain |
793 |
|
| 1 |
efrain |
794 |
if(!$exclude) {
|
| 95 |
efrain |
795 |
|
| 1 |
efrain |
796 |
$httpToken = isset($_SERVER['HTTP_X_CSRF_TOKEN']) ? $_SERVER['HTTP_X_CSRF_TOKEN'] : '';
|
|
|
797 |
|
| 100 |
efrain |
798 |
|
|
|
799 |
|
| 102 |
efrain |
800 |
if($this->jwtID) {
|
| 103 |
efrain |
801 |
|
| 95 |
efrain |
802 |
$jwtTokenMapper = JwtTokenMapper::getInstance($this->adapter);
|
| 102 |
efrain |
803 |
$jwtToken = $jwtTokenMapper->fetchOne($this->jwtID);
|
| 100 |
efrain |
804 |
if($jwtToken) {
|
| 95 |
efrain |
805 |
|
| 100 |
efrain |
806 |
$sessionToken = $jwtToken->csrf;
|
|
|
807 |
//$jwtToken->csrf= '';
|
|
|
808 |
|
|
|
809 |
|
|
|
810 |
// $jwtTokenMapper->update($jwtToken);
|
|
|
811 |
} else {
|
|
|
812 |
$sessionToken = '';
|
|
|
813 |
}
|
|
|
814 |
|
| 95 |
efrain |
815 |
|
|
|
816 |
} else {
|
|
|
817 |
$sessionToken = '';
|
|
|
818 |
}
|
|
|
819 |
|
|
|
820 |
|
| 106 |
efrain |
821 |
//error_log('$this->jwtID = ' . $this->jwtID . ' $httpToken = ' . $httpToken . ' $sessionToken = ' . $sessionToken);
|
| 100 |
efrain |
822 |
|
| 1 |
efrain |
823 |
if ( $httpToken != $sessionToken) {
|
|
|
824 |
$response = $event->getResponse();
|
| 106 |
efrain |
825 |
$this->sendResponse($response, 200, json_encode(['success' => false, 'data' => 'Unauthorized - CSRF fail', 'fatal' => true]));
|
| 1 |
efrain |
826 |
}
|
|
|
827 |
|
|
|
828 |
}
|
|
|
829 |
}
|
| 95 |
efrain |
830 |
|
| 1 |
efrain |
831 |
|
|
|
832 |
|
|
|
833 |
|
|
|
834 |
if($this->currentUserPlugin->hasIdentity()) {
|
|
|
835 |
$user = $this->currentUserPlugin->getUser();
|
| 167 |
efrain |
836 |
$user_id = $user->id;
|
| 1 |
efrain |
837 |
$userTypeId = $user->usertype_id;
|
|
|
838 |
|
|
|
839 |
|
|
|
840 |
} else {
|
|
|
841 |
|
|
|
842 |
$userTypeId = UserType::GUEST;
|
| 167 |
efrain |
843 |
$user_id = 0;
|
| 1 |
efrain |
844 |
}
|
|
|
845 |
|
| 167 |
efrain |
846 |
|
| 1 |
efrain |
847 |
|
| 167 |
efrain |
848 |
|
|
|
849 |
|
|
|
850 |
|
|
|
851 |
|
| 1 |
efrain |
852 |
if($this->acl->isAllowed($userTypeId, $routeName)) {
|
|
|
853 |
$user = $this->currentUserPlugin->getUser();
|
|
|
854 |
|
|
|
855 |
|
|
|
856 |
if($user) {
|
|
|
857 |
|
|
|
858 |
$updateLastActivity = true;
|
|
|
859 |
if ('chat' == substr($routeName, 0, 4)) {
|
|
|
860 |
$updateLastActivity = false;
|
|
|
861 |
}
|
|
|
862 |
if ('inmail' == substr($routeName, 0, 6)) {
|
|
|
863 |
$updateLastActivity = false;
|
|
|
864 |
}
|
|
|
865 |
if ('check-session' == $routeName) {
|
|
|
866 |
$updateLastActivity = false;
|
|
|
867 |
}
|
|
|
868 |
|
|
|
869 |
|
|
|
870 |
if($updateLastActivity) {
|
|
|
871 |
$userMapper = UserMapper::getInstance($adapter);
|
|
|
872 |
$userMapper->updateLastActivity($user->id);
|
|
|
873 |
}
|
|
|
874 |
}
|
|
|
875 |
|
|
|
876 |
|
|
|
877 |
|
|
|
878 |
} else {
|
|
|
879 |
$response = $event->getResponse();
|
| 54 |
efrain |
880 |
|
| 45 |
efrain |
881 |
$response->setStatusCode(200);
|
| 106 |
efrain |
882 |
$response->setContent(json_encode(['success' => false, 'data' => 'Unauthorized - Does not have permission', 'fatal' => true]));
|
| 1 |
efrain |
883 |
$response->send();
|
|
|
884 |
exit;
|
|
|
885 |
|
|
|
886 |
|
|
|
887 |
|
|
|
888 |
}
|
| 90 |
efrain |
889 |
|
| 91 |
efrain |
890 |
|
| 90 |
efrain |
891 |
|
| 1 |
efrain |
892 |
|
|
|
893 |
|
|
|
894 |
}
|
|
|
895 |
|
|
|
896 |
|
|
|
897 |
public function authPosDispatch(MvcEvent $event)
|
|
|
898 |
{
|
| 90 |
efrain |
899 |
//$response = $event->getResponse();
|
|
|
900 |
// Functions::addCrossSiteToResponse($response);
|
| 1 |
efrain |
901 |
}
|
|
|
902 |
|
| 167 |
efrain |
903 |
|
|
|
904 |
/**
|
|
|
905 |
*
|
|
|
906 |
* @param \Laminas\Db\Adapter\AdapterInterface $adapter
|
|
|
907 |
* @param int $user_id
|
|
|
908 |
* @return boolean
|
|
|
909 |
*/
|
|
|
910 |
private function isMicroLeargningAccessGranted($adapter, $user_id)
|
|
|
911 |
{
|
|
|
912 |
|
|
|
913 |
$accessGranted = false;
|
|
|
914 |
$capsuleUserMapper = \LeadersLinked\Mapper\CompanyMicrolearningCapsuleUserMapper::getInstance($adapter);
|
|
|
915 |
$now = $capsuleUserMapper->getDatebaseNow();
|
|
|
916 |
|
|
|
917 |
$records = $capsuleUserMapper->fetchAllActiveByUserId($user_id);
|
|
|
918 |
|
|
|
919 |
|
|
|
920 |
foreach($records as $record)
|
|
|
921 |
{
|
|
|
922 |
if($record->access != \LeadersLinked\Model\CompanyMicrolearningCapsuleUser::ACCESS_UNLIMITED && $record->access != \LeadersLinked\Model\CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
|
|
|
923 |
continue;
|
|
|
924 |
}
|
|
|
925 |
if($record->access == \LeadersLinked\Model\CompanyMicrolearningCapsuleUser::ACCESS_PAY_PERIOD) {
|
|
|
926 |
if($now < $record->paid_from || $now > $record->paid_to) {
|
|
|
927 |
continue;
|
|
|
928 |
}
|
|
|
929 |
}
|
|
|
930 |
$accessGranted = true;
|
|
|
931 |
break;
|
|
|
932 |
}
|
|
|
933 |
|
|
|
934 |
return $accessGranted;
|
|
|
935 |
}
|
| 1 |
efrain |
936 |
|
|
|
937 |
|
|
|
938 |
|
|
|
939 |
}
|