|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @author enea dhack <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Enea\Authorization\Operators; |
|
13
|
|
|
|
|
14
|
|
|
use Closure; |
|
15
|
|
|
use Enea\Authorization\Contracts\{ |
|
16
|
|
|
Grantable, Owner, PermissionContract, PermissionsOwner, RolesOwner |
|
17
|
|
|
}; |
|
18
|
|
|
use Enea\Authorization\Events\Granted; |
|
19
|
|
|
use Enea\Authorization\Exceptions\AuthorizationNotGrantedException; |
|
20
|
|
|
use Exception; |
|
21
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
22
|
|
|
use Illuminate\Support\Collection; |
|
23
|
|
|
|
|
24
|
|
|
class Granter extends Operator |
|
25
|
|
|
{ |
|
26
|
|
|
use WithDeniablePermission; |
|
27
|
|
|
|
|
28
|
34 |
|
public function permissions(PermissionsOwner $owner, Collection $permissions): void |
|
29
|
|
|
{ |
|
30
|
34 |
|
$granted = $this->getPermissions($owner, $permissions); |
|
31
|
34 |
|
$granted->each($this->denialStatus($owner, false)); |
|
32
|
34 |
|
$this->grantTo($owner->permissions())($this->except($granted, $permissions)); |
|
33
|
33 |
|
$this->dispatchGrantedEvent($owner, $permissions); |
|
34
|
33 |
|
} |
|
35
|
|
|
|
|
36
|
26 |
|
public function roles(RolesOwner $owner, Collection $roles): void |
|
37
|
|
|
{ |
|
38
|
26 |
|
$this->grantTo($owner->roles())($roles); |
|
39
|
25 |
|
$this->dispatchGrantedEvent($owner, $roles); |
|
40
|
25 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function grantTo(BelongsToMany $repository): Closure |
|
43
|
|
|
{ |
|
44
|
54 |
|
return function (Collection $grantableCollection) use ($repository): void { |
|
45
|
54 |
|
$grantableCollection->each($this->addTo($repository)); |
|
46
|
54 |
|
}; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function addTo(BelongsToMany $authorizations): Closure |
|
50
|
|
|
{ |
|
51
|
54 |
|
return function (Grantable $grantable) use ($authorizations): void { |
|
52
|
54 |
|
$this->saveIn($grantable, $authorizations); |
|
53
|
54 |
|
}; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
54 |
|
private function saveIn(Grantable $grantable, BelongsToMany $authorizations): void |
|
57
|
|
|
{ |
|
58
|
|
|
try { |
|
59
|
54 |
|
$authorizations->save($grantable); |
|
60
|
2 |
|
} catch (Exception $exception) { |
|
61
|
2 |
|
throw new AuthorizationNotGrantedException($grantable, $exception); |
|
62
|
|
|
} |
|
63
|
52 |
|
} |
|
64
|
|
|
|
|
65
|
52 |
|
private function dispatchGrantedEvent(Owner $owner, Collection $grantableCollection): void |
|
66
|
|
|
{ |
|
67
|
52 |
|
$this->dispatchEvent(new Granted($owner, $grantableCollection)); |
|
68
|
52 |
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
protected function isModifiable(PermissionContract $permission): bool |
|
71
|
|
|
{ |
|
72
|
4 |
|
return $permission->pivot->isDenied(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected function throwException(Grantable $grantable): void |
|
76
|
|
|
{ |
|
77
|
|
|
throw new AuthorizationNotGrantedException($grantable); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|