Passed
Push — feature/permissions-management ( 41e93d )
by
unknown
11:22 queued 04:04
created

RoleController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 64
dl 0
loc 133
rs 10
c 1
b 0
f 0
wmc 12

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A indexData() 0 4 1
A formData() 0 5 1
A indexItemData() 0 5 3
A update() 0 5 1
A edit() 0 5 1
A getIndexOption() 0 7 2
A getIndexItems() 0 5 1
A index() 0 7 1
1
<?php
2
3
namespace A17\Twill\Http\Controllers\Admin;
4
5
use A17\Twill\Models\Permission;
6
use Illuminate\Contracts\Foundation\Application;
7
use Illuminate\Http\Request;
8
9
class RoleController extends ModuleController
10
{
11
    protected $namespace = 'A17\Twill';
12
13
    protected $moduleName = 'roles';
14
15
    protected $indexWith = ['medias'];
16
17
    protected $defaultOrders = ['name' => 'asc'];
18
19
    protected $defaultFilters = [
20
        'search' => 'search',
21
    ];
22
23
    protected $titleColumnKey = 'name';
24
25
    protected $indexOptions = [
26
        'permalink' => false,
27
    ];
28
29
    protected $labels = [
30
        'published' => 'twill::lang.permissions.roles.published',
31
        'draft' => 'twill::lang.permissions.roles.draft',
32
        'listing' => [
33
            'filter' => [
34
                'published' => 'twill::lang.permissions.roles.published',
35
                'draft' => 'twill::lang.permissions.roles.draft',
36
            ],
37
        ],
38
    ];
39
40
    public function __construct(Application $app, Request $request)
41
    {
42
        parent::__construct($app, $request);
43
        $this->middleware('can:edit-user-roles');
44
45
        $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...
46
            'users' => [
47
                'title' => twillTrans('twill::lang.user-management.users'),
48
                'module' => true,
49
                'can' => 'edit-users',
50
            ],
51
            'roles' => [
52
                'title' => twillTrans('twill::lang.permissions.roles.title'),
53
                'module' => true,
54
                'active' => true,
55
                'can' => 'edit-user-roles',
56
            ],
57
            'groups' => [
58
                'title' => twillTrans('twill::lang.permissions.groups.title'),
59
                'module' => true,
60
                'can' => 'edit-user-groups',
61
            ],
62
        ];
63
    }
64
65
    protected $indexColumns = [
66
        'name' => [
67
            'title' => 'Name',
68
            'field' => 'name',
69
            'sort' => true,
70
        ],
71
        'created_at' => [
72
            'title' => 'Date created',
73
            'field' => 'created_at',
74
            'sort' => true
75
        ],
76
        'users' => [
77
            'title' => 'Users',
78
            'field' => 'users_count',
79
            'html' => true
80
        ]
81
    ];
82
83
    protected function indexData($request)
84
    {
85
        return [
86
            'primary_navigation' => $this->primaryNavigation,
87
        ];
88
    }
89
90
    protected function getIndexItems($scopes = [], $forcePagination = false)
91
    {
92
        $scopes = $scopes + ['accessible' => true];
93
94
        return parent::getIndexItems($scopes, $forcePagination);
95
    }
96
97
    protected function getIndexOption($option, $item = null)
98
    {
99
        if (in_array($option, ['publish', 'bulkEdit', 'create'])) {
100
            return auth('twill_users')->user()->can('edit-user-roles');
101
        }
102
103
        return parent::getIndexOption($option);
104
    }
105
106
    protected function formData($request)
107
    {
108
        return [
109
            'primary_navigation' => $this->primaryNavigation,
110
            'permission_modules' => Permission::permissionableParentModuleItems(),
111
        ];
112
    }
113
114
    protected function indexItemData($item)
115
    {
116
        $canEdit = auth('twill_users')->user()->can('edit-user-roles') && ($item->canEdit ?? true);
117
118
        return ['edit' => $canEdit ? $this->getModuleRoute($item->id, 'edit') : null];
119
    }
120
121
    public function index($parentModuleId = null)
122
    {
123
        // Superadmins can reorder groups to determine the access-level of each one.
124
        // A given group can't edit other groups with a higher access-level.
125
        $this->indexOptions['reorder'] = auth('twill_users')->user()->isSuperAdmin();
0 ignored issues
show
Bug introduced by
The method isSuperAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

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

125
        $this->indexOptions['reorder'] = auth('twill_users')->user()->/** @scrutinizer ignore-call */ isSuperAdmin();
Loading history...
126
127
        return parent::index($parentModuleId);
128
    }
129
130
    public function edit($id, $submoduleId = null)
131
    {
132
        $this->authorizableOptions['edit'] = 'edit-role';
133
134
        return parent::edit($id, $submoduleId);
135
    }
136
137
    public function update($id, $submoduleId = null)
138
    {
139
        $this->authorizableOptions['edit'] = 'edit-role';
140
141
        return parent::update($id, $submoduleId);
142
    }
143
}
144