1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Itstructure\LaRbac\Http\Requests; |
4
|
|
|
|
5
|
|
|
use Illuminate\Foundation\Http\FormRequest; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class StoreRole |
9
|
|
|
* |
10
|
|
|
* @package Itstructure\LaRbac\Http\Requests |
11
|
|
|
* |
12
|
|
|
* @author Andrey Girnik <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class StoreRole extends FormRequest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Determine if the user is authorized to make this request. |
18
|
|
|
* |
19
|
|
|
* @return bool |
20
|
|
|
*/ |
21
|
|
|
public function authorize() |
22
|
|
|
{ |
23
|
|
|
return !empty(config('rbac.routesMainPermission')) ? $this->user()->can(config('rbac.routesMainPermission')) : true; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the validation rules that apply to the request. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function rules() |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
'name' => 'required|string|regex:/^[\w\s\-]+$/|min:3|max:191|unique:roles', |
35
|
|
|
'description' => 'required|string|min:3|max:191|unique:roles', |
36
|
|
|
'permissions' => 'required|array', |
37
|
|
|
]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get the error messages for the defined validation rules. |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function messages() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
return [ |
48
|
|
|
'required' => __('rbac::validation.required'), |
49
|
|
|
'string' => __('rbac::validation.string'), |
50
|
|
|
'regex' => __('rbac::validation.regex'), |
51
|
|
|
'min' => __('rbac::validation.min'), |
52
|
|
|
'max' => __('rbac::validation.max'), |
53
|
|
|
'unique' => __('rbac::validation.unique'), |
54
|
|
|
]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Get custom attributes for validator errors. |
59
|
|
|
* |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function attributes() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
|
|
'name' => __('rbac::main.name'), |
66
|
|
|
'description' => __('rbac::main.description'), |
67
|
|
|
'permissions' => __('rbac::permissions.permissions'), |
68
|
|
|
]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
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.