Proyectos de Subversion LeadersLinked - Backend

Rev

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