Proyectos de Subversion LeadersLinked - Backend

Rev

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