Permission::users()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace z1haze\Acl\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use z1haze\Acl\Contracts\AclPermissionInterface;
7
use z1haze\Acl\Exceptions\UserNotFoundException;
8
use z1haze\Acl\Traits\UserAndPermission;
9
10
/**
11
 * @property mixed id
12
 * @method int detach
13
 */
14
class Permission extends Model implements AclPermissionInterface
15
{
16
    use UserAndPermission;
17
18
    /**
19
     * The attributes that should be cast to native types.
20
     *
21
     * @var array
22
     */
23
    protected $casts = ['id' => 'integer'];
24
25
    /**
26
     * The attributes that aren't mass assignable.
27
     *
28
     * @var array
29
     */
30
    protected $guarded = ['id', 'created_id', 'updated_at'];
31
32
    /**
33
     * PERMISSION
34
     * A Permission belongs to a level
35
     *
36
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
37
     */
38 10
    public function level()
39
    {
40 10
        return $this->belongsTo(config('laravel-acl.level', Level::class));
41
    }
42
43
    /**
44
     * PERMISSION
45
     * A permission belongs to many users
46
     *
47
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
48
     */
49 8
    public function users()
50
    {
51 8
        return $this->belongsToMany(config('laravel-acl.user'));
52
    }
53
54
    /**
55
     * PERMISSION
56
     * Return the relationship of users
57
     *
58
     * @return mixed
59
     */
60 1
    public function getUsers()
61
    {
62 1
        return $this->users;
63
    }
64
65
    /**
66
     * PERMISSION
67
     * Assign a single user to a permission
68
     *
69
     * @param $user
70
     */
71 3
    public function addUser($user)
72
    {
73 3
        $this->addUsers([$user]);
74 2
    }
75
76
    /**
77
     * PERMISSION
78
     * Assign an array of users to a permission
79
     *
80
     * @param $users
81
     */
82 6
    public function addUsers($users)
83
    {
84 6
        $userObjects = $this->buildUserArray($users);
85
86 5
        $this->users()->attach($userObjects);
87 5
    }
88
89
    /**
90
     * PERMISSION
91
     * Remove a single user from a permission
92
     *
93
     * @param $user
94
     */
95 1
    public function removeUser($user)
96
    {
97 1
        $this->removeUsers([$user]);
98 1
    }
99
100
    /**
101
     * PERMISSION
102
     * Remove an array of users from a permission
103
     *
104
     * @param $users
105
     */
106 2
    public function removeUsers($users)
107
    {
108 2
        $userObjects = $this->buildUserArray($users);
109
110 2
        $this->users()->detach($userObjects);
111 2
    }
112
113
114
    /* ------------------------------------------------------------------------------------------------
115
     |  Other Functions
116
     | ------------------------------------------------------------------------------------------------
117
     */
118
119
    /**
120
     * Helper function to process users and return an
121
     * array of user id's
122
     *
123
     * @param $users
124
     * @return array
125
     */
126 6
    protected function buildUserArray($users)
127
    {
128 6
        $userArray = [];
129
130 6
        foreach ($users as $user) {
131 6
            $user = aclGetUser($user);
132 5
            array_push($userArray, $user->id);
133
        }
134
135 5
        return $userArray;
136
    }
137
138
    /**
139
     * Handle model events
140
     */
141 55
    public static function boot()
142
    {
143 55
        parent::boot();
144
145 55
        static::deleting(function ($permission) {
146 1
            $permission->users()->detach();
147 1
            $permission->level()->dissociate()->save();
148 55
        });
149 55
    }
150
151 55
    public function __construct(array $attributes = [])
152
    {
153 55
        parent::__construct($attributes);
154
155 55
        $this->table = config('laravel-acl.tables.permission');
156 55
    }
157
}
158