Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Auth;
4
 
5
use Closure;
6
use Illuminate\Contracts\Auth\Factory as FactoryContract;
7
use InvalidArgumentException;
8
 
9
/**
10
 * @mixin \Illuminate\Contracts\Auth\Guard
11
 * @mixin \Illuminate\Contracts\Auth\StatefulGuard
12
 */
13
class AuthManager implements FactoryContract
14
{
15
    use CreatesUserProviders;
16
 
17
    /**
18
     * The application instance.
19
     *
20
     * @var \Illuminate\Contracts\Foundation\Application
21
     */
22
    protected $app;
23
 
24
    /**
25
     * The registered custom driver creators.
26
     *
27
     * @var array
28
     */
29
    protected $customCreators = [];
30
 
31
    /**
32
     * The array of created "drivers".
33
     *
34
     * @var array
35
     */
36
    protected $guards = [];
37
 
38
    /**
39
     * The user resolver shared by various services.
40
     *
41
     * Determines the default user for Gate, Request, and the Authenticatable contract.
42
     *
43
     * @var \Closure
44
     */
45
    protected $userResolver;
46
 
47
    /**
48
     * Create a new Auth manager instance.
49
     *
50
     * @param  \Illuminate\Contracts\Foundation\Application  $app
51
     * @return void
52
     */
53
    public function __construct($app)
54
    {
55
        $this->app = $app;
56
 
57
        $this->userResolver = fn ($guard = null) => $this->guard($guard)->user();
58
    }
59
 
60
    /**
61
     * Attempt to get the guard from the local cache.
62
     *
63
     * @param  string|null  $name
64
     * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
65
     */
66
    public function guard($name = null)
67
    {
68
        $name = $name ?: $this->getDefaultDriver();
69
 
70
        return $this->guards[$name] ?? $this->guards[$name] = $this->resolve($name);
71
    }
72
 
73
    /**
74
     * Resolve the given guard.
75
     *
76
     * @param  string  $name
77
     * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard
78
     *
79
     * @throws \InvalidArgumentException
80
     */
81
    protected function resolve($name)
82
    {
83
        $config = $this->getConfig($name);
84
 
85
        if (is_null($config)) {
86
            throw new InvalidArgumentException("Auth guard [{$name}] is not defined.");
87
        }
88
 
89
        if (isset($this->customCreators[$config['driver']])) {
90
            return $this->callCustomCreator($name, $config);
91
        }
92
 
93
        $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
94
 
95
        if (method_exists($this, $driverMethod)) {
96
            return $this->{$driverMethod}($name, $config);
97
        }
98
 
99
        throw new InvalidArgumentException(
100
            "Auth driver [{$config['driver']}] for guard [{$name}] is not defined."
101
        );
102
    }
103
 
104
    /**
105
     * Call a custom driver creator.
106
     *
107
     * @param  string  $name
108
     * @param  array  $config
109
     * @return mixed
110
     */
111
    protected function callCustomCreator($name, array $config)
112
    {
113
        return $this->customCreators[$config['driver']]($this->app, $name, $config);
114
    }
115
 
116
    /**
117
     * Create a session based authentication guard.
118
     *
119
     * @param  string  $name
120
     * @param  array  $config
121
     * @return \Illuminate\Auth\SessionGuard
122
     */
123
    public function createSessionDriver($name, $config)
124
    {
125
        $provider = $this->createUserProvider($config['provider'] ?? null);
126
 
127
        $guard = new SessionGuard(
128
            $name,
129
            $provider,
130
            $this->app['session.store'],
131
        );
132
 
133
        // When using the remember me functionality of the authentication services we
134
        // will need to be set the encryption instance of the guard, which allows
135
        // secure, encrypted cookie values to get generated for those cookies.
136
        if (method_exists($guard, 'setCookieJar')) {
137
            $guard->setCookieJar($this->app['cookie']);
138
        }
139
 
140
        if (method_exists($guard, 'setDispatcher')) {
141
            $guard->setDispatcher($this->app['events']);
142
        }
143
 
144
        if (method_exists($guard, 'setRequest')) {
145
            $guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
146
        }
147
 
148
        if (isset($config['remember'])) {
149
            $guard->setRememberDuration($config['remember']);
150
        }
151
 
152
        return $guard;
153
    }
154
 
155
    /**
156
     * Create a token based authentication guard.
157
     *
158
     * @param  string  $name
159
     * @param  array  $config
160
     * @return \Illuminate\Auth\TokenGuard
161
     */
162
    public function createTokenDriver($name, $config)
163
    {
164
        // The token guard implements a basic API token based guard implementation
165
        // that takes an API token field from the request and matches it to the
166
        // user in the database or another persistence layer where users are.
167
        $guard = new TokenGuard(
168
            $this->createUserProvider($config['provider'] ?? null),
169
            $this->app['request'],
170
            $config['input_key'] ?? 'api_token',
171
            $config['storage_key'] ?? 'api_token',
172
            $config['hash'] ?? false
173
        );
174
 
175
        $this->app->refresh('request', $guard, 'setRequest');
176
 
177
        return $guard;
178
    }
179
 
180
    /**
181
     * Get the guard configuration.
182
     *
183
     * @param  string  $name
184
     * @return array
185
     */
186
    protected function getConfig($name)
187
    {
188
        return $this->app['config']["auth.guards.{$name}"];
189
    }
190
 
191
    /**
192
     * Get the default authentication driver name.
193
     *
194
     * @return string
195
     */
196
    public function getDefaultDriver()
197
    {
198
        return $this->app['config']['auth.defaults.guard'];
199
    }
200
 
201
    /**
202
     * Set the default guard driver the factory should serve.
203
     *
204
     * @param  string  $name
205
     * @return void
206
     */
207
    public function shouldUse($name)
208
    {
209
        $name = $name ?: $this->getDefaultDriver();
210
 
211
        $this->setDefaultDriver($name);
212
 
213
        $this->userResolver = fn ($name = null) => $this->guard($name)->user();
214
    }
215
 
216
    /**
217
     * Set the default authentication driver name.
218
     *
219
     * @param  string  $name
220
     * @return void
221
     */
222
    public function setDefaultDriver($name)
223
    {
224
        $this->app['config']['auth.defaults.guard'] = $name;
225
    }
226
 
227
    /**
228
     * Register a new callback based request guard.
229
     *
230
     * @param  string  $driver
231
     * @param  callable  $callback
232
     * @return $this
233
     */
234
    public function viaRequest($driver, callable $callback)
235
    {
236
        return $this->extend($driver, function () use ($callback) {
237
            $guard = new RequestGuard($callback, $this->app['request'], $this->createUserProvider());
238
 
239
            $this->app->refresh('request', $guard, 'setRequest');
240
 
241
            return $guard;
242
        });
243
    }
244
 
245
    /**
246
     * Get the user resolver callback.
247
     *
248
     * @return \Closure
249
     */
250
    public function userResolver()
251
    {
252
        return $this->userResolver;
253
    }
254
 
255
    /**
256
     * Set the callback to be used to resolve users.
257
     *
258
     * @param  \Closure  $userResolver
259
     * @return $this
260
     */
261
    public function resolveUsersUsing(Closure $userResolver)
262
    {
263
        $this->userResolver = $userResolver;
264
 
265
        return $this;
266
    }
267
 
268
    /**
269
     * Register a custom driver creator Closure.
270
     *
271
     * @param  string  $driver
272
     * @param  \Closure  $callback
273
     * @return $this
274
     */
275
    public function extend($driver, Closure $callback)
276
    {
277
        $this->customCreators[$driver] = $callback;
278
 
279
        return $this;
280
    }
281
 
282
    /**
283
     * Register a custom provider creator Closure.
284
     *
285
     * @param  string  $name
286
     * @param  \Closure  $callback
287
     * @return $this
288
     */
289
    public function provider($name, Closure $callback)
290
    {
291
        $this->customProviderCreators[$name] = $callback;
292
 
293
        return $this;
294
    }
295
 
296
    /**
297
     * Determines if any guards have already been resolved.
298
     *
299
     * @return bool
300
     */
301
    public function hasResolvedGuards()
302
    {
303
        return count($this->guards) > 0;
304
    }
305
 
306
    /**
307
     * Forget all of the resolved guard instances.
308
     *
309
     * @return $this
310
     */
311
    public function forgetGuards()
312
    {
313
        $this->guards = [];
314
 
315
        return $this;
316
    }
317
 
318
    /**
319
     * Set the application instance used by the manager.
320
     *
321
     * @param  \Illuminate\Contracts\Foundation\Application  $app
322
     * @return $this
323
     */
324
    public function setApplication($app)
325
    {
326
        $this->app = $app;
327
 
328
        return $this;
329
    }
330
 
331
    /**
332
     * Dynamically call the default driver instance.
333
     *
334
     * @param  string  $method
335
     * @param  array  $parameters
336
     * @return mixed
337
     */
338
    public function __call($method, $parameters)
339
    {
340
        return $this->guard()->{$method}(...$parameters);
341
    }
342
}