Completed
Pull Request — master (#23)
by ARCANEDEV
07:14
created

Permission::syncRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 2
crap 1
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
     */
54 225
    public function __construct(array $attributes = [])
55
    {
56 225
        $this->setTable(config('laravel-auth.permissions.table', 'permissions'));
57
58 225
        parent::__construct($attributes);
59 225
    }
60
61
    /* -----------------------------------------------------------------
62
     |  Relationships
63
     | -----------------------------------------------------------------
64
     */
65
    /**
66
     * Permission belongs to one group.
67
     *
68
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
69
     */
70 3
    public function group()
71
    {
72 3
        return $this->belongsTo(
73 3
            config('laravel-auth.permissions-groups.model', PermissionsGroup::class),
74 3
            'group_id'
75
        );
76
    }
77
78
    /**
79
     * Permission belongs to many roles.
80
     *
81
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
82
     */
83 27
    public function roles()
84
    {
85
        return $this
86 27
            ->belongsToMany(
87 27
                config('laravel-auth.roles.model', Role::class),
88 27
                $this->getPrefix().'permission_role'
89
            )
90 27
            ->withTimestamps();
91
    }
92
93
    /* -----------------------------------------------------------------
94
     |  Setters & Getters
95
     | -----------------------------------------------------------------
96
     */
97
    /**
98
     * Set the slug attribute.
99
     *
100
     * @param  string  $slug
101
     */
102 90
    public function setSlugAttribute($slug)
103
    {
104 90
        $this->attributes['slug'] = $this->slugify($slug);
105 90
    }
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 18
    public function attachRole($role, $reload = true)
118
    {
119 18
        if ( ! $this->hasRole($role)) {
120 18
            $this->roles()->attach($role);
121 18
            $this->loadRoles($reload);
122
        }
123 18
    }
124
125
    /**
126
     * Sync the roles by its slugs.
127
     *
128
     * @param  array  $slugs
129
     * @param  bool   $reload
130
     *
131
     * @return array
132
     */
133 3
    public function syncRoles(array $slugs, $reload = true)
134
    {
135
        /** @var \Illuminate\Database\Eloquent\Collection $roles */
136 3
        $roles  = app(RoleContract::class)->whereIn('slug', $slugs)->get();
137
138
//        event(new SyncingPermissionWithRoles($this, $roles));
139 3
        $synced = $this->roles()->sync($roles->pluck('id'));
140
//        event(new SyncedPermissionWithRoles($this, $roles, $synced));
141
142 3
        $this->loadRoles($reload);
143
144 3
        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 6
    public function detachRole($role, $reload = true)
156
    {
157 6
        $results = $this->roles()->detach($role);
158 6
        $this->loadRoles($reload);
159
160 6
        return $results;
161
    }
162
163
    /**
164
     * Detach all roles from a user.
165
     *
166
     * @param  bool  $reload
167
     *
168
     * @return int
169
     */
170 3
    public function detachAllRoles($reload = true)
171
    {
172 3
        return $this->detachRole(null, $reload);
173
    }
174
175
    /* -----------------------------------------------------------------
176
     |  Check Methods
177
     | -----------------------------------------------------------------
178
     */
179
    /**
180
     * Check if the permission has the same slug.
181
     *
182
     * @param  string  $slug
183
     *
184
     * @return bool
185
     */
186 18
    public function hasSlug($slug)
187
    {
188 18
        return $this->slug === $this->slugify($slug);
189
    }
190
191
    /* -----------------------------------------------------------------
192
     |  Other Functions
193
     | -----------------------------------------------------------------
194
     */
195
    /**
196
     * Slugify the value.
197
     *
198
     * @param  string  $value
199
     *
200
     * @return string
201
     */
202 90
    protected function slugify($value)
203
    {
204 90
        return Str::slug($value, config('laravel-auth.permissions.slug-separator', '.'));
205
    }
206
}
207