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