forked from orchestral/foundation
/
User.php
248 lines (207 loc) · 7.77 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
<?php namespace Orchestra\Foundation\Processor;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Orchestra\Model\User as Eloquent;
use Orchestra\Support\Facades\Foundation;
use Orchestra\Foundation\Validation\User as Validator;
use Orchestra\Foundation\Http\Presenters\User as Presenter;
use Orchestra\Contracts\Foundation\Command\Account\UserViewer as UserViewerCommand;
use Orchestra\Contracts\Foundation\Command\Account\UserCreator as UserCreatorCommand;
use Orchestra\Contracts\Foundation\Command\Account\UserRemover as UserRemoverCommand;
use Orchestra\Contracts\Foundation\Command\Account\UserUpdater as UserUpdaterCommand;
use Orchestra\Contracts\Foundation\Listener\Account\UserViewer as UserViewerListener;
use Orchestra\Contracts\Foundation\Listener\Account\UserCreator as UserCreatorListener;
use Orchestra\Contracts\Foundation\Listener\Account\UserRemover as UserRemoverListener;
use Orchestra\Contracts\Foundation\Listener\Account\UserUpdater as UserUpdaterListener;
class User extends Processor implements UserCreatorCommand, UserRemoverCommand, UserUpdaterCommand, UserViewerCommand
{
/**
* Create a new processor instance.
*
* @param \Orchestra\Foundation\Http\Presenters\User $presenter
* @param \Orchestra\Foundation\Validation\User $validator
*/
public function __construct(Presenter $presenter, Validator $validator)
{
$this->presenter = $presenter;
$this->validator = $validator;
}
/**
* View list users page.
*
* @param \Orchestra\Contracts\Foundation\Listener\Account\UserViewer $listener
* @param array $input
*
* @return mixed
*/
public function index(UserViewerListener $listener, array $input = [])
{
$searchKeyword = Arr::get($input, 'q', '');
$searchRoles = Arr::get($input, 'roles', []);
// Get Users (with roles) and limit it to only 30 results for
// pagination. Don't you just love it when pagination simply works.
$eloquent = Foundation::make('orchestra.user')->search($searchKeyword, $searchRoles);
$roles = Foundation::make('orchestra.role')->lists('name', 'id');
// Build users table HTML using a schema liked code structure.
$table = $this->presenter->table($eloquent);
Event::fire('orchestra.list: users', [$eloquent, $table]);
// Once all event listening to `orchestra.list: users` is executed,
// we can add we can now add the final column, edit and delete
// action for users.
$this->presenter->actions($table);
$data = [
'eloquent' => $eloquent,
'roles' => $roles,
'table' => $table,
];
return $listener->showUsers($data);
}
/**
* View create user page.
*
* @param \Orchestra\Contracts\Foundation\Listener\Account\UserCreator $listener
*
* @return mixed
*/
public function create(UserCreatorListener $listener)
{
$eloquent = Foundation::make('orchestra.user');
$form = $this->presenter->form($eloquent, 'create');
$this->fireEvent('form', [$eloquent, $form]);
return $listener->showUserCreator(compact('eloquent', 'form'));
}
/**
* View edit user page.
*
* @param \Orchestra\Contracts\Foundation\Listener\Account\UserUpdater $listener
* @param string|int $id
*
* @return mixed
*/
public function edit(UserUpdaterListener $listener, $id)
{
$eloquent = Foundation::make('orchestra.user')->findOrFail($id);
$form = $this->presenter->form($eloquent, 'update');
$this->fireEvent('form', [$eloquent, $form]);
return $listener->showUserChanger(compact('eloquent', 'form'));
}
/**
* Store a user.
*
* @param \Orchestra\Contracts\Foundation\Listener\Account\UserCreator $listener
* @param array $input
*
* @return mixed
*/
public function store(UserCreatorListener $listener, array $input)
{
$validation = $this->validator->on('create')->with($input);
if ($validation->fails()) {
return $listener->createUserFailedValidation($validation->getMessageBag());
}
$user = Foundation::make('orchestra.user');
$user->status = Eloquent::UNVERIFIED;
$user->password = $input['password'];
try {
$this->saving($user, $input, 'create');
} catch (Exception $e) {
return $listener->createUserFailed(['error' => $e->getMessage()]);
}
return $listener->userCreated();
}
/**
* Update a user.
*
* @param \Orchestra\Contracts\Foundation\Listener\Account\UserUpdater $listener
* @param string|int $id
* @param array $input
*
* @return mixed
*/
public function update(UserUpdaterListener $listener, $id, array $input)
{
// Check if provided id is the same as hidden id, just a pre-caution.
if ((string) $id !== $input['id']) {
return $listener->abortWhenUserMismatched();
}
$validation = $this->validator->on('update')->with($input);
if ($validation->fails()) {
return $listener->updateUserFailedValidation($validation->getMessageBag(), $id);
}
$user = Foundation::make('orchestra.user')->findOrFail($id);
! empty($input['password']) && $user->password = $input['password'];
try {
$this->saving($user, $input, 'update');
} catch (Exception $e) {
return $listener->updateUserFailed(['error' => $e->getMessage()]);
}
return $listener->userUpdated();
}
/**
* Destroy a user.
*
* @param \Orchestra\Contracts\Foundation\Listener\Account\UserRemover $listener
* @param string|int $id
*
* @return mixed
*/
public function destroy(UserRemoverListener $listener, $id)
{
$user = Foundation::make('orchestra.user')->findOrFail($id);
// Avoid self-deleting accident.
if ((string) $user->id === (string) Auth::user()->id) {
return $listener->selfDeletionFailed();
}
try {
$this->fireEvent('deleting', [$user]);
DB::transaction(function () use ($user) {
$user->delete();
});
$this->fireEvent('deleted', [$user]);
} catch (Exception $e) {
return $listener->userDeletionFailed(['error' => $e->getMessage()]);
}
return $listener->userDeleted();
}
/**
* Save the user.
*
* @param \Orchestra\Model\User $user
* @param array $input
* @param string $type
*
* @return bool
*/
protected function saving(Eloquent $user, $input = [], $type = 'create')
{
$beforeEvent = ($type === 'create' ? 'creating' : 'updating');
$afterEvent = ($type === 'create' ? 'created' : 'updated');
$user->fullname = $input['fullname'];
$user->email = $input['email'];
$this->fireEvent($beforeEvent, [$user]);
$this->fireEvent('saving', [$user]);
DB::transaction(function () use ($user, $input) {
$user->save();
$user->roles()->sync($input['roles']);
});
$this->fireEvent($afterEvent, [$user]);
$this->fireEvent('saved', [$user]);
return true;
}
/**
* Fire Event related to eloquent process.
*
* @param string $type
* @param array $parameters
*
* @return void
*/
protected function fireEvent($type, array $parameters = [])
{
Event::fire("orchestra.{$type}: users", $parameters);
Event::fire("orchestra.{$type}: user.account", $parameters);
}
}