Completed
Pull Request — master (#1)
by ARCANEDEV
04:49
created

PermissionsGroup::attachPermissionById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4286
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Bases\Model;
4
5
/**
6
 * Class     PermissionsGroup
7
 *
8
 * @package  Arcanedev\LaravelAuth\Models
9
 * @author   ARCANEDEV <[email protected]>
10
 *
11
 * @property  int                                       id
12
 * @property  string                                    name
13
 * @property  string                                    slug
14
 * @property  string                                    description
15
 * @property  \Carbon\Carbon                            created_at
16
 * @property  \Carbon\Carbon                            updated_at
17
 * @property  \Illuminate\Database\Eloquent\Collection  permissions
18
 */
19
class PermissionsGroup extends Model
20
{
21
    /* ------------------------------------------------------------------------------------------------
22
     |  Properties
23
     | ------------------------------------------------------------------------------------------------
24
     */
25
    /**
26
     * The attributes that are mass assignable.
27
     *
28
     * @var array
29
     */
30
    protected $fillable = ['name', 'slug', 'description'];
31
32
    /* ------------------------------------------------------------------------------------------------
33
     |  Constructor
34
     | ------------------------------------------------------------------------------------------------
35
     */
36
    /**
37
     * Create a new Eloquent model instance.
38
     *
39
     * @param  array  $attributes
40
     */
41 40
    public function __construct(array $attributes = [])
42
    {
43 40
        $this->setTable(config('laravel-auth.permissions-group.table', 'permissions_group'));
44
45 40
        parent::__construct($attributes);
46 40
    }
47
48
    /* ------------------------------------------------------------------------------------------------
49
     |  Relationships
50
     | ------------------------------------------------------------------------------------------------
51
     */
52
    /**
53
     * Permissions Groups has many permissions.
54
     *
55
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
56
     */
57 32
    public function permissions()
58
    {
59 32
        return $this->hasMany(Permission::class, 'group_id');
60
    }
61
62
    /* ------------------------------------------------------------------------------------------------
63
     |  CRUD Functions
64
     | ------------------------------------------------------------------------------------------------
65
     */
66
    /**
67
     * Create and attach a permission.
68
     *
69
     * @param  array  $attributes
70
     * @param  bool   $reload
71
     */
72 8
    public function createPermission(array $attributes, $reload = true)
73
    {
74 8
        $this->permissions()->create($attributes);
75
76 8
        if ($reload) {
77 8
            $this->load('permissions');
78 6
        }
79 8
    }
80
81
    /**
82
     * Attach the permission to a group.
83
     *
84
     * @param  \Arcanedev\LaravelAuth\Models\Permission  $permission
85
     * @param  bool                                      $reload
86
     */
87 16
    public function attachPermission(&$permission, $reload = true)
88
    {
89 16
        if ($this->hasPermission($permission)) {
90 4
            return;
91
        }
92
93 16
        $permission = $this->permissions()->save($permission);
94
95 16
        if ($reload) {
96 16
            $this->load('permissions');
97 12
        }
98 16
    }
99
100
    /**
101
     * Attach the permission by id to a group.
102
     *
103
     * @param  int   $id
104
     * @param  bool  $reload
105
     *
106
     * @return \Arcanedev\LaravelAuth\Models\Permission
107
     */
108 4
    public function attachPermissionById($id, $reload = true)
109
    {
110 4
        $permission = $this->permissions()->getRelated()
111 4
            ->where('id', $id)
112 4
            ->first();
113
114 4
        $this->attachPermission($permission, $reload);
115
116 4
        return $permission;
117
    }
118
119
    /**
120
     * Attach the permission from a group.
121
     *
122
     * @param  \Arcanedev\LaravelAuth\Models\Permission  $permission
123
     * @param  bool                                      $reload
124
     */
125 8
    public function detachPermission(&$permission, $reload = true)
126
    {
127 8
        if ( ! $this->hasPermission($permission)) {
128 4
            return;
129
        }
130
131 8
        $permission = $this->getPermission($permission);
132
133 8
        $permission->update([
134 8
            'group_id' => 0,
135 6
        ]);
136
137 8
        if ($reload) {
138 8
            $this->load('permissions');
139 6
        }
140 8
    }
141
142
    /* ------------------------------------------------------------------------------------------------
143
     |  Check Functions
144
     | ------------------------------------------------------------------------------------------------
145
     */
146
    /**
147
     * Check if role has the given permission (Permission Model or Id).
148
     *
149
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
150
     *
151
     * @return bool
152
     */
153 16
    public function hasPermission($id)
154
    {
155 16
        if ($id instanceof Permission) {
156 16
            $id = $id->getKey();
157 12
        }
158
159 16
        return ! is_null($this->getPermission($id));
160
    }
161
162
    /**
163
     * Get a permission from the group.
164
     *
165
     * @param  \Arcanedev\LaravelAuth\Models\Permission|int  $id
166
     *
167
     * @return \Arcanedev\LaravelAuth\Models\Permission|null
168
     */
169 16
    private function getPermission($id)
170
    {
171 16
        if ($id instanceof Permission) {
172 8
            $id = $id->getKey();
173 6
        }
174
175 16
        $this->load('permissions');
176
177 16
        return $this->permissions->filter(function (Permission $permission) use ($id) {
178 16
            return $permission->id == $id;
179 16
        })->first();
180
    }
181
}
182