1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Epesi\Core\System\User\Access; |
4
|
|
|
|
5
|
|
|
use Epesi\Core\System\User\Database\Models\User; |
6
|
|
|
|
7
|
|
|
use Spatie\Permission\Models\Permission; |
8
|
|
|
use Spatie\Permission\Models\Role; |
9
|
|
|
use atk4\data\Model; |
10
|
|
|
use atk4\ui\jsReload; |
11
|
|
|
use atk4\ui\jsExpression; |
12
|
|
|
use atk4\ui\TableColumn\Template; |
13
|
|
|
use atk4\data\Persistence_Static; |
14
|
|
|
use Epesi\Core\System\Seeds\Form; |
15
|
|
|
use Epesi\Core\Layout\Seeds\ActionBar; |
16
|
|
|
use Epesi\Core\System\Integration\Modules\ModuleView; |
17
|
|
|
|
18
|
|
|
class AccessSettings extends ModuleView |
19
|
|
|
{ |
20
|
|
|
protected $label = ['System Settings', 'User Access']; |
21
|
|
|
|
22
|
|
|
protected $columns; |
23
|
|
|
protected $reload; |
24
|
|
|
|
25
|
|
|
public function body() |
26
|
|
|
{ |
27
|
|
|
ActionBar::addButton('back')->link(url('view/system')); |
28
|
|
|
|
29
|
|
|
$this->showEditPermissions(); |
30
|
|
|
|
31
|
|
|
$this->addGrantRoleAccessButton(); |
32
|
|
|
|
33
|
|
|
$this->addGrantUserAccessButton(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function showEditPermissions() |
37
|
|
|
{ |
38
|
|
|
$permissionsData = Permission::all(['id', 'name'])->map(function($permission){ |
39
|
|
|
$permission['name'] = trans(ucwords($permission['name'])); |
40
|
|
|
|
41
|
|
|
return $permission; |
42
|
|
|
})->toArray(); |
43
|
|
|
|
44
|
|
|
$table = $this->columns()->addColumn()->add(['Table', 'selectable'])->addStyle('cursor', 'pointer'); |
|
|
|
|
45
|
|
|
$table->setModel($this->getModel($permissionsData), false); |
46
|
|
|
|
47
|
|
|
$table->addColumn('name', 'Text', ['caption' => __('Permissions')]); |
48
|
|
|
|
49
|
|
|
$table->addColumn(null, new Template([['i', 'class' => 'indicator arrow circle right icon']]), ['caption' => 'Test'])->setAttr('class', ['right aligned']); |
|
|
|
|
50
|
|
|
|
51
|
|
|
eval_css(' |
52
|
|
|
table.selectable tr:not(.active) .indicator { |
53
|
|
|
display: none; |
54
|
|
|
}'); |
55
|
|
|
|
56
|
|
|
$table->on('click', 'tr', $this->reload(new jsExpression('$(this).data("id")'))); |
57
|
|
|
|
58
|
|
|
if ($permissionId = $this->permissionId()) { |
59
|
|
|
$permission = Permission::findById($permissionId); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->putModuleVariable('permission', $permissionId); |
62
|
|
|
|
63
|
|
|
$table->js(true)->find("tr[data-id=$permission->id]")->addClass('active'); |
|
|
|
|
64
|
|
|
|
65
|
|
|
$rolesData = $permission->roles()->get()->map(function($role) { |
66
|
|
|
$role['name'] = trans($role['name']); |
67
|
|
|
|
68
|
|
|
return $role; |
69
|
|
|
})->toArray(); |
70
|
|
|
|
71
|
|
|
$column = $this->columns()->addColumn(); |
72
|
|
|
$rolesTable = $column->add('Table'); |
73
|
|
|
$rolesTable->setModel($this->getModel($rolesData), false); |
74
|
|
|
|
75
|
|
|
$rolesTable->addColumn('name', 'Text', ['caption' => __('Roles allowed to :permission', ['permission' => $permission->name])]); |
|
|
|
|
76
|
|
|
|
77
|
|
|
$roleActions = $rolesTable->addColumn(null, 'Actions'); |
78
|
|
|
$roleActions->addAction($this->deleteButton(), function($jQuery, $roleId) use ($permission) { |
79
|
|
|
Role::findById($roleId)->revokePermissionTo($permission); |
80
|
|
|
|
81
|
|
|
return $this->reload(); |
82
|
|
|
}, __('Revoke permission to role?')); |
83
|
|
|
|
84
|
|
|
$usersData = $permission->users()->get()->toArray(); |
85
|
|
|
|
86
|
|
|
$usersTable = $column->add('Table'); |
87
|
|
|
$usersTable->setModel($this->getModel($usersData), false); |
88
|
|
|
|
89
|
|
|
$usersTable->addColumn('name', 'Text', ['caption' => __('Users allowed to :permission', ['permission' => $permission->name])]); |
90
|
|
|
|
91
|
|
|
$userActions = $usersTable->addColumn(null, 'Actions'); |
92
|
|
|
$userActions->addAction($this->deleteButton(), function($jQuery, $userId) use ($permission) { |
93
|
|
|
User::find($userId)->revokePermissionTo($permission); |
94
|
|
|
|
95
|
|
|
return $this->reload(); |
96
|
|
|
}, __('Revoke permission to user?')); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
protected function columns() |
101
|
|
|
{ |
102
|
|
|
return $this->columns = $this->columns?: $this->add('Columns'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function permissionId() |
106
|
|
|
{ |
107
|
|
|
return $this->app->stickyGet($this->columns()->name)?: $this->getModuleVariable('permission'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function reload($permissionExpression = null) |
111
|
|
|
{ |
112
|
|
|
$columns = $this->columns(); |
113
|
|
|
|
114
|
|
|
return $this->reload = $this->reload?: new jsReload($columns, [$columns->name => $permissionExpression?: '']); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
protected function getModel($array) |
118
|
|
|
{ |
119
|
|
|
return new Model(new Persistence_Static($array)); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
protected function addGrantRoleAccessButton() |
123
|
|
|
{ |
124
|
|
|
$modal = $this->add(['Modal', 'title' => __('Grant Role Access')])->set(function($view) { |
|
|
|
|
125
|
|
|
$form = $view->add(new Form(['buttonSave' => ['Button', __('Save'), 'primary']])); |
126
|
|
|
|
127
|
|
|
$form->addField('role', ['DropDown', 'caption' => __('Grant'), 'values' => Role::all()->pluck('name', 'id')])->set($this->getModuleVariable('permission')); |
128
|
|
|
|
129
|
|
|
$form->addField('permission', ['DropDown', 'caption' => __('Access To'), 'values' => Permission::all()->pluck('name', 'id')])->set($this->getModuleVariable('permission')); |
130
|
|
|
|
131
|
|
|
$form->layout->addButton(['Button', __('Cancel')])->on('click', $view->owner->hide()); |
132
|
|
|
|
133
|
|
|
$form->onSubmit(function($form) use ($view) { |
134
|
|
|
$values = $form->getValues(); |
135
|
|
|
|
136
|
|
|
Role::findById($values['role'])->givePermissionTo($values['permission']); |
137
|
|
|
|
138
|
|
|
return [ |
139
|
|
|
$view->owner->hide(), |
140
|
|
|
$this->reload() |
141
|
|
|
]; |
142
|
|
|
}); |
143
|
|
|
}); |
144
|
|
|
|
145
|
|
|
ActionBar::addButton(['icon' => 'group', 'label' => __('Grant Role Access')])->on('click', $modal->show()); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
protected function addGrantUserAccessButton() |
149
|
|
|
{ |
150
|
|
|
$modal = $this->add(['Modal', 'title' => __('Grant User Access')])->set(function($view) { |
|
|
|
|
151
|
|
|
$form = $view->add(new Form(['buttonSave' => ['Button', __('Save'), 'primary']])); |
152
|
|
|
|
153
|
|
|
$form->addField('user', ['DropDown', 'caption' => __('Grant'), 'values' => User::all()->pluck('name', 'id')]); |
154
|
|
|
|
155
|
|
|
$form->addField('permission', ['DropDown', 'caption' => __('Access To'), 'values' => Permission::all()->pluck('name', 'id')])->set($this->getModuleVariable('permission')); |
156
|
|
|
|
157
|
|
|
$form->layout->addButton(['Button', __('Cancel')])->on('click', $view->owner->hide()); |
158
|
|
|
|
159
|
|
|
$form->onSubmit(function($form) use ($view) { |
160
|
|
|
$values = $form->getValues(); |
161
|
|
|
|
162
|
|
|
User::find($values['user'])->givePermissionTo($values['permission']); |
163
|
|
|
|
164
|
|
|
return [ |
165
|
|
|
$view->owner->hide(), |
166
|
|
|
$this->reload() |
167
|
|
|
]; |
168
|
|
|
}); |
169
|
|
|
}); |
170
|
|
|
|
171
|
|
|
ActionBar::addButton(['icon' => 'user', 'label' => __('Grant User Access')])->on('click', $modal->show()); |
172
|
|
|
} |
173
|
|
|
protected function deleteButton() |
174
|
|
|
{ |
175
|
|
|
return ['icon' => 'trash', 'class' => ['red'], 'attr' => ['title' => __('Revoke Permission')]]; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|