|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\Permission; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Spatie\Permission\Exceptions\WildcardPermissionNotProperlyFormatted; |
|
7
|
|
|
|
|
8
|
|
|
class WildcardPermission |
|
9
|
|
|
{ |
|
10
|
|
|
/** @var string */ |
|
11
|
|
|
const WILDCARD_TOKEN = '*'; |
|
12
|
|
|
|
|
13
|
|
|
/** @var string */ |
|
14
|
|
|
const PART_DELIMITER = '.'; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
const SUBPART_DELIMITER = ','; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $permission; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Collection */ |
|
23
|
|
|
protected $parts; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $permission |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(string $permission) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->permission = $permission; |
|
31
|
|
|
$this->parts = collect(); |
|
32
|
|
|
|
|
33
|
|
|
$this->setParts(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string|WildcardPermission $permission |
|
38
|
|
|
* |
|
39
|
|
|
* @return bool |
|
40
|
|
|
*/ |
|
41
|
|
|
public function implies($permission): bool |
|
42
|
|
|
{ |
|
43
|
|
|
if (is_string($permission)) { |
|
44
|
|
|
$permission = new self($permission); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$otherParts = $permission->getParts(); |
|
48
|
|
|
|
|
49
|
|
|
$i = 0; |
|
50
|
|
|
foreach ($otherParts as $otherPart) { |
|
51
|
|
|
if ($this->getParts()->count() - 1 < $i) { |
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (! $this->parts->get($i)->contains(self::WILDCARD_TOKEN) |
|
56
|
|
|
&& ! $this->containsAll($this->parts->get($i), $otherPart)) { |
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$i++; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
for ($i; $i < $this->parts->count(); $i++) { |
|
64
|
|
|
if (! $this->parts->get($i)->contains(self::WILDCARD_TOKEN)) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Collection $part |
|
74
|
|
|
* @param Collection $otherPart |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function containsAll(Collection $part, Collection $otherPart): bool |
|
79
|
|
|
{ |
|
80
|
|
|
foreach ($otherPart->toArray() as $item) { |
|
81
|
|
|
if (! $part->contains($item)) { |
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return true; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return Collection |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getParts(): Collection |
|
93
|
|
|
{ |
|
94
|
|
|
return $this->parts; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Sets the different parts and subparts from permission string. |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
|
|
protected function setParts(): void |
|
103
|
|
|
{ |
|
104
|
|
|
if (empty($this->permission) || $this->permission == null) { |
|
105
|
|
|
throw WildcardPermissionNotProperlyFormatted::create($this->permission); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$parts = collect(explode(self::PART_DELIMITER, $this->permission)); |
|
109
|
|
|
|
|
110
|
|
|
$parts->each(function ($item, $key) { |
|
|
|
|
|
|
111
|
|
|
$subParts = collect(explode(self::SUBPART_DELIMITER, $item)); |
|
112
|
|
|
|
|
113
|
|
|
if ($subParts->isEmpty() || $subParts->contains('')) { |
|
114
|
|
|
throw WildcardPermissionNotProperlyFormatted::create($this->permission); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$this->parts->add($subParts); |
|
118
|
|
|
}); |
|
119
|
|
|
|
|
120
|
|
|
if ($this->parts->isEmpty()) { |
|
121
|
|
|
throw WildcardPermissionNotProperlyFormatted::create($this->permission); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.