1
|
|
|
<?php namespace Arcanesoft\Auth\Http\Requests\Backend\Users; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Auth\Bases\FormRequest; |
4
|
|
|
use Illuminate\Support\Facades\Cache; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class UpdateUserRequest |
8
|
|
|
* |
9
|
|
|
* @package Arcanesoft\Auth\Http\Requests\Backend\Users |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class UpdateUserRequest extends FormRequest |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* Role validation rules. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $rules = [ |
24
|
|
|
'username' => 'required|min:3', |
25
|
|
|
'email' => 'required|email', |
26
|
|
|
'first_name' => 'required|min:2', |
27
|
|
|
'last_name' => 'required|min:2', |
28
|
|
|
'password' => 'required_with:password_confirmation|min:8|confirmed', |
29
|
|
|
'password_confirmation' => 'required_with:password|min:8', |
30
|
|
|
'roles' => 'required|array|min:1', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/* ------------------------------------------------------------------------------------------------ |
34
|
|
|
| Main Functions |
35
|
|
|
| ------------------------------------------------------------------------------------------------ |
36
|
|
|
*/ |
37
|
|
|
/** |
38
|
|
|
* Determine if the user is authorized to make this request. |
39
|
|
|
* |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function authorize() |
43
|
|
|
{ |
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the validation rules that apply to the request. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function rules() |
53
|
|
|
{ |
54
|
|
|
/** @var \Arcanesoft\Contracts\Auth\Models\User $user */ |
55
|
|
|
$user = $this->route('user_id'); |
56
|
|
|
$rules = $this->rules; |
57
|
|
|
|
58
|
|
|
$rules['username'] .= '|unique:users,username,' . $user->id; |
|
|
|
|
59
|
|
|
$rules['email'] .= '|unique:users,email,' . $user->id; |
|
|
|
|
60
|
|
|
$rules['roles'] .= '|in:' . $this->getRoleIds()->implode(','); |
61
|
|
|
|
62
|
|
|
return $rules; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function all() |
69
|
|
|
{ |
70
|
|
|
return array_merge(parent::all(), [ |
71
|
|
|
'username' => str_slug($this->get('username')) |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/* ------------------------------------------------------------------------------------------------ |
76
|
|
|
| Other Functions |
77
|
|
|
| ------------------------------------------------------------------------------------------------ |
78
|
|
|
*/ |
79
|
|
|
/** |
80
|
|
|
* Get the role ids. |
81
|
|
|
* |
82
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
83
|
|
|
*/ |
84
|
|
|
private function getRoleIds() |
85
|
|
|
{ |
86
|
|
|
return Cache::remember('auth.roles.ids', 5, function () { |
87
|
|
|
return app(\Arcanesoft\Contracts\Auth\Models\Role::class)->lists('id'); |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: