Passed
Pull Request — 2.x (#586)
by Bekzat
04:25
created

UserController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 94
dl 0
loc 229
ccs 0
cts 58
cp 0
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 25 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
        'name' => [
69
            'title' => 'Name',
70
            'field' => 'name',
71
        ],
72
        'email' => [
73
            'title' => 'Email',
74
            'field' => 'email',
75
            'sort' => true,
76
        ],
77
        'role_value' => [
78
            'title' => 'Role',
79
            'field' => 'role_value',
80
            'sort' => true,
81
            'sortKey' => 'role',
82
        ],
83
    ];
84
85
    /**
86
     * @var array
87
     */
88
    protected $indexOptions = [
89
        'permalink' => false,
90
    ];
91
92
    /**
93
     * @var array
94
     */
95
    protected $fieldsPermissions = [
96
        'role' => 'manage-users',
97
    ];
98
99
    public function __construct(Application $app, Request $request, AuthFactory $authFactory, Config $config)
100
    {
101
        parent::__construct($app, $request);
102
103
        $this->authFactory = $authFactory;
104
        $this->config = $config;
105
106
        $this->removeMiddleware('can:edit');
107
        $this->removeMiddleware('can:delete');
108
        $this->removeMiddleware('can:publish');
109
        $this->middleware('can:manage-users', ['only' => ['index']]);
110
        $this->middleware('can:edit-user,user', ['only' => ['store', 'edit', 'update', 'destroy', 'bulkDelete', 'restore', 'bulkRestore']]);
111
        $this->middleware('can:publish-user', ['only' => ['publish']]);
112
113
        if ($this->config->get('twill.enabled.users-image')) {
114
            $this->indexColumns = [
115
                'image' => [
116
                    'title' => 'Image',
117
                    'thumb' => true,
118
                    'variant' => [
119
                        'role' => 'profile',
120
                        'crop' => 'default',
121
                    ],
122
                ],
123
            ] + $this->indexColumns;
124
        }
125
    }
126
127
    /**
128
     * @param Request $request
129
     * @return array
130
     */
131
    protected function indexData($request)
132
    {
133
        return [
134
            'defaultFilterSlug' => 'published',
135
            'create' => $this->getIndexOption('create') && $this->authFactory->guard('twill_users')->user()->can('manage-users'),
136
            'roleList' => Collection::make(UserRole::toArray()),
137
            'single_primary_nav' => [
138
                'users' => [
139
                    'title' => twillTrans('twill::lang.user-management.users'),
140
                    'module' => true,
141
                ],
142
            ],
143
            'customPublishedLabel' => 'Enabled',
144
            'customDraftLabel' => 'Disabled',
145
        ];
146
    }
147
148
    /**
149
     * @param Request $request
150
     * @return array
151
     * @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
152
     * @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
153
     */
154
    protected function formData($request)
155
    {
156
        $user = $this->authFactory->guard('twill_users')->user();
157
        $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...
158
159
        if ($with2faSettings) {
160
            $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

160
            $user->/** @scrutinizer ignore-call */ 
161
                   generate2faSecretKey();
Loading history...
161
162
            $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

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