1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\LaRbac\Http\Requests; |
4
|
|
|
|
5
|
|
|
use Illuminate\Validation\Rule; |
6
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class UpdateRole |
10
|
|
|
* |
11
|
|
|
* @package Itstructure\LaRbac\Http\Requests |
12
|
|
|
* |
13
|
|
|
* @author Andrey Girnik <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class UpdateRole extends FormRequest |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Determine if the user is authorized to make this request. |
19
|
|
|
* |
20
|
|
|
* @return bool |
21
|
|
|
*/ |
22
|
|
|
public function authorize() |
23
|
|
|
{ |
24
|
|
|
return !empty(config('rbac.routesMainPermission')) ? $this->user()->can(config('rbac.routesMainPermission')) : true; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the validation rules that apply to the request. |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
public function rules() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$id = $this->route('role')->id; |
35
|
|
|
return [ |
36
|
|
|
'name' => [ |
37
|
|
|
'required', |
38
|
|
|
'string', |
39
|
|
|
'regex:/^[\w\s\-]+$/', |
40
|
|
|
'min:3', |
41
|
|
|
'max:191', |
42
|
|
|
Rule::unique('roles')->where('id', '<>', $id), |
|
|
|
|
43
|
|
|
], |
44
|
|
|
'description' => [ |
45
|
|
|
'required', |
46
|
|
|
'string', |
47
|
|
|
'min:3', |
48
|
|
|
'max:191', |
49
|
|
|
Rule::unique('roles')->where('id', '<>', $id), |
|
|
|
|
50
|
|
|
], |
51
|
|
|
'permissions' => 'required|array', |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the error messages for the defined validation rules. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function messages() |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
'required' => __('rbac::validation.required'), |
64
|
|
|
'string' => __('rbac::validation.string'), |
65
|
|
|
'regex' => __('rbac::validation.regex'), |
66
|
|
|
'min' => __('rbac::validation.min'), |
67
|
|
|
'max' => __('rbac::validation.max'), |
68
|
|
|
'unique' => __('rbac::validation.unique'), |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get custom attributes for validator errors. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
public function attributes() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
'name' => __('rbac::main.name'), |
81
|
|
|
'description' => __('rbac::main.description'), |
82
|
|
|
'permissions' => __('rbac::permissions.permissions'), |
83
|
|
|
]; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.