1
|
|
|
<?php namespace Arcanedev\LaravelImpersonator\Traits; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelImpersonator\Contracts\Impersonatable; |
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Trait HasImpersonation |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelImpersonator\Traits |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
trait CanImpersonate |
13
|
|
|
{ |
14
|
|
|
/* ----------------------------------------------------------------- |
15
|
|
|
| Main Methods |
16
|
|
|
| ----------------------------------------------------------------- |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Impersonate the given user. |
21
|
|
|
* |
22
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated |
23
|
|
|
* |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
6 |
|
public function impersonate(Impersonatable $impersonated) |
27
|
|
|
{ |
28
|
|
|
/** @var \Arcanedev\LaravelImpersonator\Contracts\Impersonatable $this */ |
29
|
6 |
|
return impersonator()->start($this, $impersonated); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Stop the impersonation. |
34
|
|
|
* |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
6 |
|
public function stopImpersonation() |
38
|
|
|
{ |
39
|
6 |
|
return $this->isImpersonated() ? impersonator()->stop() : false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/* ----------------------------------------------------------------- |
43
|
|
|
| Check Methods |
44
|
|
|
| ----------------------------------------------------------------- |
45
|
|
|
*/ |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Check if the current modal can impersonate other models. |
49
|
|
|
* |
50
|
|
|
* @return bool |
51
|
|
|
*/ |
52
|
|
|
abstract public function canImpersonate(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Check if the current model can be impersonated. |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
|
|
abstract public function canBeImpersonated(); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if impersonation is ongoing. |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
6 |
|
public function isImpersonated() |
67
|
|
|
{ |
68
|
6 |
|
return impersonator()->isImpersonating(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check if the two persons are the same. |
73
|
|
|
* |
74
|
|
|
* @param \Arcanedev\LaravelImpersonator\Contracts\Impersonatable|mixed $impersonated |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
22 |
|
public function isSamePerson($impersonated) |
79
|
|
|
{ |
80
|
22 |
|
if ($this instanceof Model && $impersonated instanceof Model) { |
81
|
22 |
|
return $this->is($impersonated); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $this->getAuthIdentifier() == $impersonated->getAuthIdentifier(); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
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.