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\Grantable; |
16
|
|
|
use Enea\Authorization\Contracts\PermissionContract; |
17
|
|
|
use Enea\Authorization\Contracts\PermissionsOwner; |
18
|
|
|
use Enea\Authorization\Events\Denied; |
19
|
|
|
use Enea\Authorization\Exceptions\AuthorizationNotDeniedException; |
20
|
|
|
use Exception; |
21
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
22
|
|
|
use Illuminate\Support\Collection; |
23
|
|
|
|
24
|
|
|
class Denier |
25
|
|
|
{ |
26
|
|
|
use WithDeniablePermission; |
27
|
|
|
|
28
|
|
|
protected const DENIED = true; |
29
|
|
|
|
30
|
|
|
private $event; |
31
|
|
|
|
32
|
9 |
|
public function __construct(Dispatcher $event) |
33
|
|
|
{ |
34
|
9 |
|
$this->event = $event; |
35
|
9 |
|
} |
36
|
|
|
|
37
|
9 |
|
public function permissions(PermissionsOwner $owner, Collection $permissions): void |
38
|
|
|
{ |
39
|
9 |
|
$granted = $this->getPermissions($owner, $permissions); |
40
|
9 |
|
$granted->each($this->denialStatus($owner, self::DENIED)); |
41
|
9 |
|
$this->except($granted, $permissions)->each($this->deny($owner)); |
42
|
8 |
|
$this->event->dispatch(new Denied($owner, $permissions)); |
43
|
8 |
|
} |
44
|
|
|
|
45
|
9 |
|
private function deny(PermissionsOwner $owner): Closure |
46
|
|
|
{ |
47
|
9 |
|
return function (PermissionContract $permission) use ($owner): void { |
48
|
|
|
try { |
49
|
6 |
|
$owner->permissions()->save($permission, ['denied' => self::DENIED]); |
50
|
1 |
|
} catch (Exception $exception) { |
51
|
1 |
|
throw new AuthorizationNotDeniedException($permission, $exception); |
52
|
|
|
} |
53
|
9 |
|
}; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
protected function throwException(Grantable $grantable): void |
57
|
|
|
{ |
58
|
|
|
throw new AuthorizationNotDeniedException($grantable); |
59
|
|
|
} |
60
|
|
|
|
61
|
3 |
|
protected function isModifiable(PermissionContract $permission): bool |
62
|
|
|
{ |
63
|
3 |
|
return ! $permission->pivot->isDenied(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|