Proyectos de Subversion LeadersLinked - Services

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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