Issues (17)

src/Traits/HasRoles.php (5 issues)

1
<?php
2
3
namespace Helldar\Roles\Traits;
4
5
use Helldar\Roles\Constants\Tables;
6
use Helldar\Roles\Facades\Config;
7
use Helldar\Roles\Facades\Database\Search;
8
use Helldar\Roles\Models\Permission;
9
use Helldar\Roles\Models\Role;
10
use Illuminate\Database\Eloquent\Builder;
11
use Illuminate\Database\Eloquent\Model;
12
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
13
14
/**
15
 * @property \Helldar\Roles\Models\Role[]|\Illuminate\Database\Eloquent\Collection $roles
16
 * @property \Helldar\Roles\Models\Permission[]|\Illuminate\Database\Eloquent\Collection $permissions
17
 *
18
 * @mixin \Illuminate\Database\Eloquent\Model
19
 */
20
trait HasRoles
21
{
22
    use Searchable;
23
    use Cacheable;
24
25
    /**
26
     * @return \Helldar\Roles\Models\BaseModel|\Illuminate\Database\Eloquent\Relations\BelongsToMany
27
     */
28 138
    public function roles(): BelongsToMany
29
    {
30 138
        return $this->belongsToMany(Role::class, Tables::USER_ROLE);
0 ignored issues
show
It seems like belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        return $this->/** @scrutinizer ignore-call */ belongsToMany(Role::class, Tables::USER_ROLE);
Loading history...
31
    }
32
33 30
    public function hasRootRole(): bool
34
    {
35
        return $this->cache(__FUNCTION__, function () {
36 30
            return $this->roles()
37 30
                ->where('is_root', true)
38 30
                ->exists();
39 30
        });
40
    }
41
42
    public function createRole(string $slug, string $title = null, bool $is_root = false): Model
43
    {
44
        return $this->roles()->create(compact('slug', 'title', 'is_root'));
45
    }
46
47
    /**
48
     * @param  \Helldar\Roles\Models\Role|string  $role
49
     *
50
     * @throws \Throwable
51
     */
52 30
    public function assignRole($role): void
53
    {
54 30
        $role = $this->findRole($role);
55
56 30
        $this->roles()->attach($role->id);
57 30
    }
58
59
    /**
60
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
61
     *
62
     * @throws \Throwable
63
     */
64 18
    public function assignRoles(...$roles): void
65
    {
66 18
        foreach ($roles as $role) {
67 18
            $this->assignRole($role);
0 ignored issues
show
$role of type Helldar\Roles\Models\Role[]|string[] is incompatible with the type Helldar\Roles\Models\Role|string expected by parameter $role of Helldar\Roles\Traits\HasRoles::assignRole(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            $this->assignRole(/** @scrutinizer ignore-type */ $role);
Loading history...
68
        }
69 18
    }
70
71
    /**
72
     * @throws \Throwable
73
     */
74 6
    public function assignDefaultRole(): void
75
    {
76 6
        if ($role = Config::defaultRole()) {
77 6
            $this->assignRole($role);
78
        }
79 6
    }
80
81
    /**
82
     * @param  \Helldar\Roles\Models\Role|string  $role
83
     *
84
     * @throws \Throwable
85
     */
86 6
    public function revokeRole($role): void
87
    {
88 6
        $role = $this->findRole($role);
89
90 6
        $this->roles()->detach($role->id);
91 6
    }
92
93
    /**
94
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
95
     *
96
     * @throws \Throwable
97
     */
98 6
    public function revokeRoles(...$roles): void
99
    {
100 6
        foreach ($roles as $role) {
101 6
            $this->revokeRole($role);
0 ignored issues
show
$role of type Helldar\Roles\Models\Role[]|string[] is incompatible with the type Helldar\Roles\Models\Role|string expected by parameter $role of Helldar\Roles\Traits\HasRoles::revokeRole(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
            $this->revokeRole(/** @scrutinizer ignore-type */ $role);
Loading history...
102
        }
103 6
    }
104
105
    /**
106
     * @param  int[]  $roles_ids
107
     */
108 138
    public function syncRoles(array $roles_ids): void
109
    {
110 138
        $this->roles()->sync($roles_ids);
111 138
    }
112
113
    /**
114
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
115
     *
116
     * @return bool
117
     */
118 24
    public function hasRole(...$roles): bool
119
    {
120
        return $this->cache(__FUNCTION__, function () use ($roles) {
121 24
            return Search::by($this->roles(), $roles)->exists();
122 24
        }, $roles);
123
    }
124
125
    /**
126
     * @param  \Helldar\Roles\Models\Role[]|string[]  $roles
127
     *
128
     * @return bool
129
     */
130 24
    public function hasRoles(...$roles): bool
131
    {
132
        return $this->cache(__FUNCTION__, function () use ($roles) {
133 24
            $count = Search::by($this->roles(), $roles)->count();
134
135 24
            return $count === count($roles);
136 24
        }, $roles);
137
    }
138
139
    /**
140
     * @return \Helldar\Roles\Models\BaseModel|\Illuminate\Database\Eloquent\Relations\BelongsToMany
141
     */
142 138
    public function permissions(): BelongsToMany
143
    {
144 138
        return $this->belongsToMany(Permission::class, Tables::USER_PERMISSION);
145
    }
146
147
    public function createPermission(string $slug, string $title = null): Model
148
    {
149
        return $this->permissions()->create(compact('slug', 'title'));
150
    }
151
152
    /**
153
     * @param  \Helldar\Roles\Models\Permission|string  $permission
154
     *
155
     * @throws \Throwable
156
     */
157 24
    public function assignPermission($permission): void
158
    {
159 24
        $permission = $this->findPermission($permission);
160
161 24
        $this->permissions()->attach($permission->id);
162 24
    }
163
164
    /**
165
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
166
     *
167
     * @throws \Throwable
168
     */
169 18
    public function assignPermissions(...$permissions): void
170
    {
171 18
        foreach ($permissions as $permission) {
172 18
            $this->assignPermission($permission);
0 ignored issues
show
$permission of type Helldar\Roles\Models\Permission[]|string[] is incompatible with the type Helldar\Roles\Models\Permission|string expected by parameter $permission of Helldar\Roles\Traits\HasRoles::assignPermission(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

172
            $this->assignPermission(/** @scrutinizer ignore-type */ $permission);
Loading history...
173
        }
174 18
    }
175
176
    /**
177
     * @param  \Helldar\Roles\Models\Permission|string  $permission
178
     *
179
     * @throws \Throwable
180
     */
181 6
    public function revokePermission($permission): void
182
    {
183 6
        $permission = $this->findPermission($permission);
184
185 6
        $this->permissions()->detach($permission->id);
186 6
    }
187
188
    /**
189
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
190
     *
191
     * @throws \Throwable
192
     */
193 6
    public function revokePermissions(...$permissions): void
194
    {
195 6
        foreach ($permissions as $permission) {
196 6
            $this->revokePermission($permission);
0 ignored issues
show
$permission of type Helldar\Roles\Models\Permission[]|string[] is incompatible with the type Helldar\Roles\Models\Permission|string expected by parameter $permission of Helldar\Roles\Traits\HasRoles::revokePermission(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

196
            $this->revokePermission(/** @scrutinizer ignore-type */ $permission);
Loading history...
197
        }
198 6
    }
199
200
    /**
201
     * @param  int[]  $permissions_ids
202
     */
203 138
    public function syncPermissions(array $permissions_ids): void
204
    {
205 138
        $this->permissions()->sync($permissions_ids);
206 138
    }
207
208
    /**
209
     * @param  \Helldar\Roles\Models\Permission[]|int[]|string[]  $permission
210
     *
211
     * @return bool
212
     */
213 24
    public function hasPermission(...$permission): bool
214
    {
215
        return $this->cache(__FUNCTION__, function () use ($permission) {
216 24
            $first = Search::by($this->permissions(), $permission)->exists();
217
218 24
            $second = $this->roles()
219
                ->whereHas('permissions', function (Builder $builder) use ($permission) {
220 24
                    return Search::by($builder, $permission);
221 24
                })->exists();
222
223 24
            return $first || $second;
224 24
        }, $permission);
225
    }
226
227
    /**
228
     * @param  \Helldar\Roles\Models\Permission[]|string[]  $permissions
229
     *
230
     * @return bool
231
     */
232 24
    public function hasPermissions(...$permissions): bool
233
    {
234
        return $this->cache(__FUNCTION__, function () use ($permissions) {
235 24
            $count = Search::by($this->permissions(), $permissions)->count();
236
237 24
            return $count === count($permissions);
238 24
        }, $permissions);
239
    }
240
}
241