-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEditUser.php
More file actions
91 lines (76 loc) · 2.77 KB
/
Copy pathEditUser.php
File metadata and controls
91 lines (76 loc) · 2.77 KB
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
<?php
declare(strict_types=1);
namespace NyonCode\WireModuleUsers\Pages;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use NyonCode\WireCore\Core\Data\RecordContract;
use NyonCode\WireForms\Forms\Form;
use NyonCode\WireModuleUsers\Concerns\SyncsRoles;
use NyonCode\WireModuleUsers\Resources\UserResource;
use NyonCode\WireModuleUsers\Support\AccountGuard;
use NyonCode\WireModuleUsers\Support\Roles;
use NyonCode\WireModuleUsers\Support\Teams;
use NyonCode\WirePanels\Resources\Concerns\ResolvesScopedRecord;
use NyonCode\WirePanels\Resources\Pages\EditPage;
/**
* One user, edited.
*
* Two things this page does that the base page cannot know about: it keeps the
* password hash out of the form state, and it seeds the roles select from a
* relation rather than from a column.
*/
class EditUser extends EditPage
{
use ResolvesScopedRecord {
resolveRecord as resolveScopedRecord;
}
use SyncsRoles;
protected static ?string $resource = UserResource::class;
public function form(Form $form): Form
{
return $this->syncRolesAfterSave(parent::form($form));
}
/**
* What the form is seeded with.
*
* **The password is removed rather than trusted to be hidden.** Laravel's
* own `User` marks it `$hidden`, so `attributesToArray()` drops it — and an
* application's model that does not is the one where a bcrypt hash would
* ride into the Livewire snapshot, be sent to the browser, and be written
* back re-hashed on the next save. One `unset` is cheaper than depending on
* someone else's `$hidden`.
*
* @return array<string, mixed>
*/
protected function recordData(): array
{
$data = parent::recordData();
unset($data[UserResource::field('password')]);
if (Roles::enabled()) {
$record = $this->resolveRecord();
$data['roles'] = $record instanceof Model && method_exists($record, 'roles')
? $record->roles->pluck('name')->all()
: [];
}
return $data;
}
protected function scopeRecordQuery(Builder $query): Builder
{
return Teams::scopeMembers($query);
}
/**
* The account, if this person may see it — and a 403 if it is a super-admin they are not.
*
* On every request the page makes, the save included, for the reason the
* role page gives: the form cannot be built without the record, so an
* account opened by replaying a request is refused the same way.
*/
protected function resolveRecord(): Model|RecordContract|null
{
$record = $this->resolveScopedRecord();
if ($record instanceof Model && ! AccountGuard::mayEdit($record)) {
abort(403);
}
return $record;
}
}