1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelImpersonator\Policies; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable; |
8
|
|
|
use Arcanedev\LaravelPolicies\Policy; |
9
|
|
|
use Illuminate\Foundation\Auth\User; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ImpersonationPolicy |
13
|
|
|
* |
14
|
|
|
* @package Arcanedev\LaravelImpersonator\Policies |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ImpersonationPolicy extends Policy |
18
|
|
|
{ |
19
|
|
|
/* ----------------------------------------------------------------- |
20
|
|
|
| Main Methods |
21
|
|
|
| ----------------------------------------------------------------- |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the ability's prefix. |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
168 |
|
protected static function prefix(): string |
30
|
|
|
{ |
31
|
168 |
|
return 'auth.impersonator.'; |
32
|
|
|
} |
33
|
|
|
|
34
|
168 |
|
public function abilities(): iterable |
35
|
|
|
{ |
36
|
|
|
return [ |
37
|
|
|
|
38
|
|
|
// auth.impersonator.can-impersonate |
39
|
168 |
|
static::makeAbility('can-impersonate', 'canImpersonate')->setMetas([ |
40
|
168 |
|
'name' => 'Ability to impersonate', |
41
|
|
|
'description' => 'Ability to impersonate other users', |
42
|
|
|
]), |
43
|
|
|
|
44
|
|
|
// auth.impersonator.can-be-impersonated |
45
|
168 |
|
static::makeAbility('can-be-impersonated', 'canBeImpersonated')->setMetas([ |
46
|
168 |
|
'name' => 'Ability to be impersonated', |
47
|
|
|
'description' => 'Ability to be impersonated by other users', |
48
|
|
|
]), |
49
|
|
|
|
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/* ----------------------------------------------------------------- |
54
|
|
|
| Policies |
55
|
|
|
| ----------------------------------------------------------------- |
56
|
|
|
*/ |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Check if the current user can impersonate. |
60
|
|
|
* |
61
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|\Illuminate\Foundation\Auth\User $user |
62
|
|
|
* |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
36 |
|
public function canImpersonate(User $user) |
66
|
|
|
{ |
67
|
36 |
|
return $this->isEnabled() |
68
|
36 |
|
&& $user->canImpersonate(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check if the given user can be impersonated. |
73
|
|
|
* |
74
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|\Illuminate\Foundation\Auth\User $user |
75
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
36 |
|
public function canBeImpersonated(User $user, Impersonatable $impersonated) |
80
|
|
|
{ |
81
|
36 |
|
return $this->canImpersonate($user) |
82
|
36 |
|
&& $impersonated->canBeImpersonated(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/* ----------------------------------------------------------------- |
86
|
|
|
| Other Methods |
87
|
|
|
| ----------------------------------------------------------------- |
88
|
|
|
*/ |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Check if the impersonation is enabled. |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
36 |
|
private function isEnabled() |
96
|
|
|
{ |
97
|
36 |
|
return config('impersonator.enabled', false); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|