Passed
Push — feature/permission-manager ( 41e93d )
by
unknown
09:00
created

GroupController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 21
rs 9.7333
1
<?php
2
3
namespace A17\Twill\Http\Controllers\Admin;
4
5
use A17\Twill\Models\Group;
6
use Illuminate\Http\Request;
7
use A17\Twill\Models\Permission;
8
use Illuminate\Contracts\Foundation\Application;
9
10
class GroupController extends ModuleController
11
{
12
    protected $namespace = 'A17\Twill';
13
14
    protected $moduleName = 'groups';
15
16
    protected $defaultOrders = ['name' => 'asc'];
17
18
    protected $defaultFilters = [
19
        'search' => 'search',
20
    ];
21
22
    protected $titleColumnKey = 'name';
23
24
    protected $indexOptions = [
25
        'permalink' => false,
26
    ];
27
28
    protected $labels = [
29
        'published' => 'twill::lang.permissions.groups.published',
30
        'draft' => 'twill::lang.permissions.groups.draft',
31
        'listing' => [
32
            'filter' => [
33
                'published' => 'twill::lang.permissions.groups.published',
34
                'draft' => 'twill::lang.permissions.groups.draft',
35
            ],
36
        ],
37
    ];
38
39
    public function __construct(Application $app, Request $request)
40
    {
41
        parent::__construct($app, $request);
42
        $this->middleware('can:edit-user-groups');
43
44
        $this->primaryNavigation = [
0 ignored issues
show
Bug Best Practice introduced by
The property primaryNavigation does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
45
            'users' => [
46
                'title' => twillTrans('twill::lang.user-management.users'),
47
                'module' => true,
48
                'can' => 'edit-users',
49
            ],
50
            'roles' => [
51
                'title' => twillTrans('twill::lang.permissions.roles.title'),
52
                'module' => true,
53
                'can' => 'edit-user-roles',
54
            ],
55
            'groups' => [
56
                'title' => twillTrans('twill::lang.permissions.groups.title'),
57
                'module' => true,
58
                'active' => true,
59
                'can' => 'edit-user-groups',
60
            ],
61
        ];
62
    }
63
64
    protected $indexColumns = [
65
        'name' => [
66
            'title' => 'Name',
67
            'field' => 'name',
68
            'sort' => true,
69
        ],
70
        'created_at' => [
71
            'title' => 'Date created',
72
            'field' => 'created_at',
73
            'sort' => true
74
        ],
75
        'users' => [
76
            'title' => 'Users',
77
            'field' => 'users_count',
78
            'html' => true
79
        ]
80
    ];
81
82
    protected function indexData($request)
83
    {
84
        return [
85
            'primary_navigation' => $this->primaryNavigation,
86
        ];
87
    }
88
89
    protected function getIndexOption($option, $item = null)
90
    {
91
        if (in_array($option, ['publish', 'bulkEdit', 'create'])) {
92
            return auth('twill_users')->user()->can('edit-user-groups');
93
        }
94
95
        return parent::getIndexOption($option);
96
    }
97
98
    protected function formData($request)
99
    {
100
        return [
101
            'primary_navigation' => $this->primaryNavigation,
102
            'permissionModules' => Permission::permissionableParentModuleItems(),
103
        ];
104
    }
105
106
    protected function indexItemData($item)
107
    {
108
        $canEdit = auth('twill_users')->user()->can('edit-user-groups') && ($item->canEdit ?? true);
109
110
        return ['edit' => $canEdit ? $this->getModuleRoute($item->id, 'edit') : null];
111
    }
112
113
    protected function getIndexItems($scopes = [], $forcePagination = false)
114
    {
115
        // Everyone group should always be on top
116
        return parent::getIndexItems($scopes, $forcePagination)->sortByDesc('is_everyone_group')->values();
117
    }
118
119
    protected function getBrowserItems($scopes = [])
120
    {
121
        // Exclude everyone group from browsers
122
        return parent::getBrowserItems($scopes)->filter(function ($item) {
123
            return !$item->isEveryoneGroup();
124
        })->values();
125
    }
126
127
    public function edit($id, $submoduleId = null)
128
    {
129
        $this->authorizableOptions['edit'] = 'edit-group';
130
131
        return parent::edit($id, $submoduleId);
132
    }
133
134
    public function update($id, $submoduleId = null)
135
    {
136
        $this->authorizableOptions['edit'] = 'edit-group';
137
138
        return parent::update($id, $submoduleId);
139
    }
140
}
141