Passed
Push — master ( c28376...9b6cf0 )
by Georgi
07:09
created

AccessSettings::addGrantRoleAccessButton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 24
rs 9.8666
c 0
b 0
f 0
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');
0 ignored issues
show
Bug introduced by
The method addColumn() does not exist on atk4\ui\View. It seems like you code against a sub-type of atk4\ui\View such as atk4\ui\Columns or atk4\ui\Grid or atk4\ui\Table. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
		$table = $this->columns()->/** @scrutinizer ignore-call */ addColumn()->add(['Table', 'selectable'])->addStyle('cursor', 'pointer');
Loading history...
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']);
0 ignored issues
show
Bug introduced by
array(array('i', 'class'...ow circle right icon')) of type array<integer,string[]> is incompatible with the type string expected by parameter $template of atk4\ui\TableColumn\Template::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
		$table->addColumn(null, new Template(/** @scrutinizer ignore-type */ [['i', 'class' => 'indicator arrow circle right icon']]), ['caption' => 'Test'])->setAttr('class', ['right aligned']);
Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $permissionId can also be of type string; however, parameter $id of Spatie\Permission\Models\Permission::findById() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
			$permission = Permission::findById(/** @scrutinizer ignore-type */ $permissionId);
Loading history...
60
			
61
			$this->putModuleVariable('permission', $permissionId);
62
			
63
			$table->js(true)->find("tr[data-id=$permission->id]")->addClass('active');
0 ignored issues
show
Bug introduced by
Accessing id on the interface Spatie\Permission\Contracts\Permission suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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])]);
0 ignored issues
show
Bug introduced by
Accessing name on the interface Spatie\Permission\Contracts\Permission suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
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));
0 ignored issues
show
Deprecated Code introduced by
The class atk4\data\Persistence_Static has been deprecated: class will be removed in next major version. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

119
		return new Model(/** @scrutinizer ignore-deprecated */ new Persistence_Static($array));
Loading history...
120
	}
121
	
122
	protected function addGrantRoleAccessButton()
123
	{
124
		$modal = $this->add(['Modal', 'title' => __('Grant Role Access')])->set(function($view) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type array|string expected by parameter $arg1 of atk4\ui\View::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

124
		$modal = $this->add(['Modal', 'title' => __('Grant Role Access')])->set(/** @scrutinizer ignore-type */ function($view) {
Loading history...
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) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type array|string expected by parameter $arg1 of atk4\ui\View::set(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

150
		$modal = $this->add(['Modal', 'title' => __('Grant User Access')])->set(/** @scrutinizer ignore-type */ function($view) {
Loading history...
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