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\GuardMismatch; |
8
|
|
|
use Spatie\Permission\Exceptions\RoleDoesNotExist; |
9
|
|
|
use Spatie\Permission\Contracts\Role as RoleContract; |
10
|
|
|
use Spatie\Permission\Traits\RefreshesPermissionCache; |
11
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
12
|
|
|
|
13
|
|
|
class Role extends Model implements RoleContract |
14
|
|
|
{ |
15
|
|
|
use HasPermissions; |
16
|
|
|
use RefreshesPermissionCache; |
17
|
|
|
|
18
|
|
|
public $guarded = ['id']; |
19
|
|
|
|
20
|
|
View Code Duplication |
public function __construct(array $attributes = []) |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
if (empty($attributes['guard_name'])) { |
23
|
|
|
$attributes['guard_name'] = config('auth.defaults.guard'); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
parent::__construct($attributes); |
27
|
|
|
|
28
|
|
|
$this->setTable(config('laravel-permission.table_names.roles')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* A role may be given various permissions. |
33
|
|
|
*/ |
34
|
|
|
public function permissions(): BelongsToMany |
35
|
|
|
{ |
36
|
|
|
return $this->belongsToMany( |
37
|
|
|
config('laravel-permission.models.permission'), |
38
|
|
|
config('laravel-permission.table_names.role_has_permissions') |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Find a role by its name and guard name. |
44
|
|
|
* |
45
|
|
|
* @param string $name |
46
|
|
|
* @param string|null $guardName |
47
|
|
|
* |
48
|
|
|
* @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role |
49
|
|
|
* |
50
|
|
|
* @throws \Spatie\Permission\Exceptions\RoleDoesNotExist |
51
|
|
|
*/ |
52
|
|
|
public static function findByName(string $name, $guardName = null): RoleContract |
53
|
|
|
{ |
54
|
|
|
$guardName = $guardName ?? config('auth.defaults.guard'); |
55
|
|
|
|
56
|
|
|
$role = static::where('name', $name)->where('guard_name', $guardName)->first(); |
57
|
|
|
|
58
|
|
|
if (! $role) { |
59
|
|
|
throw new RoleDoesNotExist(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $role; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Determine if the user may perform the given permission. |
67
|
|
|
* |
68
|
|
|
* @param string|Permission $permission |
69
|
|
|
* |
70
|
|
|
* @return bool |
71
|
|
|
* |
72
|
|
|
* @throws \Spatie\Permission\Exceptions\GuardMismatch |
73
|
|
|
*/ |
74
|
|
|
public function hasPermissionTo($permission): bool |
75
|
|
|
{ |
76
|
|
|
if (is_string($permission)) { |
77
|
|
|
$permission = app(Permission::class)->findByName($permission, $this->getGuardName()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($permission->guard_name !== $this->getGuardName()) { |
81
|
|
|
throw new GuardMismatch("Expected `{$this->getGuardName()}` to match `{$permission->guard_name}`"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->permissions->contains('id', $permission->id); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.