Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3639 | Ir a la última revisión | | 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
     *
33
     * @return \LeadersLinked\Model\User
34
     */
35
    protected $user;
36
 
37
    /**
38
     *
39
     * @var string
40
     */
41
    protected $deviceId;
42
 
43
    /**
44
     *
45
     * @var Device
46
     */
47
    protected $device;
48
 
49
    /**
50
     *
51
     * @param AdapterInterface $adapter
52
     */
53
    public function __construct($adapter)
54
    {
55
 
56
 
57
        $this->auth = new \Laminas\Authentication\AuthenticationService();
58
        $this->hasIdentity = false;
59
        if($this->auth->hasIdentity()) {
60
            $identity = $this->auth->getIdentity();
61
            $this->deviceId = isset($identity['device_id']) ? $identity['device_id'] : '';
62
 
63
            $userMapper     = \LeadersLinked\Mapper\UserMapper::getInstance($adapter);
64
            $this->user     = $userMapper->fetchOne($identity['user_id']);
65
 
66
            if($this->user) {
67
                $this->hasIdentity = true;
68
 
69
                if($this->deviceId) {
70
                    $deviceMapper = DeviceMapper::getInstance($adapter);
71
                    $this->device = $deviceMapper->fetchOne($identity['device_id']);
72
                }
73
            }
74
        }
75
    }
76
 
77
 
78
    /**
79
     *
80
     * @return \LeadersLinked\Model\User
81
     */
82
    public function getUser()
83
    {
84
 
85
        return $this->user;
86
    }
87
 
88
    /**
89
     *
90
     * @return string
91
     */
92
    public function getDeviceId()
93
    {
94
        return $this->deviceId;
95
    }
96
 
97
    /**
98
     *
99
     * @return Device
100
     */
101
    public function getDevice()
102
    {
103
        return $this->device;
104
    }
105
 
106
    /**
107
     *
108
     * @return int
109
     */
110
    public function getUserId()
111
    {
112
        if($this->hasIdentity) {
113
            return $this->user->id;
114
        } else {
115
            return 0;
116
        }
117
    }
118
 
119
 
120
    /**
121
     * @return string
122
     */
123
    public function getUserTypeId()
124
    {
125
        if($this->hasIdentity) {
126
           return $this->user->usertype_id;
127
        } else {
128
            return UserType::GUEST;
129
        }
130
    }
131
 
132
 
133
 
134
    /**
135
     *
136
     * @return boolean
137
     */
138
    public function hasIdentity()
139
    {
140
        return $this->user ? true : false;
141
    }
142
 
143
    public function clearIdentity()
144
    {
145
        $this->auth->clearIdentity();
146
    }
147
 
148
 
149
}