1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Http\Requests\Admin; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Auth; |
6
|
|
|
use Illuminate\Support\Facades\Crypt; |
7
|
|
|
use PragmaRX\Google2FA\Google2FA; |
8
|
|
|
|
9
|
|
|
class UserRequest extends Request |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Determines if the user is authorized to make this request. |
13
|
|
|
* |
14
|
|
|
* @return bool |
15
|
|
|
*/ |
16
|
4 |
|
public function authorize() |
17
|
|
|
{ |
18
|
4 |
|
return true; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Gets the validation rules that apply to the request. |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
4 |
|
public function rules() |
27
|
|
|
{ |
28
|
4 |
|
switch ($this->method()) { |
29
|
4 |
|
case 'POST': |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
4 |
|
'name' => 'required', |
33
|
4 |
|
'email' => 'required|email|unique:' . config('twill.users_table', 'twill_users') . ',email', |
34
|
4 |
|
'role' => 'required|not_in:SUPERADMIN', |
35
|
|
|
]; |
36
|
|
|
} |
37
|
1 |
|
case 'PUT': |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
1 |
|
'name' => 'required', |
41
|
1 |
|
'role' => 'not_in:SUPERADMIN', |
42
|
1 |
|
'email' => 'required|email|unique:' . config('twill.users_table', 'twill_users') . ',email,' . $this->route('user'), |
43
|
1 |
|
'verify-code' => function ($attribute, $value, $fail) { |
44
|
|
|
$user = Auth::guard('twill_users')->user(); |
45
|
|
|
$with2faSettings = config('twill.enabled.users-2fa') && $user->id == $this->route('user'); |
|
|
|
|
46
|
|
|
|
47
|
|
|
if ($with2faSettings) { |
48
|
|
|
$userIsEnabling = $this->get('google_2fa_enabled') && !$user->google_2fa_enabled; |
|
|
|
|
49
|
|
|
$userIsDisabling = !$this->get('google_2fa_enabled') && $user->google_2fa_enabled; |
50
|
|
|
|
51
|
|
|
$shouldValidateOTP = $userIsEnabling || $userIsDisabling; |
52
|
|
|
|
53
|
|
|
if ($shouldValidateOTP) { |
54
|
|
|
$valid = (new Google2FA)->verifyKey($user->google_2fa_secret, $value ?? ''); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if (!$valid) { |
57
|
|
|
$fail('Your one time password is invalid.'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
1 |
|
}, |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
default:break; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return []; |
68
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|