1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Maklad\Permission\Models; |
5
|
|
|
|
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Jenssegers\Mongodb\Eloquent\Model; |
8
|
|
|
use Jenssegers\Mongodb\Relations\BelongsToMany; |
9
|
|
|
use Maklad\Permission\Contracts\PermissionInterface; |
10
|
|
|
use Maklad\Permission\Exceptions\PermissionAlreadyExists; |
11
|
|
|
use Maklad\Permission\Exceptions\PermissionDoesNotExist; |
12
|
|
|
use Maklad\Permission\Helpers; |
13
|
|
|
use Maklad\Permission\PermissionRegistrar; |
14
|
|
|
use Maklad\Permission\Traits\RefreshesPermissionCache; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Permission |
18
|
|
|
* @package Maklad\Permission\Models |
19
|
|
|
*/ |
20
|
|
|
class Permission extends Model implements PermissionInterface |
21
|
|
|
{ |
22
|
|
|
use RefreshesPermissionCache; |
23
|
|
|
|
24
|
|
|
public $guarded = ['id']; |
25
|
|
|
protected $helpers; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Permission constructor. |
29
|
|
|
* |
30
|
|
|
* @param array $attributes |
31
|
|
|
*/ |
32
|
119 |
View Code Duplication |
public function __construct(array $attributes = []) |
|
|
|
|
33
|
|
|
{ |
34
|
119 |
|
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard'); |
35
|
|
|
|
36
|
119 |
|
parent::__construct($attributes); |
37
|
|
|
|
38
|
119 |
|
$this->helpers = new Helpers(); |
39
|
|
|
|
40
|
119 |
|
$this->setTable(\config('permission.collection_names.permissions')); |
41
|
119 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create new Permission |
45
|
|
|
* |
46
|
|
|
* @param array $attributes |
47
|
|
|
* |
48
|
|
|
* @return $this|\Illuminate\Database\Eloquent\Model |
49
|
|
|
* @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists |
50
|
|
|
*/ |
51
|
119 |
View Code Duplication |
public static function create(array $attributes = []) |
|
|
|
|
52
|
|
|
{ |
53
|
119 |
|
$helpers = new Helpers(); |
54
|
119 |
|
$attributes['guard_name'] = $attributes['guard_name'] ?? \config('auth.defaults.guard'); |
55
|
|
|
|
56
|
119 |
|
if (static::getPermissions()->where('name', $attributes['name'])->where( |
57
|
119 |
|
'guard_name', |
58
|
119 |
|
$attributes['guard_name'] |
59
|
119 |
|
)->first()) { |
60
|
1 |
|
$name = (string) $attributes['name']; |
61
|
1 |
|
$guardName = (string) $attributes['guard_name']; |
62
|
1 |
|
throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guardName)); |
63
|
|
|
} |
64
|
|
|
|
65
|
119 |
|
if ($helpers->isNotLumen() && app()::VERSION < '5.4') { |
66
|
|
|
return parent::create($attributes); |
67
|
|
|
} |
68
|
|
|
|
69
|
119 |
|
return static::query()->create($attributes); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Find or create permission by its name (and optionally guardName). |
74
|
|
|
* |
75
|
|
|
* @param string $name |
76
|
|
|
* @param string|null $guardName |
77
|
|
|
* |
78
|
|
|
* @return PermissionInterface |
79
|
|
|
* @throws \Maklad\Permission\Exceptions\PermissionAlreadyExists |
80
|
|
|
*/ |
81
|
1 |
View Code Duplication |
public static function findOrCreate(string $name, $guardName = null): PermissionInterface |
|
|
|
|
82
|
|
|
{ |
83
|
1 |
|
$guardName = $guardName ?? config('auth.defaults.guard'); |
84
|
|
|
|
85
|
1 |
|
$permission = static::getPermissions() |
86
|
1 |
|
->where('name', $name) |
87
|
1 |
|
->where('guard_name', $guardName) |
88
|
1 |
|
->first(); |
89
|
|
|
|
90
|
1 |
|
if (! $permission) { |
91
|
1 |
|
$permission = static::create(['name' => $name, 'guard_name' => $guardName]); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
return $permission; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* A permission can be applied to roles. |
99
|
|
|
* @return BelongsToMany |
100
|
|
|
*/ |
101
|
119 |
|
public function roles(): BelongsToMany |
102
|
|
|
{ |
103
|
119 |
|
return $this->belongsToMany( |
104
|
119 |
|
\config('permission.models.role'), |
|
|
|
|
105
|
119 |
|
\config('permission.collection_names.role_has_permissions') |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* A permission belongs to some users of the model associated with its guard. |
111
|
|
|
* @return BelongsToMany |
112
|
|
|
*/ |
113
|
2 |
|
public function users(): BelongsToMany |
114
|
|
|
{ |
115
|
2 |
|
return $this->belongsToMany($this->helpers->getModelForGuard($this->attributes['guard_name'])); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Find a permission by its name (and optionally guardName). |
120
|
|
|
* |
121
|
|
|
* @param string $name |
122
|
|
|
* @param string|null $guardName |
123
|
|
|
* |
124
|
|
|
* @return PermissionInterface |
125
|
|
|
* @throws PermissionDoesNotExist |
126
|
|
|
*/ |
127
|
43 |
View Code Duplication |
public static function findByName(string $name, $guardName = null): PermissionInterface |
|
|
|
|
128
|
|
|
{ |
129
|
43 |
|
$guardName = $guardName ?? \config('auth.defaults.guard'); |
130
|
|
|
|
131
|
43 |
|
$permission = static::getPermissions()->where('name', $name)->where('guard_name', $guardName)->first(); |
132
|
|
|
|
133
|
43 |
|
if (! $permission) { |
134
|
7 |
|
$helpers = new Helpers(); |
135
|
7 |
|
throw new PermissionDoesNotExist($helpers->getPermissionDoesNotExistMessage($name, $guardName)); |
136
|
|
|
} |
137
|
|
|
|
138
|
37 |
|
return $permission; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Get the current cached permissions. |
143
|
|
|
* @return Collection |
144
|
|
|
*/ |
145
|
119 |
|
protected static function getPermissions(): Collection |
146
|
|
|
{ |
147
|
119 |
|
return \app(PermissionRegistrar::class)->getPermissions(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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.