1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arcanedev\LaravelImpersonator\Traits; |
6
|
|
|
|
7
|
|
|
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable; |
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Trait CanImpersonate |
12
|
|
|
* |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
trait CanImpersonate |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Main Methods |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Impersonate the given user. |
24
|
|
|
* |
25
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated |
26
|
|
|
* |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
12 |
|
public function impersonate(Impersonatable $impersonated) |
30
|
|
|
{ |
31
|
|
|
/** @var \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $this */ |
32
|
12 |
|
return impersonator()->start($this, $impersonated); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Stop the impersonation. |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
12 |
|
public function stopImpersonation() |
41
|
|
|
{ |
42
|
12 |
|
return $this->isImpersonated() ? impersonator()->stop() : false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/* ----------------------------------------------------------------- |
46
|
|
|
| Check Methods |
47
|
|
|
| ----------------------------------------------------------------- |
48
|
|
|
*/ |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check if the current modal can impersonate other models. |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
abstract public function canImpersonate(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Check if the current model can be impersonated. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
abstract public function canBeImpersonated(); |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Check if impersonation is ongoing. |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
12 |
|
public function isImpersonated() |
70
|
|
|
{ |
71
|
12 |
|
return impersonator()->isImpersonating(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Check if the two persons are the same. |
76
|
|
|
* |
77
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
44 |
|
public function isSamePerson($impersonated) |
82
|
|
|
{ |
83
|
44 |
|
if ($this instanceof Model && $impersonated instanceof Model) { |
84
|
44 |
|
return $this->is($impersonated); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->getAuthIdentifier() == $impersonated->getAuthIdentifier(); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.