Completed
Push — master ( 20b978...5c1cc4 )
by Chris
01:45 queued 35s
created

Role::getTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\Permission\Models;
4
5
use Spatie\Permission\Guard;
6
use Illuminate\Database\Eloquent\Model;
7
use Spatie\Permission\Traits\HasPermissions;
8
use Spatie\Permission\Exceptions\RoleDoesNotExist;
9
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
10
use Spatie\Permission\Exceptions\RoleAlreadyExists;
11
use Spatie\Permission\Contracts\Role as RoleContract;
12
use Spatie\Permission\Traits\RefreshesPermissionCache;
13
use Illuminate\Database\Eloquent\Relations\MorphToMany;
14
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
15
16
class Role extends Model implements RoleContract
17
{
18
    use HasPermissions;
19
    use RefreshesPermissionCache;
20
21
    protected $guarded = ['id'];
22
23
    public function __construct(array $attributes = [])
24
    {
25
        $attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
26
27
        parent::__construct($attributes);
28
    }
29
30
    public function getTable()
31
    {
32
        return config('permission.table_names.roles', parent::getTable());
33
    }
34
35
    public static function create(array $attributes = [])
36
    {
37
        $attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
38
39
        if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
40
            throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
41
        }
42
43
        return static::query()->create($attributes);
44
    }
45
46
    /**
47
     * A role may be given various permissions.
48
     */
49
    public function permissions(): BelongsToMany
50
    {
51
        return $this->belongsToMany(
52
            config('permission.models.permission'),
53
            config('permission.table_names.role_has_permissions'),
54
            'role_id',
55
            'permission_id'
56
        );
57
    }
58
59
    /**
60
     * A role belongs to some users of the model associated with its guard.
61
     */
62 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...
63
    {
64
        return $this->morphedByMany(
65
            getModelForGuard($this->attributes['guard_name']),
66
            'model',
67
            config('permission.table_names.model_has_roles'),
68
            'role_id',
69
            config('permission.column_names.model_morph_key')
70
        );
71
    }
72
73
    /**
74
     * Find a role by its name and guard name.
75
     *
76
     * @param string $name
77
     * @param string|null $guardName
78
     *
79
     * @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role
80
     *
81
     * @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
82
     */
83 View Code Duplication
    public static function findByName(string $name, $guardName = null): RoleContract
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...
84
    {
85
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
86
87
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
88
89
        if (! $role) {
90
            throw RoleDoesNotExist::named($name);
91
        }
92
93
        return $role;
94
    }
95
96 View Code Duplication
    public static function findById(int $id, $guardName = null): RoleContract
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...
97
    {
98
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
99
100
        $role = static::where('id', $id)->where('guard_name', $guardName)->first();
101
102
        if (! $role) {
103
            throw RoleDoesNotExist::withId($id);
104
        }
105
106
        return $role;
107
    }
108
109
    /**
110
     * Find or create role by its name (and optionally guardName).
111
     *
112
     * @param string $name
113
     * @param string|null $guardName
114
     *
115
     * @return \Spatie\Permission\Contracts\Role
116
     */
117 View Code Duplication
    public static function findOrCreate(string $name, $guardName = null): RoleContract
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...
118
    {
119
        $guardName = $guardName ?? Guard::getDefaultName(static::class);
120
121
        $role = static::where('name', $name)->where('guard_name', $guardName)->first();
122
123
        if (! $role) {
124
            return static::query()->create(['name' => $name, 'guard_name' => $guardName]);
125
        }
126
127
        return $role;
128
    }
129
130
    /**
131
     * Determine if the user may perform the given permission.
132
     *
133
     * @param string|Permission $permission
134
     *
135
     * @return bool
136
     *
137
     * @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch
138
     */
139
    public function hasPermissionTo($permission): bool
140
    {
141
        if (config('permission.enable_wildcard_permission', false)) {
142
            return $this->hasWildcardPermission($permission, $this->getDefaultGuardName());
143
        }
144
145
        $permissionClass = $this->getPermissionClass();
146
147
        if (is_string($permission)) {
148
            $permission = $permissionClass->findByName($permission, $this->getDefaultGuardName());
149
        }
150
151
        if (is_int($permission)) {
152
            $permission = $permissionClass->findById($permission, $this->getDefaultGuardName());
153
        }
154
155
        if (! $this->getGuardNames()->contains($permission->guard_name)) {
156
            throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardNames());
157
        }
158
159
        return $this->permissions->contains('id', $permission->id);
160
    }
161
}
162