Completed
Push — master ( 3c03ca...d45e9e )
by Freek
02:07
created

Role::users()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\Permission\Traits\HasPermissions;
7
use Spatie\Permission\Exceptions\RoleDoesNotExist;
8
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
9
use Spatie\Permission\Exceptions\RoleAlreadyExists;
10
use Spatie\Permission\Contracts\Role as RoleContract;
11
use Spatie\Permission\Traits\RefreshesPermissionCache;
12
use Illuminate\Database\Eloquent\Relations\MorphToMany;
13
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
14
15
class Role extends Model implements RoleContract
16
{
17
    use HasPermissions;
18
    use RefreshesPermissionCache;
19
20
    public $guarded = ['id'];
21
22
    public function __construct(array $attributes = [])
23
    {
24
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
25
26
        parent::__construct($attributes);
27
28
        $this->setTable(config('permission.table_names.roles'));
29
    }
30
31 View Code Duplication
    public static function create(array $attributes = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32
    {
33
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
34
35
        if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
36
            throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
37
        }
38
39
        return static::query()->create($attributes);
40
    }
41
42
    /**
43
     * A role may be given various permissions.
44
     */
45
    public function permissions(): BelongsToMany
46
    {
47
        return $this->belongsToMany(
48
            config('permission.models.permission'),
49
            config('permission.table_names.role_has_permissions')
50
        );
51
    }
52
53
    /**
54
     * A role belongs to some users of the model associated with its guard.
55
     */
56 View Code Duplication
    public function users(): MorphToMany
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58
        return $this->morphedByMany(
59
            getModelForGuard($this->attributes['guard_name']),
60
            'model',
61
            config('permission.table_names.model_has_roles'),
62
            'role_id',
63
            'model_id'
64
        );
65
    }
66
67
    /**
68
     * Find a role by its name and guard name.
69
     *
70
     * @param string $name
71
     * @param string|null $guardName
72
     *
73
     * @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role
74
     *
75
     * @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
76
     */
77
    public static function findByName(string $name, $guardName = null): RoleContract
78
    {
79
        $guardName = $guardName ?? config('auth.defaults.guard');
80
81
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
82
83
        if (! $role) {
84
            throw RoleDoesNotExist::create($name);
85
        }
86
87
        return $role;
88
    }
89
90
    /**
91
     * Determine if the user may perform the given permission.
92
     *
93
     * @param string|Permission $permission
94
     *
95
     * @return bool
96
     *
97
     * @throws \Spatie\Permission\Exceptions\GuardMismatch
98
     */
99
    public function hasPermissionTo($permission): bool
100
    {
101
        if (is_string($permission)) {
102
            $permission = app(Permission::class)->findByName($permission, $this->getGuardName());
103
        }
104
105
        if ($permission->guard_name !== $this->getGuardName()) {
106
            throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardName());
107
        }
108
109
        return $this->permissions->contains('id', $permission->id);
110
    }
111
}
112