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\Authenticatable as UserContract;
7
use Illuminate\Contracts\Auth\UserProvider;
8
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
9
use Illuminate\Contracts\Support\Arrayable;
10
 
11
class EloquentUserProvider implements UserProvider
12
{
13
    /**
14
     * The hasher implementation.
15
     *
16
     * @var \Illuminate\Contracts\Hashing\Hasher
17
     */
18
    protected $hasher;
19
 
20
    /**
21
     * The Eloquent user model.
22
     *
23
     * @var string
24
     */
25
    protected $model;
26
 
27
    /**
28
     * The callback that may modify the user retrieval queries.
29
     *
30
     * @var (\Closure(\Illuminate\Database\Eloquent\Builder):mixed)|null
31
     */
32
    protected $queryCallback;
33
 
34
    /**
35
     * Create a new database user provider.
36
     *
37
     * @param  \Illuminate\Contracts\Hashing\Hasher  $hasher
38
     * @param  string  $model
39
     * @return void
40
     */
41
    public function __construct(HasherContract $hasher, $model)
42
    {
43
        $this->model = $model;
44
        $this->hasher = $hasher;
45
    }
46
 
47
    /**
48
     * Retrieve a user by their unique identifier.
49
     *
50
     * @param  mixed  $identifier
51
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
52
     */
53
    public function retrieveById($identifier)
54
    {
55
        $model = $this->createModel();
56
 
57
        return $this->newModelQuery($model)
58
                    ->where($model->getAuthIdentifierName(), $identifier)
59
                    ->first();
60
    }
61
 
62
    /**
63
     * Retrieve a user by their unique identifier and "remember me" token.
64
     *
65
     * @param  mixed  $identifier
66
     * @param  string  $token
67
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
68
     */
69
    public function retrieveByToken($identifier, $token)
70
    {
71
        $model = $this->createModel();
72
 
73
        $retrievedModel = $this->newModelQuery($model)->where(
74
            $model->getAuthIdentifierName(), $identifier
75
        )->first();
76
 
77
        if (! $retrievedModel) {
78
            return;
79
        }
80
 
81
        $rememberToken = $retrievedModel->getRememberToken();
82
 
83
        return $rememberToken && hash_equals($rememberToken, $token) ? $retrievedModel : null;
84
    }
85
 
86
    /**
87
     * Update the "remember me" token for the given user in storage.
88
     *
89
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
90
     * @param  string  $token
91
     * @return void
92
     */
93
    public function updateRememberToken(UserContract $user, $token)
94
    {
95
        $user->setRememberToken($token);
96
 
97
        $timestamps = $user->timestamps;
98
 
99
        $user->timestamps = false;
100
 
101
        $user->save();
102
 
103
        $user->timestamps = $timestamps;
104
    }
105
 
106
    /**
107
     * Retrieve a user by the given credentials.
108
     *
109
     * @param  array  $credentials
110
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
111
     */
112
    public function retrieveByCredentials(array $credentials)
113
    {
114
        $credentials = array_filter(
115
            $credentials,
116
            fn ($key) => ! str_contains($key, 'password'),
117
            ARRAY_FILTER_USE_KEY
118
        );
119
 
120
        if (empty($credentials)) {
121
            return;
122
        }
123
 
124
        // First we will add each credential element to the query as a where clause.
125
        // Then we can execute the query and, if we found a user, return it in a
126
        // Eloquent User "model" that will be utilized by the Guard instances.
127
        $query = $this->newModelQuery();
128
 
129
        foreach ($credentials as $key => $value) {
130
            if (is_array($value) || $value instanceof Arrayable) {
131
                $query->whereIn($key, $value);
132
            } elseif ($value instanceof Closure) {
133
                $value($query);
134
            } else {
135
                $query->where($key, $value);
136
            }
137
        }
138
 
139
        return $query->first();
140
    }
141
 
142
    /**
143
     * Validate a user against the given credentials.
144
     *
145
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
146
     * @param  array  $credentials
147
     * @return bool
148
     */
149
    public function validateCredentials(UserContract $user, array $credentials)
150
    {
151
        if (is_null($plain = $credentials['password'])) {
152
            return false;
153
        }
154
 
155
        return $this->hasher->check($plain, $user->getAuthPassword());
156
    }
157
 
158
    /**
159
     * Get a new query builder for the model instance.
160
     *
161
     * @param  \Illuminate\Database\Eloquent\Model|null  $model
162
     * @return \Illuminate\Database\Eloquent\Builder
163
     */
164
    protected function newModelQuery($model = null)
165
    {
166
        $query = is_null($model)
167
                ? $this->createModel()->newQuery()
168
                : $model->newQuery();
169
 
170
        with($query, $this->queryCallback);
171
 
172
        return $query;
173
    }
174
 
175
    /**
176
     * Create a new instance of the model.
177
     *
178
     * @return \Illuminate\Database\Eloquent\Model
179
     */
180
    public function createModel()
181
    {
182
        $class = '\\'.ltrim($this->model, '\\');
183
 
184
        return new $class;
185
    }
186
 
187
    /**
188
     * Gets the hasher implementation.
189
     *
190
     * @return \Illuminate\Contracts\Hashing\Hasher
191
     */
192
    public function getHasher()
193
    {
194
        return $this->hasher;
195
    }
196
 
197
    /**
198
     * Sets the hasher implementation.
199
     *
200
     * @param  \Illuminate\Contracts\Hashing\Hasher  $hasher
201
     * @return $this
202
     */
203
    public function setHasher(HasherContract $hasher)
204
    {
205
        $this->hasher = $hasher;
206
 
207
        return $this;
208
    }
209
 
210
    /**
211
     * Gets the name of the Eloquent user model.
212
     *
213
     * @return string
214
     */
215
    public function getModel()
216
    {
217
        return $this->model;
218
    }
219
 
220
    /**
221
     * Sets the name of the Eloquent user model.
222
     *
223
     * @param  string  $model
224
     * @return $this
225
     */
226
    public function setModel($model)
227
    {
228
        $this->model = $model;
229
 
230
        return $this;
231
    }
232
 
233
    /**
234
     * Get the callback that modifies the query before retrieving users.
235
     *
236
     * @return \Closure|null
237
     */
238
    public function getQueryCallback()
239
    {
240
        return $this->queryCallback;
241
    }
242
 
243
    /**
244
     * Sets the callback to modify the query before retrieving users.
245
     *
246
     * @param  (\Closure(\Illuminate\Database\Eloquent\Builder):mixed)|null  $queryCallback
247
     * @return $this
248
     */
249
    public function withQuery($queryCallback = null)
250
    {
251
        $this->queryCallback = $queryCallback;
252
 
253
        return $this;
254
    }
255
}