Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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