1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Linna Framework. |
5
|
|
|
* |
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Linna\Authentication; |
13
|
|
|
|
14
|
|
|
use Linna\Authentication\Authentication; |
15
|
|
|
use Linna\Authentication\Exception\AuthenticationException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Help protect a controller with login. |
19
|
|
|
* |
20
|
|
|
* This trait is designed to add to a controller, the ability to interrupt its own execution, in case authentication is required. |
21
|
|
|
* |
22
|
|
|
* This trait contains only private mothods. |
23
|
|
|
* |
24
|
|
|
* <pre><code class="php"> |
25
|
|
|
* use Linna\Authentication\Authentication; |
26
|
|
|
* use Linna\Authentication\ProtectedControllerTrait; |
27
|
|
|
* use Linna\Mvc\Controller; |
28
|
|
|
* use Linna\Mvc\Model; |
29
|
|
|
* |
30
|
|
|
* class ProtectedController extends Controller |
31
|
|
|
* { |
32
|
|
|
* use ProtectedControllerTrait; |
33
|
|
|
* |
34
|
|
|
* public function __construct(Model $model, Authentication $authentication) |
35
|
|
|
* { |
36
|
|
|
* parent::__construct($model); |
37
|
|
|
* |
38
|
|
|
* $this->protect($authentication, '/error'); |
39
|
|
|
* } |
40
|
|
|
* |
41
|
|
|
* public function action(): bool |
42
|
|
|
* { |
43
|
|
|
* if ($this->authentication === false) { |
44
|
|
|
* return false; |
45
|
|
|
* } |
46
|
|
|
* |
47
|
|
|
* return true; |
48
|
|
|
* } |
49
|
|
|
* } |
50
|
|
|
* </code></pre> |
51
|
|
|
* |
52
|
|
|
*/ |
53
|
|
|
trait ProtectedControllerTrait |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* @var bool Contain login status. |
57
|
|
|
*/ |
58
|
|
|
private bool $authentication = false; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Allow access to controller class or methods only if logged. |
62
|
|
|
* Return a status code, useful with AJAX requests. |
63
|
|
|
* |
64
|
|
|
* @param Authentication $authentication Authentication class instance. |
65
|
|
|
* @param string $route Valid Route for Authentication exception. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
* |
69
|
|
|
* @throws AuthenticationException if user not authenticathed. |
70
|
|
|
*/ |
71
|
3 |
|
private function protect(Authentication $authentication, string $route): void |
72
|
|
|
{ |
73
|
3 |
|
if (($this->authentication = $authentication->isLogged()) === false) { |
74
|
2 |
|
throw new AuthenticationException($route); |
75
|
|
|
} |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Allow access to controller class or methods only if logged |
80
|
|
|
* and do an HTTP redirection if not. |
81
|
|
|
* |
82
|
|
|
* @param Authentication $authentication Authentication class instance. |
83
|
|
|
* @param string $location Valid url for Location header. |
84
|
|
|
* @param string $route Valid Route for Authentication exception. |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
* |
88
|
|
|
* @throws AuthenticationException if user not authenticathed. |
89
|
|
|
*/ |
90
|
2 |
|
private function protectWithRedirect(Authentication $authentication, string $location, string $route): void |
91
|
|
|
{ |
92
|
2 |
|
if (($this->authentication = $authentication->isLogged()) === false) { |
93
|
2 |
|
\header('Location: '.$location); |
94
|
2 |
|
throw new AuthenticationException($route); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|