|
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, PermissionsOwner, RolesOwner |
|
17
|
|
|
}; |
|
18
|
|
|
use Enea\Authorization\Events\Revoked; |
|
19
|
|
|
use Enea\Authorization\Exceptions\AuthorizationNotRevokedException; |
|
20
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
21
|
|
|
use Illuminate\Support\Collection; |
|
22
|
|
|
|
|
23
|
|
|
class Revoker extends Operator |
|
24
|
|
|
{ |
|
25
|
10 |
|
public function permissions(PermissionsOwner $owner, Collection $permissions): void |
|
26
|
|
|
{ |
|
27
|
10 |
|
$this->revokeTo($owner->permissions())($permissions); |
|
28
|
9 |
|
$this->dispatchRevokedEvent($owner, $permissions); |
|
29
|
9 |
|
} |
|
30
|
|
|
|
|
31
|
8 |
|
public function roles(RolesOwner $owner, Collection $roles): void |
|
32
|
|
|
{ |
|
33
|
8 |
|
$this->revokeTo($owner->roles())($roles); |
|
34
|
7 |
|
$this->dispatchRevokedEvent($owner, $roles); |
|
35
|
7 |
|
} |
|
36
|
|
|
|
|
37
|
18 |
|
private function revokeTo(BelongsToMany $repository): Closure |
|
38
|
|
|
{ |
|
39
|
18 |
|
return function (Collection $grantableCollection) use ($repository): void { |
|
40
|
18 |
|
$grantableCollection->each($this->removeFrom($repository)); |
|
41
|
18 |
|
}; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
18 |
|
private function removeFrom(BelongsToMany $authorizations): Closure |
|
45
|
|
|
{ |
|
46
|
18 |
|
return function (Grantable $grantable) use ($authorizations): void { |
|
47
|
18 |
|
$result = $authorizations->detach($grantable->getIdentificationKey()); |
|
48
|
|
|
|
|
49
|
18 |
|
if (! $this->isSuccessful($result)) { |
|
50
|
2 |
|
throw new AuthorizationNotRevokedException($grantable); |
|
51
|
|
|
} |
|
52
|
18 |
|
}; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
18 |
|
private function isSuccessful(int $results): bool |
|
56
|
|
|
{ |
|
57
|
18 |
|
return $results > 0; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
16 |
|
private function dispatchRevokedEvent(Owner $owner, Collection $grantableCollection): void |
|
61
|
|
|
{ |
|
62
|
16 |
|
$this->dispatchEvent(new Revoked($owner, $grantableCollection)); |
|
63
|
16 |
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|