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
|
|
|
* Get the validation rules that apply to the request. |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function rules() |
27
|
|
|
{ |
28
|
|
|
return [ |
29
|
|
|
'first_name' => ['required', 'min:2'], |
30
|
|
|
'last_name' => ['required', 'min:2'], |
31
|
|
|
'roles' => $this->getRolesRule(), |
32
|
|
|
]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Sanitize all inputs. |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
protected function sanitize() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
'username' => $this->sanitizeUsername(), |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/* ----------------------------------------------------------------- |
48
|
|
|
| Other Methods |
49
|
|
|
| ----------------------------------------------------------------- |
50
|
|
|
*/ |
51
|
|
|
/** |
52
|
|
|
* Sanitize the username. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
protected function sanitizeUsername() |
57
|
|
|
{ |
58
|
|
|
$username = $this->has('username') |
59
|
|
|
? $this->get('username') |
60
|
|
|
: Str::limit($this->get('first_name'), 1, '.').' '.$this->get('last_name'); |
61
|
|
|
|
62
|
|
|
return Str::slug($username, config('arcanesoft.auth.slug-separator', '.')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the email rule. |
67
|
|
|
* |
68
|
|
|
* @param string $column |
69
|
|
|
* |
70
|
|
|
* @return \Illuminate\Validation\Rules\Unique |
71
|
|
|
*/ |
72
|
|
|
protected function getEmailRule($column = 'email') |
73
|
|
|
{ |
74
|
|
|
return Rule::unique($this->getUsersTable(), $column); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the username rule. |
79
|
|
|
* |
80
|
|
|
* @param string $column |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Validation\Rules\Unique |
83
|
|
|
*/ |
84
|
|
|
protected function getUsernameRule($column = 'username') |
85
|
|
|
{ |
86
|
|
|
return Rule::unique($this->getUsersTable(), $column); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the users table name. |
91
|
|
|
* |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
|
|
protected function getUsersTable() |
95
|
|
|
{ |
96
|
|
|
return $this->getPrefixTable().config('arcanesoft.auth.users.table', 'users'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Get the roles rule. |
101
|
|
|
* |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
protected function getRolesRule() |
105
|
|
|
{ |
106
|
|
|
$rolesIds = Cache::remember('auth.roles.ids', 1, function () { |
107
|
|
|
return app(RoleContract::class)->pluck('id'); |
108
|
|
|
}); |
109
|
|
|
|
110
|
|
|
return ['required', 'array', 'min:1', 'in:'.$rolesIds->implode(',')]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|