| 1 |
efrain |
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 |
|
|
|
15 |
class AuthAdapter implements AuthAdapterInterface
|
|
|
16 |
{
|
|
|
17 |
/**
|
|
|
18 |
*
|
|
|
19 |
* @var AdapterInterface
|
|
|
20 |
*/
|
|
|
21 |
private $adapter;
|
|
|
22 |
|
|
|
23 |
/**
|
|
|
24 |
*
|
|
|
25 |
* @var string
|
|
|
26 |
*/
|
|
|
27 |
private $email;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
*
|
|
|
31 |
* @var string
|
|
|
32 |
*/
|
|
|
33 |
private $password;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
*
|
|
|
37 |
* @var int
|
|
|
38 |
*/
|
|
|
39 |
private $network_id;
|
|
|
40 |
|
| 255 |
efrain |
41 |
|
| 1 |
efrain |
42 |
/**
|
|
|
43 |
*
|
|
|
44 |
* @param AdapterInterface $adapter
|
|
|
45 |
*/
|
|
|
46 |
public function __construct(AdapterInterface $adapter)
|
|
|
47 |
{
|
|
|
48 |
$this->adapter = $adapter;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
*
|
|
|
53 |
* @param string $email
|
|
|
54 |
* @param string $password
|
|
|
55 |
* @param int $network_id
|
|
|
56 |
*/
|
|
|
57 |
public function setData($email, $password, $network_id)
|
|
|
58 |
{
|
| 255 |
efrain |
59 |
$this->email = $email;
|
|
|
60 |
$this->password = $password;
|
|
|
61 |
$this->network_id = $network_id;
|
|
|
62 |
|
| 1 |
efrain |
63 |
}
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
*
|
|
|
67 |
* {@inheritDoc}
|
|
|
68 |
* @see \Laminas\Authentication\Adapter\AdapterInterface::authenticate()
|
|
|
69 |
*/
|
|
|
70 |
public function authenticate()
|
|
|
71 |
{
|
|
|
72 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
73 |
$user = $userMapper->fetchOneByEmailAndNetworkId($this->email, $this->network_id);
|
|
|
74 |
|
|
|
75 |
if(!$user) {
|
|
|
76 |
return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, ['ERROR_USER_NOT_FOUND']);
|
|
|
77 |
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
if(User::EMAIL_VERIFIED_NO == $user->email_verified) {
|
|
|
82 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_EMAIL_HASNT_BEEN_VARIFIED']);
|
|
|
83 |
}
|
|
|
84 |
if(User::BLOCKED_YES == $user->blocked) {
|
|
|
85 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_BLOCKED']);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
if(User::STATUS_INACTIVE == $user->status) {
|
|
|
89 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_INACTIVE']);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
if(User::REQUEST_ACCESS_PENDING == $user->request_access) {
|
|
|
93 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_REQUEST_ACCESS_IS_PENDING']);
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
|
|
|
97 |
if(User::REQUEST_ACCESS_REJECTED == $user->request_access) {
|
|
|
98 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_REQUEST_ACCESS_IS_REJECTED']);
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
if(!password_verify($this->password, $user->password) && !(md5($this->password) == $user->password))
|
|
|
103 |
{
|
|
|
104 |
$max_login_attempt = 3;
|
|
|
105 |
$user->login_attempt++;
|
|
|
106 |
if($user->login_attempt >= $max_login_attempt) {
|
|
|
107 |
$user->blocked = User::BLOCKED_YES;
|
|
|
108 |
}
|
| 255 |
efrain |
109 |
$user->password_xmpp = '';
|
| 1 |
efrain |
110 |
$userMapper->update($user);
|
|
|
111 |
if(User::BLOCKED_YES == $user->blocked) {
|
|
|
112 |
return new Result(Result::FAILURE_CREDENTIAL_INVALID, null, ['ERROR_ENTERED_PASS_INCORRECT_USER_IS_BLOCKED']);
|
|
|
113 |
} else {
|
|
|
114 |
$available_attempts = $max_login_attempt - $user->login_attempt;
|
|
|
115 |
return new Result(Result::FAILURE_CREDENTIAL_INVALID, null, ['ERROR_ENTERED_PASS_INCORRECT_' . $available_attempts]);
|
|
|
116 |
}
|
|
|
117 |
} else {
|
|
|
118 |
$user->login_attempt = 0;
|
|
|
119 |
$userMapper->update($user);
|
|
|
120 |
}
|
|
|
121 |
|
|
|
122 |
|
|
|
123 |
$data = [
|
|
|
124 |
'user_id' => $user->id,
|
|
|
125 |
'device_id' => '',
|
|
|
126 |
];
|
|
|
127 |
|
|
|
128 |
return new Result(Result::SUCCESS, $data, []);
|
|
|
129 |
}
|
|
|
130 |
}
|