UserProvider   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 8
Bugs 0 Features 2
Metric Value
eloc 24
c 8
b 0
f 2
dl 0
loc 64
rs 10
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A deleteAdmin() 0 5 1
A findUser() 0 3 1
A makeAdmin() 0 21 3
A paginateAdmins() 0 3 1
A getUserModel() 0 3 1
A makeSuperAdminRole() 0 10 1
A getAdmins() 0 3 1
1
<?php
2
3
namespace EasyPanel\Support\User;
4
5
use App\Models\User;
0 ignored issues
show
Bug introduced by
The type App\Models\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Iya30n\DynamicAcl\Models\Role;
7
8
class UserProvider
9
{
10
11
    public function makeAdmin($id, $is_super = false)
12
    {
13
        $user = $this->findUser($id);
14
15
        if ($user->panelAdmin()->exists()){
16
            return [
17
                'type' => 'error',
18
                'message' => 'User already is an admin!'
19
            ];
20
        }
21
22
        $user->panelAdmin()->create([
23
            'is_superuser' => $is_super,
24
        ]);
25
26
        if($is_super)
27
            $this->makeSuperAdminRole($user);
28
29
        return [
30
            'type' => 'success',
31
            'message' => "User '$id' was converted to an admin",
32
        ];
33
    }
34
35
    public function getAdmins()
36
    {
37
        return $this->getUserModel()::query()->whereHas('panelAdmin')->with('panelAdmin')->get();
38
    }
39
40
    public function paginateAdmins($perPage = 20)
41
    {
42
        return $this->getUserModel()::query()->whereHas('panelAdmin')->with('panelAdmin')->paginate($perPage);
43
    }
44
45
    public function findUser($id)
46
    {
47
        return $this->getUserModel()::query()->findOrFail($id);
48
    }
49
50
    public function deleteAdmin($id)
51
    {
52
        $user = $this->findUser($id);
53
54
        $user->panelAdmin()->delete();
55
    }
56
57
    private function getUserModel()
58
    {
59
        return config('easy_panel.user_model') ?? User::class;
60
    }
61
62
    private function makeSuperAdminRole($user)
63
    {
64
        $role = Role::firstOrCreate(['name' => 'super_admin'], [
65
            'name' => 'super_admin',
66
            'permissions' => [
67
                'fullAccess' => 1
68
            ]
69
        ]);
70
71
        $role->users()->sync([$user->id]);
72
    }
73
74
}
75