Proyectos de Subversion LeadersLinked - Backend

Rev

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\Authentication;
6
 
7
use Laminas\Authentication\Adapter\AdapterInterface as AuthAdapterInterface;
8
use Laminas\Authentication\Result;
9
use Laminas\Db\Adapter\AdapterInterface;
10
use Laminas\Log\LoggerInterface;
11
use LeadersLinked\Model\User;
12
use LeadersLinked\Mapper\UserMapper;
13
use LeadersLinked\Library\Functions;
14
use LeadersLinked\Mapper\DeviceMapper;
15
use LeadersLinked\Mapper\DeviceHistoryMapper;
16
use LeadersLinked\Model\DeviceHistory;
17
use LeadersLinked\Model\UserType;
18
use LeadersLinked\Mapper\CompanyMapper;
19
use LeadersLinked\Model\Company;
20
use LeadersLinked\Mapper\CompanyUserMapper;
21
use LeadersLinked\Model\CompanyUser;
22
 
23
 
24
class AuthOneTimePasswordAdapter implements AuthAdapterInterface
25
{
26
    /**
27
     *
28
     * @var AdapterInterface
29
     */
30
    private $adapter;
31
 
32
    /**
33
     *
34
     * @var array
35
     */
36
    private $config;
37
 
38
    /**
39
     *
40
     * @var string
41
     */
42
    private $user_uuid;
43
 
44
    /**
45
     *
46
     * @var string
47
     */
48
    private $password;
49
 
50
    /**
51
     *
52
     * @var string
53
     */
54
    private $timestamp;
55
 
56
 
57
    /**
58
     *
59
     * @var int
60
     */
61
    private $rand;
62
 
63
 
64
    /**
65
     *
66
     * @var string
67
     */
68
    private $company_uuid;
69
 
70
 
71
    /**
72
     *
73
     * @var int
74
     */
75
    private $usertype_id;
76
 
77
    /**
78
     *
79
     * @param AdapterInterface $adapter
80
     * @param array $config
81
     */
82
    public function __construct(AdapterInterface $adapter, $config)
83
    {
84
        $this->adapter = $adapter;
85
        $this->config = $config;
86
    }
87
 
88
    /**
89
     *
90
     * @param string $user_uuid
91
     * @param string $token
92
     * @param string $timestamp
93
     * @param int $rand
94
     */
95
    public function setDataAdmin($user_uuid, $password, $timestamp, $rand)
96
    {
97
        $this->user_uuid    = $user_uuid;
98
        $this->password     = $password;
99
        $this->timestamp    = $timestamp;
100
        $this->rand         = $rand;
101
        $this->usertype_id  = UserType::ADMIN;
102
    }
103
 
104
    /**
105
     *
106
     * @param string $user_uuid
107
     * @param string $token
108
     * @param string $timestamp
109
     * @param int $rand
110
     * @param string company_uuid
111
     */
112
    public function setDataCompany($user_uuid, $password, $timestamp, $rand, $company_uuid)
113
    {
114
        $this->user_uuid    = $user_uuid;
115
        $this->password     = $password;
116
        $this->timestamp    = $timestamp;
117
        $this->rand         = $rand;
118
        $this->usertype_id  = UserType::COMPANY;
119
        $this->company_uuid = $company_uuid;
120
    }
121
 
122
 
123
 
124
    /**
125
     *
126
     * {@inheritDoc}
127
     * @see \Laminas\Authentication\Adapter\AdapterInterface::authenticate()
128
     */
129
    public function authenticate()
130
    {
131
        $userMapper = UserMapper::getInstance($this->adapter);
132
        $user = $userMapper->fetchOneByUuid($this->user_uuid);
133
 
134
        if(!$user) {
135
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_NOT_FOUND']);
136
        }
137
 
138
        if(User::BLOCKED_YES == $user->blocked) {
139
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_BLOCKED']);
140
        }
141
 
142
        if(User::STATUS_INACTIVE == $user->status) {
143
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_INACTIVE']);
144
        }
145
 
146
        $company = null;
147
        if($this->usertype_id == UserType::COMPANY) {
148
 
149
 
150
            $companyMapper = CompanyMapper::getInstance($this->adapter);
151
            $company = $companyMapper->fetchOneByUuid($this->company_uuid);
152
 
153
            if(!$company) {
154
                return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_COMPANY_NOT_FOUND']);
155
            }
156
 
157
            if($company->status != Company::STATUS_PENDING && $company->status != Company::STATUS_ACTIVE) {
158
                return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_COMPANY_IS_INACTIVE']);
159
            }
160
 
161
 
162
            $companyUserMapper = CompanyUserMapper::getInstance($this->adapter);
163
            $companyUser = $companyUserMapper->fetchOneByCompanyIdAndUserId($company->id, $user->id);
164
 
165
 
166
            if(!$companyUser || !in_array($companyUser->status, [CompanyUser::STATUS_ACCEPTED, CompanyUser::STATUS_ADMIN_WILL_ADD]) || !$companyUser->backend) {
167
                return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_UNAUTHORIZED']);
168
            }
169
 
170
        }
171
 
172
 
173
        $dt = \DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s',gmdate('Y-m-d\TH:i:s'));
174
        $t1 = $dt->sub(new \DateInterval('PT5M'));
175
        $t1 = $t1->getTimestamp();
176
 
177
        $t2 = $dt->add(new \DateInterval('PT5M'));
178
        $t2 = $t2->getTimestamp();
179
 
180
 
181
        if($this->timestamp < $t1 || $this->timestamp > $t2) {
182
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_TIMESTAMP']);
183
        }
184
 
185
        $sandbox = $this->config['leaderslinked.runmode.sandbox'];
186
        if($sandbox) {
187
            $salt = $this->config['leaderslinked.backend.sandbox_salt'];
188
        } else {
189
            $salt = $this->config['leaderslinked.backend.production_salt'];
190
        }
191
 
192
 
193
        $passworVerification = md5($user->one_time_password . '-' . $this->rand . '-' . $this->timestamp . '-' . $salt);
194
        if($this->password != $passworVerification)
195
        {
196
            return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_WEBSERVICE_PASSWORD']);
197
        } else {
198
            $userMapper->update($user);
199
        }
200
 
201
        $data = [
202
            'user_id' => $user->id,
203
            'company_id' => $company ? $company->id : 0,
204
        ];
205
 
206
        return new Result(Result::SUCCESS, $data, []);
207
    }
208
}