|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Canvas\Auth; |
|
6
|
|
|
|
|
7
|
|
|
use Canvas\Models\Users; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
use Phalcon\Di; |
|
10
|
|
|
use RuntimeException; |
|
11
|
|
|
use Canvas\Hashing\Password; |
|
12
|
|
|
|
|
13
|
|
|
class App extends Auth |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* User login. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string $email |
|
19
|
|
|
* @param string $password |
|
20
|
|
|
* @param integer $autologin |
|
21
|
|
|
* @param integer $admin |
|
22
|
|
|
* @param string $userIp |
|
23
|
|
|
* @return Users |
|
24
|
|
|
*/ |
|
25
|
|
|
public static function login(string $email, string $password, int $autologin = 1, int $admin, string $userIp) : Users |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
//trim email |
|
28
|
|
|
$email = ltrim(trim($email)); |
|
29
|
|
|
$password = ltrim(trim($password)); |
|
30
|
|
|
|
|
31
|
|
|
//if its a email lets by it by email, if not by displayname |
|
32
|
|
|
$user = Users::getByEmail($email); |
|
33
|
|
|
|
|
34
|
|
|
//first we find the user |
|
35
|
|
|
if (!$user) { |
|
36
|
|
|
throw new Exception(_('Invalid Username or Password.')); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
self::loginAttempsValidation($user); |
|
40
|
|
|
|
|
41
|
|
|
//check if the user exist on this app |
|
42
|
|
|
$currentAppUserInfo = $user->getApp(); |
|
43
|
|
|
|
|
44
|
|
|
if (!is_object($currentAppUserInfo) || empty($currentAppUserInfo->password)) { |
|
45
|
|
|
throw new Exception(_('Invalid Username or Password.')); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
//password verification |
|
49
|
|
|
if (Password::check($password, $currentAppUserInfo->password) && $user->isActive()) { |
|
50
|
|
|
//rehash password |
|
51
|
|
|
Password::rehash($password, $currentAppUserInfo); |
|
52
|
|
|
|
|
53
|
|
|
// Reset login tries |
|
54
|
|
|
self::resetLoginTries($user); |
|
55
|
|
|
return $user; |
|
56
|
|
|
} elseif ($user->isActive()) { |
|
57
|
|
|
// Only store a failed login attempt for an active user - inactive users can't login even with a correct password |
|
58
|
|
|
self::updateLoginTries($user); |
|
59
|
|
|
|
|
60
|
|
|
throw new Exception(_('Invalid Username or Password..')); |
|
61
|
|
|
} elseif ($user->isBanned()) { |
|
62
|
|
|
throw new Exception(_('User has not been banned, please check your email for the activation link.')); |
|
63
|
|
|
} else { |
|
64
|
|
|
throw new Exception(_('User has not been activated, please check your email for the activation link.')); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Update the password for the current app of all the companies, FOR NOW. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Users $user |
|
72
|
|
|
* @param string $password |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function updatePassword(Users $user, string $password): bool |
|
76
|
|
|
{ |
|
77
|
|
|
$app = Di::getDefault()->getApp(); |
|
78
|
|
|
|
|
79
|
|
|
$userApps = $user->getApps([ |
|
80
|
|
|
'conditions' => 'apps_id = ?0', |
|
81
|
|
|
'bind' => [$app->getId()] |
|
82
|
|
|
]); |
|
83
|
|
|
|
|
84
|
|
|
if (is_object($userApps)) { |
|
85
|
|
|
$userApps->update(['password' => $password]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.