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