|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BeyondCode\EmailConfirmation\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use BeyondCode\EmailConfirmation\Events\Confirmed; |
|
6
|
|
|
use Illuminate\Http\Request; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Illuminate\Auth\Events\Registered; |
|
9
|
|
|
|
|
10
|
|
|
trait RegistersUsers |
|
11
|
|
|
{ |
|
12
|
|
|
use \Illuminate\Foundation\Auth\RegistersUsers { |
|
13
|
|
|
register as baseRegister; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Get redirect path after a successful confirmation. |
|
18
|
|
|
* |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
|
View Code Duplication |
public function redirectAfterConfirmationPath() |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
if (method_exists($this, 'redirectConfirmationTo')) { |
|
24
|
|
|
return $this->redirectConfirmationTo(); |
|
|
|
|
|
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return property_exists($this, 'redirectConfirmationTo') ? $this->redirectConfirmationTo : route('login'); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get redirect path after a registration that still needs to be confirmed. |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
View Code Duplication |
public function redirectAfterRegistrationPath() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
if (method_exists($this, 'redirectAfterRegistrationTo')) { |
|
38
|
|
|
return $this->redirectAfterRegistrationTo(); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return property_exists($this, 'redirectAfterRegistrationTo') ? $this->redirectAfterRegistrationTo : route('login'); |
|
|
|
|
|
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get redirect path after the confirmation was sent. |
|
46
|
|
|
* |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
View Code Duplication |
public function redirectAfterResendConfirmationPath() |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
if (method_exists($this, 'redirectAfterResendConfirmationTo')) { |
|
52
|
|
|
return $this->redirectAfterResendConfirmationTo(); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return property_exists($this, 'redirectAfterResendConfirmationTo') ? $this->redirectAfterResendConfirmationTo : route('login'); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Confirm a user with a given confirmation code. |
|
60
|
|
|
* |
|
61
|
|
|
* @param $confirmation_code |
|
62
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
63
|
|
|
*/ |
|
64
|
|
|
public function confirm($confirmation_code) |
|
65
|
|
|
{ |
|
66
|
|
|
$model = $this->guard()->getProvider()->createModel(); |
|
67
|
|
|
|
|
68
|
|
|
$user = $model->where('confirmation_code', $confirmation_code)->firstOrFail(); |
|
69
|
|
|
|
|
70
|
|
|
$user->confirmation_code = null; |
|
71
|
|
|
$user->confirmed_at = now(); |
|
72
|
|
|
$user->save(); |
|
73
|
|
|
|
|
74
|
|
|
event(new Confirmed($user)); |
|
75
|
|
|
|
|
76
|
|
|
return $this->confirmed($user) |
|
77
|
|
|
?: redirect($this->redirectAfterConfirmationPath())->with('confirmation', __('confirmation::confirmation.confirmation_successful')); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Resend a confirmation code to a user. |
|
82
|
|
|
* |
|
83
|
|
|
* @param \Illuminate\Http\Request $request |
|
84
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
85
|
|
|
*/ |
|
86
|
|
|
public function resendConfirmation(Request $request) |
|
87
|
|
|
{ |
|
88
|
|
|
$model = $this->guard()->getProvider()->createModel(); |
|
89
|
|
|
|
|
90
|
|
|
$user = $model->findOrFail($request->session()->pull('confirmation_user_id')); |
|
|
|
|
|
|
91
|
|
|
$this->sendConfirmationToUser($user); |
|
92
|
|
|
|
|
93
|
|
|
return redirect($this->redirectAfterResendConfirmationPath())->with('confirmation', __('confirmation::confirmation.confirmation_resent')); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Handle a registration request for the application. |
|
98
|
|
|
* |
|
99
|
|
|
* @param \Illuminate\Http\Request $request |
|
100
|
|
|
* @return \Illuminate\Http\Response |
|
101
|
|
|
*/ |
|
102
|
|
|
public function register(Request $request) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->validator($request->all())->validate(); |
|
|
|
|
|
|
105
|
|
|
|
|
106
|
|
|
event(new Registered($user = $this->create($request->all()))); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
$this->sendConfirmationToUser($user); |
|
109
|
|
|
|
|
110
|
|
|
return $this->registered($request, $user) |
|
111
|
|
|
?: redirect($this->redirectAfterRegistrationPath())->with('confirmation', __('confirmation::confirmation.confirmation_info')); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Send the confirmation code to a user. |
|
116
|
|
|
* |
|
117
|
|
|
* @param $user |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function sendConfirmationToUser($user) |
|
120
|
|
|
{ |
|
121
|
|
|
// Create the confirmation code |
|
122
|
|
|
$user->confirmation_code = Str::random(25); |
|
123
|
|
|
$user->save(); |
|
124
|
|
|
|
|
125
|
|
|
// Notify the user |
|
126
|
|
|
$notification = app(config('confirmation.notification')); |
|
127
|
|
|
$user->notify($notification); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* The users email address has been confirmed. |
|
133
|
|
|
* |
|
134
|
|
|
* @param mixed $user |
|
135
|
|
|
* @return mixed |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function confirmed($user) { |
|
|
|
|
|
|
138
|
|
|
// |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.