1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* UserEventHandler.php |
4
|
|
|
* Copyright (c) 2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace FireflyIII\Handlers\Events; |
24
|
|
|
|
25
|
|
|
use FireflyIII\Events\RegisteredUser; |
26
|
|
|
use FireflyIII\Events\RequestedNewPassword; |
27
|
|
|
use FireflyIII\Events\UserChangedEmail; |
28
|
|
|
use FireflyIII\Mail\ConfirmEmailChangeMail; |
29
|
|
|
use FireflyIII\Mail\RegisteredUser as RegisteredUserMail; |
30
|
|
|
use FireflyIII\Mail\RequestedNewPassword as RequestedNewPasswordMail; |
31
|
|
|
use FireflyIII\Mail\UndoEmailChangeMail; |
32
|
|
|
use FireflyIII\Models\Role; |
33
|
|
|
use FireflyIII\Repositories\User\UserRepositoryInterface; |
34
|
|
|
use FireflyIII\User; |
35
|
|
|
use Illuminate\Auth\Events\Login; |
36
|
|
|
use Log; |
37
|
|
|
use Mail; |
38
|
|
|
use Preferences; |
39
|
|
|
use Swift_TransportException; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Class UserEventHandler. |
43
|
|
|
* |
44
|
|
|
* This class responds to any events that have anything to do with the User object. |
45
|
|
|
* |
46
|
|
|
* The method name reflects what is being done. This is in the present tense. |
47
|
|
|
*/ |
48
|
|
|
class UserEventHandler |
49
|
|
|
{ |
50
|
|
|
/** |
51
|
|
|
* This method will bestow upon a user the "owner" role if he is the first user in the system. |
52
|
|
|
* |
53
|
|
|
* @param RegisteredUser $event |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
|
|
public function attachUserRole(RegisteredUser $event): bool |
58
|
|
|
{ |
59
|
|
|
/** @var UserRepositoryInterface $repository */ |
60
|
|
|
$repository = app(UserRepositoryInterface::class); |
61
|
|
|
|
62
|
|
|
// first user ever? |
63
|
|
|
if (1 === $repository->count()) { |
64
|
|
|
$repository->attachRole($event->user, 'owner'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return true; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Login $event |
72
|
|
|
* |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function checkSingleUserIsAdmin(Login $event): bool |
76
|
|
|
{ |
77
|
|
|
Log::debug('In checkSingleUserIsAdmin'); |
78
|
|
|
|
79
|
|
|
$user = $event->user; |
80
|
|
|
$count = User::count(); |
81
|
|
|
|
82
|
|
|
if ($count > 1) { |
83
|
|
|
// if more than one user, do nothing. |
84
|
|
|
Log::debug(sprintf('System has %d users, will not change users roles.', $count)); |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
// user is only user but has admin role |
89
|
|
|
if ($count === 1 && $user->hasRole('owner')) { |
|
|
|
|
90
|
|
|
Log::debug(sprintf('User #%d is only user but has role owner so all is well.', $user->id)); |
|
|
|
|
91
|
|
|
|
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
// user is the only user but does not have role "owner". |
95
|
|
|
$role = Role::where('name', 'owner')->first(); |
96
|
|
|
if (is_null($role)) { |
97
|
|
|
// create role, does not exist. Very strange situation so let's raise a big fuss about it. |
98
|
|
|
$role = Role::create(['name' => 'owner', 'display_name' => 'Site Owner', 'description' => 'User runs this instance of FF3']); |
99
|
|
|
Log::error('Could not find role "owner". This is weird.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
Log::info(sprintf('Gave user #%d role #%d ("%s")', $user->id, $role->id, $role->name)); |
|
|
|
|
103
|
|
|
// give user the role |
104
|
|
|
$user->attachRole($role); |
|
|
|
|
105
|
|
|
$user->save(); |
|
|
|
|
106
|
|
|
|
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param UserChangedEmail $event |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
public function sendEmailChangeConfirmMail(UserChangedEmail $event): bool |
116
|
|
|
{ |
117
|
|
|
$newEmail = $event->newEmail; |
118
|
|
|
$oldEmail = $event->oldEmail; |
119
|
|
|
$user = $event->user; |
120
|
|
|
$ipAddress = $event->ipAddress; |
121
|
|
|
$token = Preferences::getForUser($user, 'email_change_confirm_token', 'invalid'); |
122
|
|
|
$uri = route('profile.confirm-email-change', [$token->data]); |
123
|
|
|
try { |
124
|
|
|
Mail::to($newEmail)->send(new ConfirmEmailChangeMail($newEmail, $oldEmail, $uri, $ipAddress)); |
125
|
|
|
// @codeCoverageIgnoreStart |
126
|
|
|
} catch (Swift_TransportException $e) { |
127
|
|
|
Log::error($e->getMessage()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// @codeCoverageIgnoreEnd |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param UserChangedEmail $event |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function sendEmailChangeUndoMail(UserChangedEmail $event): bool |
140
|
|
|
{ |
141
|
|
|
$newEmail = $event->newEmail; |
142
|
|
|
$oldEmail = $event->oldEmail; |
143
|
|
|
$user = $event->user; |
144
|
|
|
$ipAddress = $event->ipAddress; |
145
|
|
|
$token = Preferences::getForUser($user, 'email_change_undo_token', 'invalid'); |
146
|
|
|
$uri = route('profile.undo-email-change', [$token->data, hash('sha256', $oldEmail)]); |
147
|
|
|
try { |
148
|
|
|
Mail::to($oldEmail)->send(new UndoEmailChangeMail($newEmail, $oldEmail, $uri, $ipAddress)); |
149
|
|
|
// @codeCoverageIgnoreStart |
150
|
|
|
} catch (Swift_TransportException $e) { |
151
|
|
|
Log::error($e->getMessage()); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
// @codeCoverageIgnoreEnd |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param RequestedNewPassword $event |
160
|
|
|
* |
161
|
|
|
* @return bool |
162
|
|
|
*/ |
163
|
|
|
public function sendNewPassword(RequestedNewPassword $event): bool |
164
|
|
|
{ |
165
|
|
|
$email = $event->user->email; |
166
|
|
|
$ipAddress = $event->ipAddress; |
167
|
|
|
$token = $event->token; |
168
|
|
|
|
169
|
|
|
$url = route('password.reset', [$token]); |
170
|
|
|
|
171
|
|
|
// send email. |
172
|
|
|
try { |
173
|
|
|
Mail::to($email)->send(new RequestedNewPasswordMail($url, $ipAddress)); |
174
|
|
|
// @codeCoverageIgnoreStart |
175
|
|
|
} catch (Swift_TransportException $e) { |
176
|
|
|
Log::error($e->getMessage()); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
// @codeCoverageIgnoreEnd |
180
|
|
|
|
181
|
|
|
return true; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* This method will send the user a registration mail, welcoming him or her to Firefly III. |
186
|
|
|
* This message is only sent when the configuration of Firefly III says so. |
187
|
|
|
* |
188
|
|
|
* @param RegisteredUser $event |
189
|
|
|
* |
190
|
|
|
* @return bool |
191
|
|
|
*/ |
192
|
|
|
public function sendRegistrationMail(RegisteredUser $event) |
193
|
|
|
{ |
194
|
|
|
$sendMail = env('SEND_REGISTRATION_MAIL', true); |
195
|
|
|
if (!$sendMail) { |
196
|
|
|
return true; // @codeCoverageIgnore |
197
|
|
|
} |
198
|
|
|
// get the email address |
199
|
|
|
$email = $event->user->email; |
200
|
|
|
$uri = route('index'); |
201
|
|
|
$ipAddress = $event->ipAddress; |
202
|
|
|
|
203
|
|
|
// send email. |
204
|
|
|
try { |
205
|
|
|
Mail::to($email)->send(new RegisteredUserMail($uri, $ipAddress)); |
206
|
|
|
// @codeCoverageIgnoreStart |
207
|
|
|
} catch (Swift_TransportException $e) { |
208
|
|
|
Log::error($e->getMessage()); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// @codeCoverageIgnoreEnd |
212
|
|
|
|
213
|
|
|
return true; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: