Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 17002 | | 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
    /**
17018 efrain 74
     *
75
     * @var \LeadersLinked\Plugin\CurrentUserPlugin
76
     */
77
    private  $adapter;
78
 
79
    /**
1 www 80
     *
17002 efrain 81
     * @param array $config
1 www 82
     * @param AdapterInterface $adapter
17002 efrain 83
     * @return \LeadersLinked\Plugin\CurrentUserPlugin
1 www 84
     */
17002 efrain 85
    public static function getInstance($config, $adapter)
1 www 86
    {
17002 efrain 87
        if(self::$_instance == null) {
88
            self::$_instance = new CurrentUserPlugin($config, $adapter);
89
        }
90
 
91
        return self::$_instance;
92
    }
93
 
94
 
95
    /**
96
     *
97
     * @param array $config
98
     * @param AdapterInterface $adapter
99
     */
100
    private function __construct($config, $adapter)
101
    {
102
        $this->config = $config;
17018 efrain 103
        $this->adapter = $adapter;
17002 efrain 104
 
1 www 105
        $this->auth = new \Laminas\Authentication\AuthenticationService();
15342 efrain 106
        $this->impersonateUser = null;
1 www 107
        $this->hasIdentity = false;
15342 efrain 108
        $this->hasImpersonate = false;
109
 
1 www 110
        if($this->auth->hasIdentity()) {
111
            $identity = $this->auth->getIdentity();
112
 
113
            $userMapper     = \LeadersLinked\Mapper\UserMapper::getInstance($adapter);
114
            $this->user     = $userMapper->fetchOne($identity['user_id']);
115
 
116
            if($this->user) {
117
                $this->hasIdentity = true;
118
 
15342 efrain 119
                if($this->user->impersonate_user_id) {
120
                    $this->hasImpersonate = true;
121
                    $this->impersonateUser = $userMapper->fetchOne($this->user->impersonate_user_id);
122
                }
123
 
124
 
1 www 125
                if($identity['company_id']) {
126
                    $companyMapper = CompanyMapper::getInstance($adapter);
127
                    $this->company = $companyMapper->fetchOne($identity['company_id']);
128
                }
129
            }
130
        }
131
    }
132
 
133
 
134
    /**
135
     *
136
     * @return \LeadersLinked\Model\User
137
     */
138
    public function getUser()
139
    {
140
 
141
        return $this->user;
142
    }
143
 
15342 efrain 144
 
145
 
1 www 146
    /**
15342 efrain 147
     *
148
     * @return \LeadersLinked\Model\User
1 www 149
     */
15342 efrain 150
    public function getImpersonateUser()
1 www 151
    {
15342 efrain 152
 
153
        return $this->impersonateUser;
1 www 154
    }
15342 efrain 155
 
1 www 156
 
157
 
15342 efrain 158
     /**
1 www 159
     *
160
     * @return int
161
     */
162
    public function getUserId()
163
    {
164
        if($this->hasIdentity) {
15342 efrain 165
            if($this->hasImpersonate) {
166
                return $this->impersonateUser->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) {
15342 efrain 182
            if($this->hasImpersonate) {
183
                return $this->impersonateUser->usertype_id;
184
            } else {
185
                return $this->user->usertype_id;
186
            }
1 www 187
        } else {
188
            return UserType::GUEST;
189
        }
190
    }
191
 
15342 efrain 192
    /**
193
     *
194
     * @return boolean
195
     */
196
    public function hasImpersonate()
197
    {
198
        return $this->hasImpersonate;
199
    }
1 www 200
 
15342 efrain 201
 
202
 
1 www 203
    /**
204
     *
15342 efrain 205
     * @return boolean
206
     */
207
    public function hasIdentity()
208
    {
209
        return $this->hasIdentity;
210
    }
211
 
212
 
213
    /**
214
     *
1 www 215
     * @return \LeadersLinked\Model\Company
216
     */
217
    public function getCompany()
218
    {
219
        return $this->company;
220
    }
221
 
222
    public function getCompanyId()
223
    {
224
        if($this->company) {
17002 efrain 225
            return $this->company->id;
1 www 226
        } else {
227
            return 0;
228
        }
229
    }
15342 efrain 230
 
1 www 231
 
232
    public function clearIdentity()
233
    {
234
        $this->auth->clearIdentity();
235
    }
17002 efrain 236
 
237
 
238
    public function getCompanyImage()
239
    {
240
        if($this->company) {
241
 
17018 efrain 242
            $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 243
            return $storage->getCompanyImage($this->company);
244
        }
245
 
246
    }
247
 
248
    public function getCompanyCover()
249
    {
250
        if($this->company) {
251
 
17018 efrain 252
            $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 253
            return $storage->getCompanyCover($this->company);
254
        }
255
 
256
    }
257
 
258
    public function getUserImage()
259
    {
260
        if($this->user) {
261
 
17018 efrain 262
            $storage = Storage::getInstance($this->config, $this->adapter);
17002 efrain 263
            return $storage->getUserImage($this->user);
264
        }
265
 
266
    }
1 www 267
 
268
 
269
}