-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathAugmentedUser.php
More file actions
110 lines (92 loc) · 2.76 KB
/
AugmentedUser.php
File metadata and controls
110 lines (92 loc) · 2.76 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespace Statamic\Auth;
use Statamic\Data\AbstractAugmented;
use Statamic\Facades\Role;
use Statamic\Facades\User;
use Statamic\Facades\UserGroup;
use Statamic\Fields\Value;
use Statamic\Support\Str;
class AugmentedUser extends AbstractAugmented
{
public function keys()
{
return $this->data->data()->keys()
->merge(collect($this->data->supplements() ?? [])->keys())
->merge($this->commonKeys())
->merge($this->roleHandles())
->merge($this->groupHandles())
->merge($this->blueprintFields()->keys())
->unique()->sort()->values()->all();
}
private function commonKeys()
{
return [
'id',
'name',
'title',
'email',
'initials',
'edit_url',
'is_user',
'last_login',
'avatar',
'api_url',
'preferred_locale',
];
}
public function get($handle): Value
{
if ($handle === 'is_user') {
return new Value(true, 'is_user', null, $this->data);
}
if ($handle === 'is_super') {
return new Value($this->data->isSuper(), 'is_super', null, $this->data);
}
if (Str::startsWith($handle, 'is_')) {
return new Value(in_array(Str::after($handle, 'is_'), $this->roles()), $handle, null, $this->data);
}
if (Str::startsWith($handle, 'in_')) {
return new Value(in_array(Str::after($handle, 'in_'), $this->groups()), $handle, null, $this->data);
}
return parent::get($handle);
}
protected function roles()
{
return $this->data->roles()->map->id()->values()->all();
}
protected function groups()
{
return $this->data->groups()->map->id()->values()->all();
}
protected function roleHandles()
{
return Role::all()->map(function ($role) {
return 'is_'.$role->handle();
})->values()->all();
}
protected function groupHandles()
{
return UserGroup::all()->map(function ($group) {
return 'in_'.$group->handle();
})->values()->all();
}
protected function initials()
{
if (! $this->data->hasQueriedColumn('name')) {
$user = User::query()
->where('id', $this->data->id())
->get(['name'])
->first();
$this->data->set('name', $user->get('name'));
}
return $this->data->initials();
}
protected function avatar()
{
return $this->data->hasAvatarField() ? $this->data->avatarFieldValue() : $this->data->gravatarUrl();
}
protected function preferredLocale()
{
return $this->data->preferredLocale();
}
}