1
1
<?php
2
2
/**
3
3
* Impersonate component for Cakephp3
4
- *
4
+ *
5
5
* Component to use impersonate to easily access as others user account
6
- *
7
- *
6
+ *
7
+ *
8
8
*/
9
+
9
10
namespace CakeImpersonate \Controller \Component ;
10
11
11
- use Cake \Controller \Component \AuthComponent ;
12
12
use Cake \Controller \Component ;
13
- use Cake \Controller \ComponentRegistry ;
14
- use Cake \Core \Configure ;
15
- use Cake \Event \Event ;
16
- use Cake \ORM \Table ;
13
+ use Cake \ORM \Entity ;
14
+
17
15
/**
18
16
* Impersonate component
19
17
*/
@@ -26,68 +24,58 @@ class ImpersonateComponent extends Component
26
24
* @var array
27
25
*/
28
26
protected $ _defaultConfig = [];
29
-
30
-
31
- /**
32
- * Initialize and use all Controller library
33
- *
34
- * return none
35
- */
36
- public function initialize (array $ config )
37
- {
38
- $ this ->controller = $ this ->_registry ->getController ();
39
-
40
- }
41
-
27
+
42
28
/**
43
29
* Function impersonate
44
30
*
45
- * receive user Id
46
- * return true
31
+ * @param mixed $id
32
+ * @ return bool
47
33
*/
48
- public function login ($ id ){
49
-
50
- $ this ->controller ->loadModel ('Users ' );
51
-
52
- $ originalAuth = $ this ->request ->getSession ()->read ('Auth ' );
53
-
54
- $ users = $ this ->controller ->Users ->get ($ id );
55
- $ this ->controller ->Auth ->setUser ($ users ->toArray ());
56
- $ this ->request ->getSession ()->write ('OriginalAuth ' ,$ originalAuth );
57
-
34
+ public function login ($ id )
35
+ {
36
+ $ this ->getController ()->loadModel ('Users ' );
37
+
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 );
44
+
58
45
return true ;
59
46
}
60
-
47
+
61
48
/**
62
49
* Function isImpersonate
63
50
*
64
- * To check wether current account is under impersonate
65
- * return boolean
51
+ * To check if current account is being impersonated
52
+ * @ return bool
66
53
*/
67
- public function isImpersonate () {
68
-
69
- if ($ this ->request ->getSession ()->read ('OriginalAuth ' )){
54
+ public function isImpersonate ()
55
+ {
56
+ if ($ this ->getController ()->getRequest ()->getSession ()->read ('OriginalAuth ' )) {
57
+
70
58
return true ;
71
59
}
72
-
60
+
73
61
return false ;
74
62
}
75
-
63
+
76
64
/**
77
65
* Function logout
78
66
*
79
- * To logout impersonate account
80
- * return true
67
+ * To log out of impersonated account
68
+ *
69
+ * @return bool
81
70
*/
82
- public function logout () {
83
-
84
- if ($ this ->isImpersonate ()) {
85
- $ Auth = $ this ->request ->session ()->read ('OriginalAuth ' );
86
- $ this ->request ->getSession ()->write ('Auth ' ,$ Auth );
87
-
88
- $ this ->request ->getSession ()->delete ('OriginalAuth ' );
71
+ public function logout ()
72
+ {
73
+ if ($ this ->isImpersonate ()) {
74
+ $ Auth = $ this ->getController ()->getRequest ()->getSession ()->read ('OriginalAuth ' );
75
+ $ this ->getController ()->getRequest ()->getSession ()->write ('Auth ' , $ Auth );
76
+ $ this ->getController ()->getRequest ()->getSession ()->delete ('OriginalAuth ' );
89
77
}
90
-
78
+
91
79
return true ;
92
80
}
93
81
}
0 commit comments