|
1
|
|
|
<?php namespace Arcanedev\LaravelImpersonator\Policies; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable; |
|
4
|
|
|
use Arcanedev\Support\Bases\Policy; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class ImpersonationPolicy |
|
8
|
|
|
* |
|
9
|
|
|
* @package Arcanedev\LaravelImpersonator\Policies |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class ImpersonationPolicy extends Policy |
|
13
|
|
|
{ |
|
14
|
|
|
/* ----------------------------------------------------------------- |
|
15
|
|
|
| Constants |
|
16
|
|
|
| ----------------------------------------------------------------- |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
const CAN_IMPERSONATE = 'auth.impersonator.can-impersonate'; |
|
20
|
|
|
const CAN_BE_IMPERSONATED = 'auth.impersonator.can-be-impersonated'; |
|
21
|
|
|
|
|
22
|
|
|
/* ----------------------------------------------------------------- |
|
23
|
|
|
| Main Methods |
|
24
|
|
|
| ----------------------------------------------------------------- |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Check if the current user has the `can impersonate` ability. |
|
29
|
|
|
* |
|
30
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonator |
|
31
|
|
|
* |
|
32
|
|
|
* @return bool |
|
33
|
|
|
*/ |
|
34
|
15 |
|
public function canImpersonatePolicy(Impersonatable $impersonator) |
|
35
|
|
|
{ |
|
36
|
15 |
|
return $this->isEnabled() && $impersonator->canImpersonate(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check if the given user can be impersonated. |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonator |
|
43
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $impersonated |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
15 |
|
public function canBeImpersonatedPolicy(Impersonatable $impersonator, Impersonatable $impersonated) |
|
48
|
|
|
{ |
|
49
|
15 |
|
return $this->canImpersonatePolicy($impersonator) && $impersonated->canBeImpersonated(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/* ----------------------------------------------------------------- |
|
53
|
|
|
| Other Methods |
|
54
|
|
|
| ----------------------------------------------------------------- |
|
55
|
|
|
*/ |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Check if the impersonation is enabled. |
|
59
|
|
|
* |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
15 |
|
private function isEnabled() |
|
63
|
|
|
{ |
|
64
|
15 |
|
return config('impersonator.enabled', false); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|