|
1
|
|
|
<?php |
|
2
|
|
|
namespace Xetaravel\Models\Validators; |
|
3
|
|
|
|
|
4
|
|
|
use Illuminate\Support\Facades\App; |
|
5
|
|
|
use Illuminate\Support\Facades\Validator as FacadeValidator; |
|
6
|
|
|
use Illuminate\Validation\Rule; |
|
7
|
|
|
use Illuminate\Validation\Validator; |
|
8
|
|
|
use Ultraware\Roles\Models\Role; |
|
9
|
|
|
|
|
10
|
|
|
class UserValidator |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Get the validator for an incoming registration request. |
|
14
|
|
|
* |
|
15
|
|
|
* @param array $data The data to validate. |
|
16
|
|
|
* |
|
17
|
|
|
* @return \Illuminate\Validation\Validator |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function create(array $data): Validator |
|
20
|
|
|
{ |
|
21
|
|
|
$rules = [ |
|
22
|
|
|
'username' => 'required|alpha_num|min:4|max:20|unique:users', |
|
23
|
|
|
'email' => 'required|email|max:50|unique:users', |
|
24
|
|
|
'password' => 'required|min:6|confirmed', |
|
25
|
|
|
'terms' => 'required|min:1' |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
// Bipass the captcha for the unit testing. |
|
29
|
|
|
if (App::environment() !== 'testing') { |
|
30
|
|
|
$rules = array_merge($rules, ['g-recaptcha-response' => 'required|recaptcha']); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return FacadeValidator::make($data, $rules); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Get a validator for an incoming update request. (Administration) |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $data The data to validate. |
|
40
|
|
|
* @param int $id The actual user id to ignore the username rule. |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Illuminate\Validation\Validator |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function update(array $data, int $id): Validator |
|
45
|
|
|
{ |
|
46
|
|
|
$rules = [ |
|
47
|
|
|
'username' => [ |
|
48
|
|
|
'required', |
|
49
|
|
|
'alpha_num', |
|
50
|
|
|
'min:4', |
|
51
|
|
|
'max:20', |
|
52
|
|
|
Rule::unique('users')->ignore($id) |
|
53
|
|
|
], |
|
54
|
|
|
'email' => [ |
|
55
|
|
|
'required', |
|
56
|
|
|
'email', |
|
57
|
|
|
'max:50', |
|
58
|
|
|
Rule::unique('users')->ignore($id) |
|
59
|
|
|
], |
|
60
|
|
|
'account.first_name' => 'max:100', |
|
61
|
|
|
'account.last_name' => 'max:100', |
|
62
|
|
|
'account.facebook' => 'max:50', |
|
63
|
|
|
'account.twitter' => 'max:50', |
|
64
|
|
|
'roles' => 'required' |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
return FacadeValidator::make($data, $rules); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the validator for an incoming email update request. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $data The data to validate. |
|
74
|
|
|
* |
|
75
|
|
|
* @return \Illuminate\Validation\Validator |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function updateEmail(array $data): Validator |
|
78
|
|
|
{ |
|
79
|
|
|
$rules = [ |
|
80
|
|
|
'email' => 'required|email|max:50|unique:users' |
|
81
|
|
|
]; |
|
82
|
|
|
|
|
83
|
|
|
return FacadeValidator::make($data, $rules); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Get the validator for an incoming password update request. |
|
88
|
|
|
* |
|
89
|
|
|
* @param array $data The data to validate. |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Illuminate\Validation\Validator |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function updatePassword(array $data): Validator |
|
94
|
|
|
{ |
|
95
|
|
|
$rules = [ |
|
96
|
|
|
'oldpassword' => 'required', |
|
97
|
|
|
'password' => 'required|min:6|confirmed' |
|
98
|
|
|
]; |
|
99
|
|
|
|
|
100
|
|
|
return FacadeValidator::make($data, $rules); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|