Passed
Pull Request — master (#405)
by
unknown
05:59
created

UserController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Test Coverage

Coverage 93.22%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 95
dl 0
loc 231
ccs 55
cts 59
cp 0.9322
rs 10
c 2
b 0
f 0
wmc 15

7 Methods

Rating   Name   Duplication   Size   Complexity  
A indexItemData() 0 7 3
A getIndexTableMainFilters() 0 23 2
A indexData() 0 14 2
A __construct() 0 43 2
A getRequestFilters() 0 3 1
A getIndexOption() 0 7 2
A formData() 0 23 3
1
<?php
2
3
namespace A17\Twill\Http\Controllers\Admin;
4
5
use A17\Twill\Models\Enums\UserRole;
6
use Illuminate\Config\Repository as Config;
7
use Illuminate\Contracts\Auth\Factory as AuthFactory;
8
use Illuminate\Contracts\Foundation\Application;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Collection;
11
use PragmaRX\Google2FAQRCode\Google2FA;
12
13
class UserController extends ModuleController
14
{
15
    /**
16
     * @var Config
17
     */
18
    protected $config;
19
20
    /**
21
     * @var AuthFactory
22
     */
23
    protected $authFactory;
24
25
    /**
26
     * @var string
27
     */
28
    protected $namespace = 'A17\Twill';
29
30
    /**
31
     * @var string
32
     */
33
    protected $moduleName = 'users';
34
35
    /**
36
     * @var string[]
37
     */
38
    protected $indexWith = ['medias'];
39
40
    /**
41
     * @var array
42
     */
43
    protected $defaultOrders = ['name' => 'asc'];
44
45
    /**
46
     * @var array
47
     */
48
    protected $defaultFilters = [
49
        'search' => 'search',
50
    ];
51
52
    /**
53
     * @var array
54
     */
55
    protected $filters = [
56
        'role' => 'role',
57
    ];
58
59
    /**
60
     * @var string
61
     */
62
    protected $titleColumnKey = 'name';
63
64
    /**
65
     * @var array
66
     */
67
    protected $indexColumns;
68
69
    /**
70
     * @var array
71
     */
72
    protected $indexOptions = [
73
        'permalink' => false,
74
    ];
75
76
    /**
77
     * @var array
78
     */
79
    protected $fieldsPermissions = [
80
        'role' => 'manage-users',
81
    ];
82
83 6
    public function __construct(Application $app, Request $request, AuthFactory $authFactory, Config $config)
84
    {
85 6
        parent::__construct($app, $request);
86
87 6
        $this->authFactory = $authFactory;
88 6
        $this->config = $config;
89
90 6
        $this->removeMiddleware('can:edit');
91 6
        $this->removeMiddleware('can:delete');
92 6
        $this->removeMiddleware('can:publish');
93 6
        $this->middleware('can:manage-users', ['only' => ['index']]);
94 6
        $this->middleware('can:edit-user,user', ['only' => ['store', 'edit', 'update', 'destroy', 'bulkDelete', 'restore', 'bulkRestore']]);
95 6
        $this->middleware('can:publish-user', ['only' => ['publish']]);
96
        
97 6
        $this->indexColumns = [
98
            'name' => [
99
                'title' => 'Name',
100
                'field' => 'name',
101
            ],
102
            'email' => [
103
                'title' => 'Email',
104
                'field' => 'email',
105
                'sort' => true,
106
            ],
107
            'role_value' => [
108
                'title' => 'Role',
109
                'field' => 'role_value',
110
                'sort' => true,
111
                'sortKey' => 'role',
112
            ],
113
        ];
114
115 6
        if ($this->config->get('twill.enabled.users-image')) {
116 6
            $this->indexColumns = [
117
                'image' => [
118
                    'title' => 'Image',
119
                    'thumb' => true,
120
                    'variant' => [
121
                        'role' => 'profile',
122
                        'crop' => 'default',
123
                    ],
124
                ],
125 6
            ] + $this->indexColumns;
126
        }
127 6
    }
128
129
    /**
130
     * @param Request $request
131
     * @return array
132
     */
133 1
    protected function indexData($request)
134
    {
135
        return [
136 1
            'defaultFilterSlug' => 'published',
137 1
            'create' => $this->getIndexOption('create') && $this->authFactory->guard('twill_users')->user()->can('manage-users'),
138 1
            'roleList' => Collection::make(UserRole::toArray()),
139
            'single_primary_nav' => [
140
                'users' => [
141 1
                    'title' => __('twill::lang.user-management.users'),
142
                    'module' => true,
143
                ],
144
            ],
145 1
            'customPublishedLabel' => 'Enabled',
146 1
            'customDraftLabel' => 'Disabled',
147
        ];
148
    }
149
150
    /**
151
     * @param Request $request
152
     * @return array
153
     * @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
154
     * @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
155
     */
156 1
    protected function formData($request)
157
    {
158 1
        $user = $this->authFactory->guard('twill_users')->user();
159 1
        $with2faSettings = $this->config->get('twill.enabled.users-2fa') && $user->id == $request->route('user');
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
160
161 1
        if ($with2faSettings) {
162 1
            $user->generate2faSecretKey();
0 ignored issues
show
Bug introduced by
The method generate2faSecretKey() 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

162
            $user->/** @scrutinizer ignore-call */ 
163
                   generate2faSecretKey();
Loading history...
163
164 1
            $qrCode = $user->get2faQrCode();
0 ignored issues
show
Bug introduced by
The method get2faQrCode() 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

164
            /** @scrutinizer ignore-call */ 
165
            $qrCode = $user->get2faQrCode();
Loading history...
165
        }
166
167
        return [
168 1
            'roleList' => Collection::make(UserRole::toArray()),
169
            'single_primary_nav' => [
170
                'users' => [
171 1
                    'title' => __('twill::lang.user-management.users'),
172
                    'module' => true,
173
                ],
174
            ],
175 1
            'customPublishedLabel' => __('twill::lang.user-management.enabled'),
176 1
            'customDraftLabel' => __('twill::lang.user-management.disabled'),
177 1
            'with2faSettings' => $with2faSettings,
178 1
            'qrCode' => $qrCode ?? null,
179
        ];
180
    }
181
182
    /**
183
     * @return array
184
     */
185 1
    protected function getRequestFilters()
186
    {
187 1
        return json_decode($this->request->get('filter'), true) ?? ['status' => 'published'];
188
    }
189
190
    /**
191
     * @param \Illuminate\Database\Eloquent\Collection $items
192
     * @param array $scopes
193
     * @return array
194
     */
195 1
    public function getIndexTableMainFilters($items, $scopes = [])
196
    {
197 1
        $statusFilters = [];
198
199 1
        array_push($statusFilters, [
200 1
            'name' => __('twill::lang.user-management.active'),
201 1
            'slug' => 'published',
202 1
            'number' => $this->repository->getCountByStatusSlug('published'),
203
        ], [
204 1
            'name' => __('twill::lang.user-management.disabled'),
205 1
            'slug' => 'draft',
206 1
            'number' => $this->repository->getCountByStatusSlug('draft'),
207
        ]);
208
209 1
        if ($this->getIndexOption('restore')) {
210 1
            array_push($statusFilters, [
211 1
                'name' => __('twill::lang.user-management.trash'),
212 1
                'slug' => 'trash',
213 1
                'number' => $this->repository->getCountByStatusSlug('trash'),
214
            ]);
215
        }
216
217 1
        return $statusFilters;
218
    }
219
220
    /**
221
     * @param string $option
222
     * @return bool
223
     */
224 2
    protected function getIndexOption($option)
225
    {
226 2
        if (in_array($option, ['publish', 'delete', 'restore'])) {
227 1
            return $this->authFactory->guard('twill_users')->user()->can('manage-users');
228
        }
229
230 2
        return parent::getIndexOption($option);
231
    }
232
233
    /**
234
     * @param \A17\Twill\Models\Model $item
235
     * @return array
236
     */
237
    protected function indexItemData($item)
238
    {
239
240
        $user = $this->authFactory->guard('twill_users')->user();
241
        $canEdit = $user->can('manage-users') || $user->id === $item->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
242
        return [
243
            'edit' => $canEdit ? $this->getModuleRoute($item->id, 'edit') : null,
244
        ];
245
    }
246
}
247