Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3639 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Plugin;
6
 
7
use Laminas\Mvc\Controller\Plugin\AbstractPlugin;
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Authentication\AuthenticationService;
10
use LeadersLinked\Model\Device;
11
use LeadersLinked\Mapper\DeviceMapper;
12
use LeadersLinked\Model\UserType;
13
 
14
 
15
class CurrentUserPlugin extends AbstractPlugin
16
{
17
 
18
    /**
19
     *
20
     * @var AuthenticationService
21
     */
22
    protected $auth;
23
 
24
    /**
25
     *
26
     * @var boolean
27
     */
28
    protected $hasIdentity;
29
 
30
 
31
    /**
32
     *
3639 efrain 33
     * @var boolean
34
     */
35
    protected $hasImpersonate;
36
 
37
 
38
    /**
39
     *
1 www 40
     * @return \LeadersLinked\Model\User
41
     */
42
    protected $user;
43
 
3639 efrain 44
 
1 www 45
    /**
3639 efrain 46
     *
47
     * @return \LeadersLinked\Model\User
48
     */
6749 efrain 49
    protected $impersonate_user;
3639 efrain 50
 
51
    /**
1 www 52
     *
53
     * @var string
54
     */
55
    protected $deviceId;
56
 
57
    /**
58
     *
59
     * @var Device
60
     */
61
    protected $device;
62
 
63
    /**
64
     *
65
     * @param AdapterInterface $adapter
66
     */
67
    public function __construct($adapter)
68
    {
69
 
70
 
71
        $this->auth = new \Laminas\Authentication\AuthenticationService();
3639 efrain 72
        $this->impersonate_user = null;
1 www 73
        $this->hasIdentity = false;
3639 efrain 74
        $this->hasImpersonate = false;
1 www 75
        if($this->auth->hasIdentity()) {
76
            $identity = $this->auth->getIdentity();
77
            $this->deviceId = isset($identity['device_id']) ? $identity['device_id'] : '';
78
 
79
            $userMapper     = \LeadersLinked\Mapper\UserMapper::getInstance($adapter);
80
            $this->user     = $userMapper->fetchOne($identity['user_id']);
3639 efrain 81
 
1 www 82
 
83
            if($this->user) {
3639 efrain 84
 
1 www 85
                $this->hasIdentity = true;
3639 efrain 86
                if($this->user->impersonate_user_id) {
87
                    $this->hasImpersonate = true;
88
                    $this->impersonate_user = $userMapper->fetchOne($this->user->impersonate_user_id);
89
                }
1 www 90
 
3639 efrain 91
 
92
 
1 www 93
                if($this->deviceId) {
94
                    $deviceMapper = DeviceMapper::getInstance($adapter);
95
                    $this->device = $deviceMapper->fetchOne($identity['device_id']);
96
                }
97
            }
98
        }
99
    }
100
 
101
 
102
    /**
103
     *
104
     * @return \LeadersLinked\Model\User
105
     */
106
    public function getUser()
107
    {
3639 efrain 108
 
109
        if($this->hasImpersonate) {
110
            return $this->impersonate_user;
111
        } else {
112
 
113
            return $this->user;
114
        }
115
    }
116
 
117
    /**
118
     *
119
     * @return \LeadersLinked\Model\User
120
     */
121
    public function getRawUser()
122
    {
1 www 123
 
124
        return $this->user;
3639 efrain 125
 
1 www 126
    }
127
 
3639 efrain 128
 
1 www 129
    /**
3639 efrain 130
     *
131
     * @return \LeadersLinked\Model\User
132
     */
133
    public function getImpersonateUser()
134
    {
135
 
136
        return $this->impersonate_user;
137
    }
138
 
139
 
140
    /**
1 www 141
     *
142
     * @return string
143
     */
144
    public function getDeviceId()
145
    {
146
        return $this->deviceId;
147
    }
148
 
149
    /**
150
     *
151
     * @return Device
152
     */
153
    public function getDevice()
154
    {
155
        return $this->device;
156
    }
157
 
158
    /**
159
     *
160
     * @return int
161
     */
162
    public function getUserId()
163
    {
164
        if($this->hasIdentity) {
3639 efrain 165
            if($this->hasImpersonate) {
166
                return $this->impersonate_user->id;
167
            } else {
168
                return $this->user->id;
169
            }
1 www 170
        } else {
171
            return 0;
172
        }
173
    }
174
 
175
 
176
    /**
177
     * @return string
178
     */
179
    public function getUserTypeId()
180
    {
181
        if($this->hasIdentity) {
3639 efrain 182
            if($this->hasImpersonate) {
183
                return $this->impersonate_user->usertype_id;
184
            } else {
185
                return $this->user->usertype_id;
186
            }
1 www 187
        } else {
188
            return UserType::GUEST;
189
        }
190
    }
191
 
3639 efrain 192
    /**
193
     *
194
     * @return boolean
195
     */
196
    public function hasImpersonate()
197
    {
198
        return $this->hasImpersonate;
199
    }
1 www 200
 
3639 efrain 201
 
1 www 202
 
203
    /**
204
     *
205
     * @return boolean
206
     */
207
    public function hasIdentity()
208
    {
3639 efrain 209
        return $this->hasIdentity;
1 www 210
    }
211
 
212
    public function clearIdentity()
213
    {
214
        $this->auth->clearIdentity();
215
    }
216
 
217
 
218
}