Passed
Push — main ( 5e9128...7e664e )
by PRATIK
03:55
created

AclPanel::setACLPanel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Pratiksh\Adminetic\Http\Livewire\Admin\Role;
4
5
use Livewire\Component;
6
use Pratiksh\Adminetic\Models\Admin\Permission;
7
use Pratiksh\Adminetic\Models\Admin\Role;
8
9
class AclPanel extends Component
10
{
11
    public $role;
12
    public $permissions;
13
    public $remaining_models;
14
    public $all_models;
15
    public $role_models;
16
    public $model_name;
17
18
    public $listeners = ['permission_deleted' => 'permissionDeleted'];
19
20
    public function mount(Role $role)
21
    {
22
        $this->setACLPanel($role);
23
    }
24
25
    public function permissionDeleted()
26
    {
27
        $this->setACLPanel($this->role);
28
    }
29
30
    public function makeModuleACL()
31
    {
32
        $this->validate([
33
            'model_name' => 'required|max:30'
34
        ]);
35
36
        Permission::create([
37
            'browse' => 1,
38
            'read' => 1,
39
            'edit' => 1,
40
            'add' => 1,
41
            'delete' => 1,
42
            'role_id' => $this->role->id,
43
            'model' => $this->model_name,
44
        ]);
45
        $this->permissions = Permission::where('role_id', $this->role->id)->get();
46
47
        $this->emit('make_module_acl_success');
48
    }
49
50
    public function render()
51
    {
52
        return view('adminetic::livewire.admin.role.acl-panel');
53
    }
54
55
    private function setACLPanel(Role $role)
56
    {
57
        $this->role = $role;
58
        $this->permissions = $role->permissions;
59
        $this->all_models = getAllModelNames(app_path('Models'));
60
        $this->role_models = $role->permissions->pluck('model')->toArray();
61
        $this->remaining_models = array_diff($this->all_models, $this->role_models ?? []);
62
    }
63
}
64