|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Jitamin. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (C) Jitamin Team |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jitamin\Http\Middleware; |
|
13
|
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Exceptions\AccessForbiddenException; |
|
15
|
|
|
use Jitamin\Foundation\Middleware\BaseMiddleware; |
|
16
|
|
|
use Jitamin\Foundation\Security\Role; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class AuthenticationMiddleware. |
|
20
|
|
|
*/ |
|
21
|
|
|
class AuthenticationMiddleware extends BaseMiddleware |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Execute middleware. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function execute() |
|
27
|
|
|
{ |
|
28
|
|
|
if (!$this->authenticationManager->checkCurrentSession()) { |
|
|
|
|
|
|
29
|
|
|
throw AccessForbiddenException::getInstance()->withoutLayout(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (!$this->isPublicAccess()) { |
|
33
|
|
|
$this->handleAuthentication(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
$this->next(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Handle authentication. |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function handleAuthentication() |
|
43
|
|
|
{ |
|
44
|
|
|
if (!$this->userSession->isLogged() && !$this->authenticationManager->preAuthentication()) { |
|
|
|
|
|
|
45
|
|
|
$this->nextMiddleware = null; |
|
46
|
|
|
|
|
47
|
|
|
if ($this->request->isAjax()) { |
|
|
|
|
|
|
48
|
|
|
$this->response->text('Not Authorized', 401); |
|
|
|
|
|
|
49
|
|
|
} else { |
|
50
|
|
|
$this->sessionStorage->redirectAfterLogin = $this->request->getUri(); |
|
|
|
|
|
|
51
|
|
|
$this->response->redirect($this->helper->url->to('Auth/AuthController', 'login')); |
|
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Check authentication. |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function isPublicAccess() |
|
60
|
|
|
{ |
|
61
|
|
|
if ($this->applicationAuthorization->isAllowed($this->router->getController(), $this->router->getAction(), Role::APP_PUBLIC, $this->router->getPlugin())) { |
|
|
|
|
|
|
62
|
|
|
$this->nextMiddleware = null; |
|
63
|
|
|
|
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.