Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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