-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathAuth.php
More file actions
192 lines (162 loc) · 5.5 KB
/
Auth.php
File metadata and controls
192 lines (162 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter Shield.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Shield;
use CodeIgniter\Router\RouteCollection;
use CodeIgniter\Shield\Authentication\Authentication;
use CodeIgniter\Shield\Authentication\AuthenticationException;
use CodeIgniter\Shield\Authentication\AuthenticatorInterface;
use CodeIgniter\Shield\Config\Auth as AuthConfig;
use CodeIgniter\Shield\Entities\User;
use CodeIgniter\Shield\Models\UserModel;
/**
* Facade for Authentication
*
* @method Result attempt(array{email?: string, username?: string, password?: string, token?: string} $credentials)
* @method Result check(array{email?: string, username?: string, password?: string, token?: string} $credentials)
* @method bool checkAction(string $token, string $type) [Session]
* @method void forget(?User $user = null) [Session]
* @method User|null getUser()
* @method bool loggedIn()
* @method bool login(User $user)
* @method void loginById($userId)
* @method bool logout()
* @method void recordActiveDate()
* @method $this remember(bool $shouldRemember = true) [Session]
*/
class Auth
{
/**
* The current version of CodeIgniter Shield
*/
public const SHIELD_VERSION = '1.2.0';
protected ?Authentication $authenticate = null;
/**
* The Authenticator alias to use for this request.
*/
protected ?string $alias = null;
protected ?UserModel $userProvider = null;
public function __construct(protected AuthConfig $config)
{
}
protected function ensureAuthentication(): void
{
if ($this->authenticate !== null) {
return;
}
$authenticate = new Authentication($this->config);
$authenticate->setProvider($this->getProvider());
$this->authenticate = $authenticate;
}
/**
* Sets the Authenticator alias that should be used for this request.
*
* @return $this
*/
public function setAuthenticator(?string $alias = null): self
{
if ($alias !== null) {
$this->alias = $alias;
}
return $this;
}
/**
* Returns the current authentication class.
*/
public function getAuthenticator(): AuthenticatorInterface
{
$this->ensureAuthentication();
return $this->authenticate
->factory($this->alias);
}
/**
* Returns the current user, if logged in.
*/
public function user(): ?User
{
return $this->getAuthenticator()->loggedIn()
? $this->getAuthenticator()->getUser()
: null;
}
/**
* Returns the current user's id, if logged in.
*
* @return int|string|null
*/
public function id()
{
$user = $this->user();
return ($user !== null) ? $user->id : null;
}
public function authenticate(array $credentials): Result
{
$this->ensureAuthentication();
return $this->authenticate
->factory($this->alias)
->attempt($credentials);
}
/**
* Will set the routes in your application to use
* the Shield auth routes.
*
* Usage (in Config/Routes.php):
* - auth()->routes($routes);
* - auth()->routes($routes, ['except' => ['login', 'register']])
*/
public function routes(RouteCollection &$routes, array $config = []): void
{
$authRoutes = config('AuthRoutes')->routes;
$namespace = $config['namespace'] ?? 'CodeIgniter\Shield\Controllers';
$routes->group('/', ['namespace' => $namespace], static function (RouteCollection $routes) use ($authRoutes, $config): void {
foreach ($authRoutes as $name => $row) {
if (! isset($config['except']) || ! in_array($name, $config['except'], true)) {
foreach ($row as $params) {
$options = isset($params[3])
? ['as' => $params[3]]
: null;
$routes->{$params[0]}($params[1], $params[2], $options);
}
}
}
});
}
/**
* Returns the Model that is responsible for getting users.
*
* @throws AuthenticationException
*/
public function getProvider(): UserModel
{
if ($this->userProvider !== null) {
return $this->userProvider;
}
$className = $this->config->userProvider;
$this->userProvider = new $className();
return $this->userProvider;
}
/**
* Provide magic function-access to Authenticators to save use
* from repeating code here, and to allow them to have their
* own, additional, features on top of the required ones,
* like "remember-me" functionality.
*
* @param list<string> $args
*
* @throws AuthenticationException
*/
public function __call(string $method, array $args)
{
$this->ensureAuthentication();
$authenticator = $this->authenticate->factory($this->alias);
if (method_exists($authenticator, $method)) {
return $authenticator->{$method}(...$args);
}
}
}