UserRepository.php
9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
namespace App\Repositories\Frontend\Access\User;
use App\Access\Model\User\User;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Repositories\BaseRepository;
use Illuminate\Support\Facades\Hash;
use App\Access\Model\User\SocialLogin;
use App\Events\Frontend\Auth\UserConfirmed;
use App\Access\Repository\Role\RoleRepository;
use App\Notifications\Frontend\Auth\UserNeedsConfirmation;
/**
* Class UserRepository.
*/
class UserRepository extends BaseRepository
{
/**
* Associated Repository Model.
*/
const MODEL = User::class;
/**
* @var RoleRepository
*/
protected $role;
/**
* @param RoleRepository $role
*/
public function __construct(RoleRepository $role)
{
$this->role = $role;
}
/**
* @param $email
*
* @return mixed
*/
public function findByEmail($email)
{
return $this->query()->where('email', $email)->first();
}
/**
* @param $token
*
* @throws GeneralException
*
* @return mixed
*/
public function findByConfirmationToken($token)
{
return $this->query()->where('confirmation_code', $token)->first();
}
/**
* @param $token
*
* @return mixed
*/
public function findByPasswordResetToken($token)
{
foreach (DB::table(config('auth.passwords.users.table'))->get() as $row) {
if (password_verify($token, $row->token)) {
return $this->findByEmail($row->email);
}
}
return false;
}
/**
* @param $token
*
* @throws GeneralException
*
* @return mixed
*/
public function getEmailForPasswordToken($token)
{
$rows = DB::table(config('auth.passwords.users.table'))->get();
foreach ($rows as $row) {
if (password_verify($token, $row->token)) {
return $row->email;
}
}
throw new GeneralException(trans('auth.unknown'));
}
/**
* @param array $data
* @param bool $provider
*
* @return static
*/
public function create(array $data, $provider = false)
{
$user = self::MODEL;
$user = new $user;
$user->first_name = $data['first_name'];
$user->last_name = $data['last_name'];
$user->email = $data['email'];
$user->confirmation_code = md5(uniqid(mt_rand(), true));
$user->status = 1;
$user->password = $provider ? null : bcrypt($data['password']);
$confirm = false; // Whether or not they get an e-mail to confirm their account
// If users require approval, confirmed is false regardless of account type
if (config('access.users.requires_approval')) {
$user->confirmed = 0; // No confirm e-mail sent, that defeats the purpose of manual approval
} elseif (config('access.users.confirm_email')) { // If user must confirm email
// If user is from social, already confirmed
if ($provider) {
$user->confirmed = 1; // E-mails are validated through the social platform
} else {
// Otherwise needs confirmation
$user->confirmed = 0;
$confirm = true;
}
} else {
// Otherwise both are off and confirmed is default
$user->confirmed = 1;
}
DB::transaction(function () use ($user) {
if ($user->save()) {
/*
* Add the default site role to the new user
*/
$user->attachRole($this->role->getDefaultUserRole());
}
});
/*
* If users have to confirm their email and this is not a social account,
* and the account does not require admin approval
* send the confirmation email
*
* If this is a social account they are confirmed through the social provider by default
*/
if ($confirm) {
// Pretty much only if account approval is off, confirm email is on, and this isn't a social account.
$user->notify(new UserNeedsConfirmation($user->confirmation_code));
}
/*
* Return the user object
*/
return $user;
}
/**
* @param $data
* @param $provider
*
* @return UserRepository|bool
* @throws GeneralException
*/
public function findOrCreateSocial($data, $provider)
{
// User email may not provided.
$user_email = $data->email ?: "{$data->id}@{$provider}.com";
// Check to see if there is a user with this email first.
$user = $this->findByEmail($user_email);
/*
* If the user does not exist create them
* The true flag indicate that it is a social account
* Which triggers the script to use some default values in the create method
*/
if (! $user) {
// Registration is not enabled
if (! config('access.users.registration')) {
throw new GeneralException(trans('exceptions.frontend.auth.registration_disabled'));
}
// Get users first name and last name from their full name
$nameParts = $this->getNameParts($data->getName());
$user = $this->create([
'first_name' => $nameParts['first_name'],
'last_name' => $nameParts['last_name'],
'email' => $user_email,
], true);
}
// See if the user has logged in with this social account before
if (! $user->hasProvider($provider)) {
// Gather the provider data for saving and associate it with the user
$user->providers()->save(new SocialLogin([
'provider' => $provider,
'provider_id' => $data->id,
'token' => $data->token,
'avatar' => $data->avatar,
]));
} else {
// Update the users information, token and avatar can be updated.
$user->providers()->update([
'token' => $data->token,
'avatar' => $data->avatar,
]);
}
// Return the user object
return $user;
}
/**
* @param $token
*
* @throws GeneralException
*
* @return bool
*/
public function confirmAccount($token)
{
$user = $this->findByConfirmationToken($token);
if ($user->confirmed == 1) {
throw new GeneralException(trans('exceptions.frontend.auth.confirmation.already_confirmed'));
}
if ($user->confirmation_code == $token) {
$user->confirmed = 1;
event(new UserConfirmed($user));
return $user->save();
}
throw new GeneralException(trans('exceptions.frontend.auth.confirmation.mismatch'));
}
/**
* @param $id
* @param $input
*
* @throws GeneralException
*
* @return mixed
*/
public function updateProfile($id, $input)
{
$user = $this->find($id);
$user->first_name = $input['first_name'];
$user->last_name = $input['last_name'];
if ($user->canChangeEmail()) {
//Address is not current address
if ($user->email != $input['email']) {
//Emails have to be unique
if ($this->findByEmail($input['email'])) {
throw new GeneralException(trans('exceptions.frontend.auth.email_taken'));
}
// Force the user to re-verify his email address
$user->confirmation_code = md5(uniqid(mt_rand(), true));
$user->confirmed = 0;
$user->email = $input['email'];
$updated = $user->save();
// Send the new confirmation e-mail
$user->notify(new UserNeedsConfirmation($user->confirmation_code));
return [
'success' => $updated,
'email_changed' => true,
];
}
}
return $user->save();
}
/**
* @param $input
*
* @throws GeneralException
*
* @return mixed
*/
public function changePassword($input)
{
$user = $this->find(access()->id());
if (Hash::check($input['old_password'], $user->password)) {
$user->password = bcrypt($input['password']);
return $user->save();
}
throw new GeneralException(trans('exceptions.frontend.auth.password.change_mismatch'));
}
/**
* @param $fullName
*
* @return array
*/
protected function getNameParts($fullName)
{
$parts = array_values(array_filter(explode(' ', $fullName)));
$size = count($parts);
$result = [];
if (empty($parts)) {
$result['first_name'] = null;
$result['last_name'] = null;
}
if (! empty($parts) && $size == 1) {
$result['first_name'] = $parts[0];
$result['last_name'] = null;
}
if (! empty($parts) && $size >= 2) {
$result['first_name'] = $parts[0];
$result['last_name'] = $parts[1];
}
return $result;
}
}