|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Back; |
|
4
|
|
|
|
|
5
|
|
|
use App\Services\Auth\Back\Enums\UserRole; |
|
6
|
|
|
use App\Services\Auth\Back\Enums\UserStatus; |
|
7
|
|
|
use App\Services\Auth\Back\Events\UserCreated; |
|
8
|
|
|
use App\Http\Requests\Back\BackUserRequest; |
|
9
|
|
|
use App\Services\Auth\Back\User; |
|
10
|
|
|
use App\Services\Auth\Back\UserUpdater; |
|
11
|
|
|
use App\Repositories\BackUserRepository; |
|
12
|
|
|
|
|
13
|
|
|
class AdministratorsController |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var \App\Repositories\BackUserRepository */ |
|
16
|
|
|
protected $backUserRepository; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(BackUserRepository $backUserRepository) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->backUserRepository = $backUserRepository; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function index() |
|
24
|
|
|
{ |
|
25
|
|
|
$users = $this->backUserRepository->getAll(); |
|
26
|
|
|
|
|
27
|
|
|
return view('back.administrators.index')->with(compact('users')); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function create() |
|
31
|
|
|
{ |
|
32
|
|
|
return view('back.administrators.create', ['user' => new User()]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function store(BackUserRequest $request) |
|
36
|
|
|
{ |
|
37
|
|
|
$user = new User(); |
|
38
|
|
|
|
|
39
|
|
|
$user->email = $request->get('email'); |
|
40
|
|
|
$user->first_name = $request->get('first_name'); |
|
41
|
|
|
$user->last_name = $request->get('last_name'); |
|
42
|
|
|
$user->locale = $request->get('locale', 'nl'); |
|
43
|
|
|
|
|
44
|
|
|
if ($request->has('password')) { |
|
45
|
|
|
$user->password = $request->get('password'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$user->role = UserRole::ADMIN(); |
|
49
|
|
|
$user->status = UserStatus::ACTIVE(); |
|
50
|
|
|
|
|
51
|
|
|
$this->backUserRepository->save($user); |
|
52
|
|
|
|
|
53
|
|
|
$eventDescription = $this->getEventDescriptionFor('created', $user); |
|
54
|
|
|
activity()->on($user)->log($eventDescription); |
|
55
|
|
|
flash()->success(strip_tags($eventDescription).'. '.fragment('back.administrators.passwordMailSent')); |
|
56
|
|
|
|
|
57
|
|
|
event(new UserCreated($user)); |
|
58
|
|
|
|
|
59
|
|
|
return redirect(action('Back\AdministratorsController@index', ['role' => $user->role])); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
public function edit($id) |
|
63
|
|
|
{ |
|
64
|
|
|
$user = $this->backUserRepository->find($id); |
|
65
|
|
|
|
|
66
|
|
|
if (!$user) { |
|
67
|
|
|
abort(404); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return view('back.administrators.edit')->with(compact('user')); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function update($id, BackUserRequest $request) |
|
74
|
|
|
{ |
|
75
|
|
|
abort_unless($user = $this->backUserRepository->find($id), 500); |
|
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
$user->email = $request->get('email'); |
|
78
|
|
|
$user->first_name = $request->get('first_name'); |
|
79
|
|
|
$user->last_name = $request->get('last_name'); |
|
80
|
|
|
$user->locale = $request->get('locale', 'nl'); |
|
81
|
|
|
|
|
82
|
|
|
if ($request->has('password')) { |
|
83
|
|
|
$user->password = $request->get('password'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->backUserRepository->save($user); |
|
87
|
|
|
|
|
88
|
|
|
$eventDescription = $this->getEventDescriptionFor('updated', $user); |
|
89
|
|
|
activity()->log($eventDescription); |
|
90
|
|
|
flash()->success(strip_tags($eventDescription)); |
|
91
|
|
|
|
|
92
|
|
|
return redirect()->action('Back\AdministratorsController@index'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
View Code Duplication |
public function activate($id) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
abort_unlees($user = $this->backUserRepository->find($id), 500); |
|
98
|
|
|
|
|
99
|
|
|
$user->activate(); |
|
100
|
|
|
|
|
101
|
|
|
$eventDescription = $this->getEventDescriptionFor('activated', $user); |
|
|
|
|
|
|
102
|
|
|
activity($eventDescription); |
|
103
|
|
|
flash()->success(strip_tags($eventDescription)); |
|
104
|
|
|
|
|
105
|
|
|
return back(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
View Code Duplication |
public function destroy($id) |
|
|
|
|
|
|
109
|
|
|
{ |
|
110
|
|
|
$user = $this->backUserRepository->find($id); |
|
111
|
|
|
|
|
112
|
|
|
$eventDescription = $this->getEventDescriptionFor('deleted', $user); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
$this->backUserRepository->delete($user); |
|
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
activity($eventDescription); |
|
117
|
|
|
flash()->success(strip_tags($eventDescription)); |
|
118
|
|
|
|
|
119
|
|
|
return redirect()->action('Back\AdministratorsController@index'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
View Code Duplication |
protected function getEventDescriptionFor(string $event, User $user): string |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$name = sprintf( |
|
125
|
|
|
'<a href="%s">%s</a>', |
|
126
|
|
|
action('Back\AdministratorsController@edit', [$user->id]), |
|
127
|
|
|
$user->email |
|
128
|
|
|
); |
|
129
|
|
|
|
|
130
|
|
|
if ($event === 'deleted') { |
|
131
|
|
|
$name = $user->email; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return trans("back.events.$event", ['model' => fragment('back.administrators.administrator'), 'name' => $name]); |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: