Passed
Pull Request — master (#610)
by John
22:48
created

Permission::handle()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 6
eloc 21
nc 6
nop 0
dl 0
loc 32
rs 8.9617
c 2
b 0
f 1
1
<?php
2
3
namespace App\Console\Commands\Manage;
4
5
use Illuminate\Console\Command;
6
use App\Models\Eloquent\User;
7
use App\Models\Eloquent\UserPermission;
8
use Exception;
9
10
class Permission extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature='manage:permission
18
        {--action=list : specific action, can be list, grant or revoke}
19
        {--uid= : the user you want to manage permission}
20
        {--permission= : the permission id number, use list action to check all available permission ids}';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description='Manage user permissions of NOJ';
28
29
    /**
30
     * Create a new command instance.
31
     *
32
     * @return void
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return mixed
43
     */
44
    public function handle()
45
    {
46
        $uid=$this->option('uid');
47
        $permission_id=$this->option('permission');
48
        $action=$this->option('action');
49
50
        if (!in_array($action, ['list', 'grant', 'revoke'])) {
51
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>Unknown Action</>\n");
52
            return;
53
        }
54
55
        if ($action=='list') {
56
            $this->listPermission();
57
            return;
58
        }
59
60
        $userInfo=User::find($uid);
61
        if (is_null($userInfo)) {
62
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>User Not Found</>\n");
63
            return;
64
        }
65
66
        if (!isset(UserPermission::$permInfo[$permission_id])) {
67
            $this->line("\n  <bg=red;fg=white> Exception </> : <fg=yellow>Unknown Permission</>\n");
68
            return;
69
        }
70
        $permissionInfo=UserPermission::$permInfo[$permission_id];
71
72
        if ($action=='grant') {
73
            $this->grantPermission($uid, $permission_id, $permissionInfo);
74
        } else {
75
            $this->revokePermission($uid, $permission_id, $permissionInfo);
76
        }
77
    }
78
79
    protected function listPermission()
80
    {
81
        $headers=['ID', 'Permission'];
82
        $permInfo=[];
83
        foreach (UserPermission::$permInfo as $permID=>$permDesc) {
84
            $permInfo[]=[$permID, $permDesc];
85
        }
86
        $this->table($headers, $permInfo);
87
    }
88
89
    protected function grantPermission($uid, $permission_id, $permissionInfo)
90
    {
91
        $this->line("<fg=yellow>Granting:  </>$permissionInfo");
92
93
        $permissionExists=UserPermission::where([
94
            'user_id' => $uid,
95
            'permission_id' => $permission_id,
96
        ])->count();
97
98
        if (!$permissionExists) {
99
            UserPermission::create([
100
                'user_id' => $uid,
101
                'permission_id' => $permission_id,
102
            ]);
103
        }
104
105
        $this->line("<fg=green>Granted:   </>$permissionInfo");
106
    }
107
108
    protected function revokePermission($uid, $permission_id, $permissionInfo)
109
    {
110
        $this->line("<fg=yellow>Revoking:  </>$permissionInfo");
111
112
        $permissionExists=UserPermission::where([
113
            'user_id' => $uid,
114
            'permission_id' => $permission_id,
115
        ])->count();
116
117
        if ($permissionExists) {
118
            UserPermission::where([
119
                'user_id' => $uid,
120
                'permission_id' => $permission_id,
121
            ])->delete();
122
        }
123
124
        $this->line("<fg=green>Revoked:   </>$permissionInfo");
125
    }
126
}
127