| 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 |
|
|
|
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 |
*
|
|
|
38 |
* @param AdapterInterface $adapter
|
|
|
39 |
*/
|
|
|
40 |
public function __construct(AdapterInterface $adapter)
|
|
|
41 |
{
|
|
|
42 |
$this->adapter = $adapter;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
*
|
|
|
47 |
* @param string $email
|
|
|
48 |
* @param string $password
|
|
|
49 |
*/
|
|
|
50 |
public function setData($email, $password)
|
|
|
51 |
{
|
|
|
52 |
$this->email = $email;
|
|
|
53 |
$this->password = $password;
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
*
|
|
|
58 |
* {@inheritDoc}
|
|
|
59 |
* @see \Laminas\Authentication\Adapter\AdapterInterface::authenticate()
|
|
|
60 |
*/
|
|
|
61 |
public function authenticate()
|
|
|
62 |
{
|
|
|
63 |
$userMapper = UserMapper::getInstance($this->adapter);
|
|
|
64 |
$user = $userMapper->fetchOneByEmail($this->email);
|
|
|
65 |
|
|
|
66 |
if(!$user) {
|
|
|
67 |
return new Result(Result::FAILURE_IDENTITY_NOT_FOUND, null, ['ERROR_USER_NOT_FOUND']);
|
|
|
68 |
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
|
|
|
72 |
if(User::EMAIL_VERIFIED_NO == $user->email_verified) {
|
|
|
73 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_EMAIL_HASNT_BEEN_VARIFIED']);
|
|
|
74 |
}
|
|
|
75 |
if(User::BLOCKED_YES == $user->blocked) {
|
|
|
76 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_BLOCKED']);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
if(User::STATUS_INACTIVE == $user->status) {
|
|
|
80 |
return new Result(Result::FAILURE_UNCATEGORIZED, null, ['ERROR_USER_IS_INACTIVE']);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
if(!password_verify($this->password, $user->password) && !(md5($this->password) == $user->password))
|
|
|
84 |
{
|
|
|
85 |
$max_login_attempt = 3;
|
|
|
86 |
$user->login_attempt++;
|
|
|
87 |
if($user->login_attempt >= $max_login_attempt) {
|
|
|
88 |
$user->blocked = User::BLOCKED_YES;
|
|
|
89 |
}
|
|
|
90 |
$userMapper->update($user);
|
|
|
91 |
if(User::BLOCKED_YES == $user->blocked) {
|
|
|
92 |
return new Result(Result::FAILURE_CREDENTIAL_INVALID, null, ['ERROR_ENTERED_PASS_INCORRECT_USER_IS_BLOCKED']);
|
|
|
93 |
} else {
|
|
|
94 |
$available_attempts = $max_login_attempt - $user->login_attempt;
|
|
|
95 |
return new Result(Result::FAILURE_CREDENTIAL_INVALID, null, ['ERROR_ENTERED_PASS_INCORRECT_' . $available_attempts]);
|
|
|
96 |
}
|
|
|
97 |
} else {
|
|
|
98 |
$user->login_attempt = 0;
|
|
|
99 |
$userMapper->update($user);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
$data = [
|
|
|
104 |
'user_id' => $user->id,
|
|
|
105 |
'device_id' => '',
|
|
|
106 |
];
|
|
|
107 |
|
|
|
108 |
return new Result(Result::SUCCESS, $data, []);
|
|
|
109 |
}
|
|
|
110 |
}
|