1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Requests; |
4
|
|
|
|
5
|
|
|
use App\User; |
6
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
use Illuminate\Support\Facades\Route; |
9
|
|
|
|
10
|
|
|
class UserRequest extends Request |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
protected $method; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Determine if the user is authorized to make this request. |
17
|
|
|
* |
18
|
|
|
* @return bool |
19
|
|
|
*/ |
20
|
|
|
|
21
|
5 |
|
public function authorize() |
22
|
|
|
{ |
23
|
5 |
|
return true; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Get the validation rules that apply to the request. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
6 |
|
public function rules() |
32
|
|
|
{ |
33
|
|
|
|
34
|
6 |
|
$uniqueUser = ''; |
35
|
6 |
|
$passwordRules = ''; |
36
|
6 |
|
if (Route::getCurrentRoute()->getName() == 'users.create') { |
37
|
|
|
|
38
|
|
|
$passwordRules = '|required|min:6'; |
39
|
|
|
$uniqueUser = '|unique:users'; |
40
|
|
|
} |
41
|
|
|
$rules = [ |
42
|
6 |
|
'name' => 'required|max:255', |
43
|
6 |
|
'email' => 'required|email|max:255' . $uniqueUser, |
44
|
6 |
|
'password' => 'confirmed' . $passwordRules, |
45
|
|
|
]; |
46
|
6 |
|
return $rules; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
1 |
|
public function store() |
51
|
|
|
{ |
52
|
|
|
|
53
|
1 |
|
$data = $this->except('_token'); |
54
|
|
|
|
55
|
1 |
|
if ($this->is("users")) { |
56
|
1 |
|
$data['provider'] = "created"; |
57
|
|
|
} else { |
58
|
|
|
$data['provider'] = "register"; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$data['provider_id'] = $data['email']; |
62
|
1 |
|
$data['verified'] = 1; |
63
|
|
|
|
64
|
1 |
|
$user = new User; |
65
|
|
|
|
66
|
|
|
|
67
|
1 |
|
$user->fill($data); |
68
|
1 |
|
$user->password = bcrypt($this->password); |
69
|
|
|
|
70
|
1 |
|
if (Auth::user()->cannot('store', $user)) { |
71
|
|
|
throw new AuthorizationException(); |
72
|
|
|
} |
73
|
1 |
|
return $user->save(); |
74
|
|
|
} |
75
|
|
|
|
76
|
4 |
|
public function update(User $user) |
77
|
|
|
{ |
78
|
|
|
// $this->authorize('update', [$user, Auth::user()]); |
79
|
|
|
|
80
|
4 |
|
if (Auth::user()->cannot('update', $user)) { |
81
|
|
|
throw new AuthorizationException(); |
82
|
|
|
} |
83
|
4 |
|
$except = []; |
84
|
4 |
|
if (trim($this->role_id) == '') { |
85
|
1 |
|
array_push($except, 'role_id'); |
86
|
|
|
} |
87
|
|
|
|
88
|
4 |
|
if (trim($this->password) == '' && trim($this->password_confirmation) == '') { |
89
|
|
|
|
90
|
3 |
|
array_push($except, 'password'); |
91
|
|
|
} |
92
|
4 |
|
array_push($except, '_token'); |
93
|
|
|
|
94
|
4 |
|
$req = $this->except($except); |
95
|
4 |
|
if ($this->avatar == "") { |
96
|
4 |
|
$req['avatar'] = null; |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
$user->fill($req); |
100
|
|
|
|
101
|
4 |
|
if (!in_array('password', $except)) { |
102
|
1 |
|
$user->password = bcrypt($this->password); |
103
|
|
|
} |
104
|
4 |
|
return $user->save(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|