1
|
|
|
<?php namespace Arcanesoft\Auth\Http\Requests\Admin\Users; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Auth\Http\Requests\FormRequest; |
4
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract; |
5
|
|
|
use Cache; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Illuminate\Validation\Rule; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class UserFormRequest |
11
|
|
|
* |
12
|
|
|
* @package Arcanesoft\Auth\Http\Requests\Admin\Users |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class UserFormRequest extends FormRequest |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Main Methods |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Get the validation rules that apply to the request. |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
public function rules() |
28
|
|
|
{ |
29
|
|
|
return [ |
30
|
|
|
'first_name' => ['required', 'string', 'min:2'], |
31
|
|
|
'last_name' => ['required', 'string', 'min:2'], |
32
|
|
|
'roles' => $this->getRolesRule(), |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Sanitize all inputs. |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
protected function sanitize() |
42
|
|
|
{ |
43
|
|
|
return [ |
44
|
|
|
'username' => $this->sanitizeUsername(), |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get custom attributes for validator errors. |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
public function attributes() |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'roles' => trans('auth::roles.titles.roles') |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/* ----------------------------------------------------------------- |
61
|
|
|
| Other Methods |
62
|
|
|
| ----------------------------------------------------------------- |
63
|
|
|
*/ |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Sanitize the username. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
protected function sanitizeUsername() |
71
|
|
|
{ |
72
|
|
|
$username = $this->has('username') |
73
|
|
|
? $this->get('username') |
74
|
|
|
: $this->get('first_name').' '.$this->get('last_name'); |
75
|
|
|
|
76
|
|
|
return Str::slug($username, config('arcanesoft.auth.slug-separator', '.')); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the email rule. |
81
|
|
|
* |
82
|
|
|
* @param string $column |
83
|
|
|
* |
84
|
|
|
* @return \Illuminate\Validation\Rules\Unique |
85
|
|
|
*/ |
86
|
|
|
protected function getEmailRule($column = 'email') |
87
|
|
|
{ |
88
|
|
|
return Rule::unique($this->getUsersTable(), $column); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the username rule. |
93
|
|
|
* |
94
|
|
|
* @param string $column |
95
|
|
|
* |
96
|
|
|
* @return \Illuminate\Validation\Rules\Unique |
97
|
|
|
*/ |
98
|
|
|
protected function getUsernameRule($column = 'username') |
99
|
|
|
{ |
100
|
|
|
return Rule::unique($this->getUsersTable(), $column); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get the users table name. |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function getUsersTable() |
109
|
|
|
{ |
110
|
|
|
return $this->getPrefixTable().config('arcanesoft.auth.users.table', 'users'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the roles rule. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
protected function getRolesRule() |
119
|
|
|
{ |
120
|
|
|
$rolesIds = Cache::remember('auth.roles.ids', 1, function () { |
121
|
|
|
return app(RoleContract::class)->pluck('id'); |
122
|
|
|
}); |
123
|
|
|
|
124
|
|
|
return ['required', 'array', 'min:1', 'in:'.$rolesIds->implode(',')]; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|