AccessSettings::showEditPermissions()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 62
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 36
nc 2
nop 0
dl 0
loc 62
rs 9.344
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Epesi\Core\System\View\Form;
11
use Epesi\Core\Layout\View\ActionBar;
12
use Epesi\Core\System\Modules\ModuleView;
13
use atk4\data\Persistence\Static_;
14
use atk4\ui\Table;
15
use atk4\ui\Button;
16
use atk4\ui\Columns;
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::addItemButton('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::class, 'selectable'])->addStyle('cursor', 'pointer');
45
		$table->setModel($this->getModel($permissionsData), false);
46
		
47
		$table->addColumn('name', Table\Column\Text::class, ['caption' => __('Permissions')]);
48
		
49
		$table->addColumn(null, new Table\Column\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\Table\Column\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 Table\Column\Template(/** @scrutinizer ignore-type */ [['i', 'class' => 'indicator arrow circle right icon']]), ['caption' => 'Test'])->setAttr('class', ['right aligned']);
Loading history...
50
		
51
		$this->getApp()->addStyle('
52
			table.selectable tr:not(.active) .indicator {
53
				display: none;
54
			}'
55
		);
56
		
57
		$table->on('click', 'tr', $this->reload(new \atk4\ui\JsExpression('$(this).data("id")')));
58
		
59
		if ($permissionId = $this->permissionId()) {
60
			$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

60
			$permission = Permission::findById(/** @scrutinizer ignore-type */ $permissionId);
Loading history...
61
			
62
			$this->putModuleVariable('permission', $permissionId);
63
			
64
			$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...
65
			
66
			$rolesData = $permission->roles()->get()->map(function($role) {
67
				$role['name'] = trans($role['name']);
68
				
69
				return $role;
70
			})->toArray();
71
72
			$column = $this->columns()->addColumn();
73
			$rolesTable = Table::addTo($column);
74
			$rolesTable->setModel($this->getModel($rolesData), false);
75
			
76
			$rolesTable->addColumn('name', [Table\Column\Text::class], ['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...
77
			
78
			$roleActions = $rolesTable->addColumn(null, [Table\Column\ActionButtons::class]);
79
			$roleActions->addButton($this->deleteButton(), function($jQuery, $roleId) use ($permission) {
0 ignored issues
show
Bug introduced by
The method addButton() does not exist on atk4\ui\Table\Column. It seems like you code against a sub-type of atk4\ui\Table\Column such as atk4\ui\Table\Column\ActionButtons. ( Ignorable by Annotation )

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

79
			$roleActions->/** @scrutinizer ignore-call */ 
80
                 addButton($this->deleteButton(), function($jQuery, $roleId) use ($permission) {
Loading history...
80
				Role::findById($roleId)->revokePermissionTo($permission);
81
				
82
				return $this->reload();
83
			}, __('Revoke permission to role?'));
84
				
85
			$usersData = $permission->users()->get()->toArray();
86
87
			$usersTable = Table::addTo($column);
88
			$usersTable->setModel($this->getModel($usersData), false);
89
				
90
			$usersTable->addColumn('name', [Table\Column\Text::class], ['caption' => __('Users allowed to :permission', ['permission' => $permission->name])]);
91
				
92
			$userActions = $usersTable->addColumn(null, [Table\Column\ActionButtons::class]);
93
			$userActions->addButton($this->deleteButton(), function($jQuery, $userId) use ($permission) {
94
				User::find($userId)->revokePermissionTo($permission);
95
					
96
				return $this->reload();
97
			}, __('Revoke permission to user?'));
98
		}
99
	}
100
	
101
	protected function columns()
102
	{
103
		return $this->columns = $this->columns?: Columns::addTo($this);
104
	}
105
	
106
	protected function permissionId()
107
	{
108
		return $this->getApp()->stickyGet($this->columns()->name)?: $this->getModuleVariable('permission');
109
	}
110
	
111
	protected function reload($permissionExpression = null)
112
	{
113
		$columns = $this->columns();
114
		
115
		return $this->reload = $this->reload?: new \atk4\ui\JsReload($columns, [$columns->name => $permissionExpression?: '']);
116
	}
117
	
118
	protected function getModel($array)
119
	{
120
		return new Model(new Static_($array));
121
	}
122
	
123
	protected function addGrantRoleAccessButton()
124
	{
125
		$modal = \atk4\ui\Modal::addTo($this, ['title' => __('Grant Role Access')])->set(function($view) {
126
			$form = Form::addTo($view, ['buttonSave' => ['Button', __('Save'), 'primary']]);
127
			
128
			$form->addControl('role', [\atk4\ui\Form\Control\Dropdown::class, 'caption' => __('Grant'), 'values' => Role::all()->pluck('name', 'id')])->set($this->getModuleVariable('permission'));
129
			
130
			$form->addControl('permission', [\atk4\ui\Form\Control\Dropdown::class, 'caption' => __('Access To'), 'values' => Permission::all()->pluck('name', 'id')])->set($this->getModuleVariable('permission'));
131
132
			$form->layout->addButton([Button::class, __('Cancel')])->on('click', $view->getOwner()->hide());
133
134
			$form->onSubmit(function($form) use ($view) {
135
				$values = $form->getValues();
136
				
137
				Role::findById($values['role'])->givePermissionTo($values['permission']);
138
				
139
				return [
140
						$view->getOwner()->hide(),
141
						$this->reload()
142
				];
143
			});
144
		});
145
		
146
		ActionBar::addItemButton([__('Grant Role Access'), 'icon' => 'group'])->on('click', $modal->show());
147
	}
148
	
149
	protected function addGrantUserAccessButton()
150
	{
151
		$modal = \atk4\ui\Modal::addTo($this, ['title' => __('Grant User Access')])->set(function($view) {
152
			$form = Form::addTo($view, ['buttonSave' => [Button::class, __('Save'), 'primary']]);
153
			
154
			$form->addControl('user', [\atk4\ui\Form\Control\Dropdown::class, 'caption' => __('Grant'), 'values' => User::all()->pluck('name', 'id')]);
155
			
156
			$form->addControl('permission', [\atk4\ui\Form\Control\Dropdown::class, 'caption' => __('Access To'), 'values' => Permission::all()->pluck('name', 'id')])->set($this->getModuleVariable('permission'));
157
			
158
			$form->layout->addButton([Button::class, __('Cancel')])->on('click', $view->getOwner()->hide());
159
			
160
			$form->onSubmit(function($form) use ($view) {
161
				$values = $form->getValues();
162
				
163
				User::find($values['user'])->givePermissionTo($values['permission']);
164
				
165
				return [
166
						$view->getOwner()->hide(),
167
						$this->reload()
168
				];
169
			});
170
		});
171
		
172
		ActionBar::addItemButton([__('Grant User Access'), 'icon' => 'user', ])->on('click', $modal->show());
173
	}
174
	protected function deleteButton()
175
	{
176
		return ['icon' => 'trash', 'class' => ['red'], 'attr' => ['title' => __('Revoke Permission')]];
177
	}
178
}
179