10
10
namespace CakeImpersonate \Controller \Component ;
11
11
12
12
use Cake \Controller \Component ;
13
- use Cake \ORM \ Entity ;
13
+ use Cake \Event \ Event ;
14
14
15
15
/**
16
16
* Impersonate component
@@ -23,24 +23,29 @@ class ImpersonateComponent extends Component
23
23
*
24
24
* @var array
25
25
*/
26
- protected $ _defaultConfig = [];
26
+ protected $ _defaultConfig = [
27
+ 'userModel ' => 'Users ' ,
28
+ 'finder ' => 'all ' ,
29
+ ];
27
30
28
31
/**
29
32
* Function impersonate
30
33
*
31
- * @param mixed $id
34
+ * @param mixed $id ID of user to impersonate
32
35
* @return bool
36
+ * @throws \Exception If userModal is not loaded in the Controller
33
37
*/
34
38
public function login ($ id )
35
39
{
36
- $ this ->getController ()->loadModel ('Users ' );
40
+ $ userModel = $ this ->getConfig ('userModal ' , 'Users ' );
41
+ $ this ->getController ()->loadModel ($ userModel );
37
42
38
- $ originalAuth = $ this ->getController ()-> getRequest ()-> getSession ()-> read ( ' Auth ' );
39
-
40
- /** @var Entity $users */
41
- $ users = $ this -> getController ( )->Users -> get ( $ id );
42
- $ this ->getController ()->Auth ->setUser ($ users -> toArray () );
43
- $ this ->getController ()->getRequest ()->getSession ()->write ('OriginalAuth ' , $ originalAuth );
43
+ $ finder = $ this ->getConfig ( ' finder ' );
44
+ /** @var \Cake\ORM\Table $userTable */
45
+ $ userTable = $ this -> getController ()->{ $ userModel };
46
+ $ userArray = $ userTable -> find ( $ finder )->where ([ $ userTable -> getAlias () . ' .id ' => $ id])-> firstOrFail ()-> toArray ( );
47
+ $ this ->getController ()->Auth ->setUser ($ userArray );
48
+ $ this ->getController ()->getRequest ()->getSession ()->write ('OriginalAuth ' , $ this -> getController ()-> getRequest ()-> getSession ()-> read ( ' Auth ' ) );
44
49
45
50
return true ;
46
51
}
@@ -54,7 +59,6 @@ public function login($id)
54
59
public function isImpersonate ()
55
60
{
56
61
if ($ this ->getController ()->getRequest ()->getSession ()->read ('OriginalAuth ' )) {
57
-
58
62
return true ;
59
63
}
60
64
@@ -78,4 +82,35 @@ public function logout()
78
82
79
83
return true ;
80
84
}
85
+
86
+ /**
87
+ * {@inheritdoc}
88
+ */
89
+ public function implementedEvents ()
90
+ {
91
+ $ eventMap = [
92
+ 'Controller.initialize ' => 'updateConfig ' ,
93
+ 'Controller.startup ' => 'updateConfig ' ,
94
+ ];
95
+ $ events = [];
96
+ foreach ($ eventMap as $ event => $ method ) {
97
+ if (method_exists ($ this , $ method )) {
98
+ $ events [$ event ] = $ method ;
99
+ }
100
+ }
101
+
102
+ return $ events ;
103
+ }
104
+
105
+ /**
106
+ * Updates the userModel and finder based on the AuthComponent.
107
+ *
108
+ * @param Event $event Event that started the update.
109
+ * @return void
110
+ */
111
+ public function updateConfig (Event $ event )
112
+ {
113
+ $ this ->setConfig ('userModel ' , $ this ->getController ()->Auth ->getConfig ('authorize.all.userModel ' , $ this ->getConfig ('userModel ' )));
114
+ $ this ->setConfig ('finder ' , $ this ->getController ()->Auth ->getConfig ('authorize.all.finder ' , $ this ->getConfig ('finder ' )));
115
+ }
81
116
}
0 commit comments