Completed
Pull Request — master (#23)
by ARCANEDEV
02:45
created

Permission::detachRole()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 0
cp 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php namespace Arcanedev\LaravelAuth\Models;
2
3
use Arcanedev\LaravelAuth\Models\Traits\AuthRoleTrait;
4
use Arcanesoft\Contracts\Auth\Models\Permission as PermissionContract;
5
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract;
6
use Illuminate\Support\Str;
7
8
/**
9
 * Class     Permission
10
 *
11
 * @package  Arcanedev\LaravelAuth\Models
12
 * @author   ARCANEDEV <[email protected]>
13
 *
14
 * @property  int                                            id
15
 * @property  int                                            group_id
16
 * @property  string                                         name
17
 * @property  string                                         slug
18
 * @property  string                                         description
19
 * @property  \Carbon\Carbon                                 created_at
20
 * @property  \Carbon\Carbon                                 updated_at
21
 *
22
 * @property  \Illuminate\Database\Eloquent\Collection             roles
23
 * @property  \Arcanedev\LaravelAuth\Models\PermissionsGroup       group
24
 * @property  \Arcanedev\LaravelAuth\Models\Pivots\PermissionRole  pivot
25
 */
26
class Permission extends AbstractModel implements PermissionContract
27
{
28
    /* -----------------------------------------------------------------
29
     |  Traits
30
     | -----------------------------------------------------------------
31
     */
32
    use AuthRoleTrait;
33
34
    /* -----------------------------------------------------------------
35
     |  Properties
36
     | -----------------------------------------------------------------
37
     */
38
    /**
39
     * The attributes that are mass assignable.
40
     *
41
     * @var array
42
     */
43
    protected $fillable = ['group_id', 'name', 'slug', 'description'];
44
45
    /* -----------------------------------------------------------------
46
     |  Constructor
47
     | -----------------------------------------------------------------
48
     */
49
    /**
50
     * Create a new Eloquent model instance.
51
     *
52
     * @param  array  $attributes
53 225
     */
54
    public function __construct(array $attributes = [])
55 225
    {
56
        $this->setTable(config('laravel-auth.permissions.table', 'permissions'));
57 225
58 225
        parent::__construct($attributes);
59
    }
60
61
    /* -----------------------------------------------------------------
62
     |  Relationships
63
     | -----------------------------------------------------------------
64
     */
65
    /**
66
     * Permission belongs to one group.
67
     *
68
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
69 3
     */
70
    public function group()
71 3
    {
72 3
        return $this->belongsTo(
73 3
            config('laravel-auth.permissions-groups.model', PermissionsGroup::class),
74
            'group_id'
75
        );
76
    }
77
78
    /**
79
     * Permission belongs to many roles.
80
     *
81
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
82
     */
83
    public function roles()
84
    {
85
        return $this
86 90
            ->belongsToMany(
87
                config('laravel-auth.roles.model', Role::class),
88 90
                $this->getPrefix().'permission_role'
89 90
            )
90
            ->withTimestamps();
91
    }
92
93
    /* -----------------------------------------------------------------
94
     |  Setters & Getters
95
     | -----------------------------------------------------------------
96
     */
97
    /**
98
     * Set the slug attribute.
99
     *
100
     * @param  string  $slug
101
     */
102 9
    public function setSlugAttribute($slug)
103
    {
104 9
        $this->attributes['slug'] = $this->slugify($slug);
105
    }
106
107
    /* -----------------------------------------------------------------
108
     |  Main Methods
109
     | -----------------------------------------------------------------
110
     */
111
    /**
112
     * Attach a role to a user.
113
     *
114
     * @param  \Arcanesoft\Contracts\Auth\Models\Role|int  $role
115
     * @param  bool                                    $reload
116
     */
117
    public function attachRole($role, $reload = true)
118 90
    {
119
        if ( ! $this->hasRole($role)) {
120 90
            $this->roles()->attach($role);
121
            $this->loadRoles($reload);
122
        }
123
    }
124
125
    /**
126
     * Sync the roles by its slugs.
127
     *
128
     * @param  array  $slugs
129
     * @param  bool   $reload
130
     *
131
     * @return array
132
     */
133
    public function syncRoles(array $slugs, $reload = true)
134
    {
135
        /** @var \Illuminate\Database\Eloquent\Collection $roles */
136
        $roles  = app(RoleContract::class)->whereIn('slug', $slugs)->get();
137
138
//        event(new SyncingPermissionWithRoles($this, $roles));
139
        $synced = $this->roles()->sync($roles->pluck('id'));
140
//        event(new SyncedPermissionWithRoles($this, $roles, $synced));
141
142
        $this->loadRoles($reload);
143
144
        return $synced;
145
    }
146
147
    /**
148
     * Detach a role from a user.
149
     *
150
     * @param  \Arcanesoft\Contracts\Auth\Models\Role|int  $role
151
     * @param  bool                                        $reload
152
     *
153
     * @return int
154
     */
155
    public function detachRole($role, $reload = true)
156
    {
157
        $results = $this->roles()->detach($role);
158
        $this->loadRoles($reload);
159
160
        return $results;
161
    }
162
163
    /**
164
     * Detach all roles from a user.
165
     *
166
     * @param  bool  $reload
167
     *
168
     * @return int
169
     */
170
    public function detachAllRoles($reload = true)
171
    {
172
        return $this->detachRole(null, $reload);
173
    }
174
175
    /* -----------------------------------------------------------------
176
     |  Check Methods
177
     | -----------------------------------------------------------------
178
     */
179
    /**
180
     * Check if slug is the same as the given value.
181
     *
182
     * @param  string  $value
183
     *
184
     * @return bool
185
     */
186
    public function checkSlug($value)
187
    {
188
        return $this->slug === $this->slugify($value);
189
    }
190
191
    /* -----------------------------------------------------------------
192
     |  Other Functions
193
     | -----------------------------------------------------------------
194
     */
195
    /**
196
     * Slugify the value.
197
     *
198
     * @param  string  $value
199
     *
200
     * @return string
201
     */
202
    protected function slugify($value)
203
    {
204
        return Str::slug($value, config('laravel-auth.permissions.slug-separator', '.'));
205
    }
206
}
207