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\Passwords;
4
 
5
use Closure;
6
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
7
use Illuminate\Contracts\Auth\PasswordBroker as PasswordBrokerContract;
8
use Illuminate\Contracts\Auth\UserProvider;
9
use Illuminate\Support\Arr;
10
use UnexpectedValueException;
11
 
12
class PasswordBroker implements PasswordBrokerContract
13
{
14
    /**
15
     * The password token repository.
16
     *
17
     * @var \Illuminate\Auth\Passwords\TokenRepositoryInterface
18
     */
19
    protected $tokens;
20
 
21
    /**
22
     * The user provider implementation.
23
     *
24
     * @var \Illuminate\Contracts\Auth\UserProvider
25
     */
26
    protected $users;
27
 
28
    /**
29
     * Create a new password broker instance.
30
     *
31
     * @param  \Illuminate\Auth\Passwords\TokenRepositoryInterface  $tokens
32
     * @param  \Illuminate\Contracts\Auth\UserProvider  $users
33
     * @return void
34
     */
35
    public function __construct(TokenRepositoryInterface $tokens, UserProvider $users)
36
    {
37
        $this->users = $users;
38
        $this->tokens = $tokens;
39
    }
40
 
41
    /**
42
     * Send a password reset link to a user.
43
     *
44
     * @param  array  $credentials
45
     * @param  \Closure|null  $callback
46
     * @return string
47
     */
48
    public function sendResetLink(array $credentials, Closure $callback = null)
49
    {
50
        // First we will check to see if we found a user at the given credentials and
51
        // if we did not we will redirect back to this current URI with a piece of
52
        // "flash" data in the session to indicate to the developers the errors.
53
        $user = $this->getUser($credentials);
54
 
55
        if (is_null($user)) {
56
            return static::INVALID_USER;
57
        }
58
 
59
        if ($this->tokens->recentlyCreatedToken($user)) {
60
            return static::RESET_THROTTLED;
61
        }
62
 
63
        $token = $this->tokens->create($user);
64
 
65
        if ($callback) {
66
            $callback($user, $token);
67
        } else {
68
            // Once we have the reset token, we are ready to send the message out to this
69
            // user with a link to reset their password. We will then redirect back to
70
            // the current URI having nothing set in the session to indicate errors.
71
            $user->sendPasswordResetNotification($token);
72
        }
73
 
74
        return static::RESET_LINK_SENT;
75
    }
76
 
77
    /**
78
     * Reset the password for the given token.
79
     *
80
     * @param  array  $credentials
81
     * @param  \Closure  $callback
82
     * @return mixed
83
     */
84
    public function reset(array $credentials, Closure $callback)
85
    {
86
        $user = $this->validateReset($credentials);
87
 
88
        // If the responses from the validate method is not a user instance, we will
89
        // assume that it is a redirect and simply return it from this method and
90
        // the user is properly redirected having an error message on the post.
91
        if (! $user instanceof CanResetPasswordContract) {
92
            return $user;
93
        }
94
 
95
        $password = $credentials['password'];
96
 
97
        // Once the reset has been validated, we'll call the given callback with the
98
        // new password. This gives the user an opportunity to store the password
99
        // in their persistent storage. Then we'll delete the token and return.
100
        $callback($user, $password);
101
 
102
        $this->tokens->delete($user);
103
 
104
        return static::PASSWORD_RESET;
105
    }
106
 
107
    /**
108
     * Validate a password reset for the given credentials.
109
     *
110
     * @param  array  $credentials
111
     * @return \Illuminate\Contracts\Auth\CanResetPassword|string
112
     */
113
    protected function validateReset(array $credentials)
114
    {
115
        if (is_null($user = $this->getUser($credentials))) {
116
            return static::INVALID_USER;
117
        }
118
 
119
        if (! $this->tokens->exists($user, $credentials['token'])) {
120
            return static::INVALID_TOKEN;
121
        }
122
 
123
        return $user;
124
    }
125
 
126
    /**
127
     * Get the user for the given credentials.
128
     *
129
     * @param  array  $credentials
130
     * @return \Illuminate\Contracts\Auth\CanResetPassword|null
131
     *
132
     * @throws \UnexpectedValueException
133
     */
134
    public function getUser(array $credentials)
135
    {
136
        $credentials = Arr::except($credentials, ['token']);
137
 
138
        $user = $this->users->retrieveByCredentials($credentials);
139
 
140
        if ($user && ! $user instanceof CanResetPasswordContract) {
141
            throw new UnexpectedValueException('User must implement CanResetPassword interface.');
142
        }
143
 
144
        return $user;
145
    }
146
 
147
    /**
148
     * Create a new password reset token for the given user.
149
     *
150
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
151
     * @return string
152
     */
153
    public function createToken(CanResetPasswordContract $user)
154
    {
155
        return $this->tokens->create($user);
156
    }
157
 
158
    /**
159
     * Delete password reset tokens of the given user.
160
     *
161
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
162
     * @return void
163
     */
164
    public function deleteToken(CanResetPasswordContract $user)
165
    {
166
        $this->tokens->delete($user);
167
    }
168
 
169
    /**
170
     * Validate the given password reset token.
171
     *
172
     * @param  \Illuminate\Contracts\Auth\CanResetPassword  $user
173
     * @param  string  $token
174
     * @return bool
175
     */
176
    public function tokenExists(CanResetPasswordContract $user, $token)
177
    {
178
        return $this->tokens->exists($user, $token);
179
    }
180
 
181
    /**
182
     * Get the password reset token repository implementation.
183
     *
184
     * @return \Illuminate\Auth\Passwords\TokenRepositoryInterface
185
     */
186
    public function getRepository()
187
    {
188
        return $this->tokens;
189
    }
190
}