/
Authenticate.php
144 lines (125 loc) · 5.95 KB
/
Authenticate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*********************************************************************************
** The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
********************************************************************************/
require_once 'modules/Users/Users.php';
require_once 'modules/Users/UserPrivilegesWriter.php';
require_once 'include/logging.php';
global $mod_strings, $default_charset, $adb, $cbodUniqueUserConnection;
$focus = new Users();
// Add in defensive code here.
$focus->column_fields['user_name'] = to_html(vtlib_purify($_POST['user_name']));
$user_password = isset($_POST['user_password']) ? $_POST['user_password'] : '';
$focus->load_user($user_password);
if ($cbodUniqueUserConnection && $focus->loggedIn() && (empty($_REQUEST['unblock']) || $_REQUEST['unblock'] != 'on')) {
coreBOS_Session::set('login_user_name', $focus->column_fields['user_name']);
coreBOS_Session::set('login_password', $user_password);
// go back to the login screen and create an error message for the user.
if ($focus->canUnblock()) {
coreBOS_Session::set('login_error', $mod_strings['ERR_USER_CAN_UNBLOCK']);
coreBOS_Session::set('can_unblock', 1);
} else {
coreBOS_Session::set('login_error', $mod_strings['ERR_USER_LOGGED_IN']);
coreBOS_Session::set('can_unblock', 0);
}
header('Location: index.php');
exit;
}
if ($focus->is_authenticated() && !$focus->is_twofaauthenticated()) {
coreBOS_Session::set('login_user_name', $focus->column_fields['user_name']);
include 'modules/Users/do2FAAuthentication.php';
die();
}
if ($focus->is_authenticated() && $focus->is_twofaauthenticated()) {
coreBOS_Session::regenerate();
//Inserting entries for audit trail during login
if (coreBOS_Settings::getSetting('audit_trail', false)) {
$date_var = $adb->formatDate(date('Y-m-d H:i:s'), true);
$query = 'insert into vtiger_audit_trial values(?,?,?,?,?,?)';
$params = array($adb->getUniqueID('vtiger_audit_trial'), $focus->id, 'Users','Authenticate','',$date_var);
$adb->pquery($query, $params);
}
cbEventHandler::do_action('corebos.audit.authenticate', array($focus->id, 'Users', 'Authenticate', $focus->id, date('Y-m-d H:i:s')));
// Recording the login info
require_once 'modules/Users/LoginHistory.php';
$loghistory=new LoginHistory();
$loghistory->user_login($focus->column_fields['user_name'], Vtiger_Request::get_ip(), date('Y/m/d H:i:s'));
require_once 'include/utils/UserInfoUtil.php';
UserPrivilegesWriter::setUserPrivileges($focus->id);
coreBOS_Session::delete('login_password');
coreBOS_Session::delete('login_error');
coreBOS_Session::delete('login_user_name');
coreBOS_Session::set('authenticated_user_id', $focus->id);
coreBOS_Session::set('app_unique_key', $application_unique_key);
$connection_id = uniqid();
coreBOS_Session::set('conn_unique_key', $connection_id);
coreBOS_Settings::setSetting('cbodUserConnection'.$focus->id, $connection_id);
//Enabled session variable for KCFINDER
coreBOS_Session::setKCFinderVariables();
// store the user's theme in the session
if (!empty($focus->column_fields['theme'])) {
$authenticated_user_theme = $focus->column_fields['theme'];
} else {
$authenticated_user_theme = $default_theme;
}
// store the user's language in the session
if (!empty($focus->column_fields['language'])) {
$authenticated_user_language = $focus->column_fields['language'];
} else {
$authenticated_user_language = $default_language;
}
coreBOS_Session::set('vtiger_authenticated_user_theme', $authenticated_user_theme);
coreBOS_Session::set('authenticated_user_language', $authenticated_user_language);
coreBOS_Session::save();
coreBOS_Session::saveUserID($focus->id, session_id());
coreBOS_Session::deleteUserID($focus->id, session_id());
cbEventHandler::do_action('corebos.login', array($focus));
$log->debug("authenticated_user_language and ID: $authenticated_user_language $focus->id");
// Reset number of failed login attempts
$query = 'UPDATE vtiger_users SET failed_login_attempts=0 where user_name=?';
$adb->pquery($query, array($focus->column_fields['user_name']));
// Clear all uploaded import files for this user if it exists
global $import_dir;
$tmp_file_name = $import_dir. 'IMPORT_'.$focus->id;
if (file_exists($tmp_file_name)) {
unlink($tmp_file_name);
}
if (isset($_SESSION['lastpage'])) {
header('Location: index.php?'.$_SESSION['lastpage']);
} else {
header('Location: index.php');
}
} else {
$sql = 'select failed_login_attempts from vtiger_users where user_name=?';
$result = $adb->pquery($sql, array($focus->column_fields['user_name']));
$failed_login_attempts = 0;
if ($result && $adb->num_rows($result)>0) {
$failed_login_attempts = $adb->query_result($result, 0, 0);
}
$maxFailedLoginAttempts = GlobalVariable::getVariable('Application_MaxFailedLoginAttempts', 5);
// Increment number of failed login attempts
$query = 'UPDATE vtiger_users SET failed_login_attempts=COALESCE(failed_login_attempts,0)+1 where user_name=?';
$adb->pquery($query, array($focus->column_fields['user_name']));
coreBOS_Session::set('login_user_name', $focus->column_fields['user_name']);
coreBOS_Session::set('login_password', $user_password);
if (empty($_SESSION['login_error'])) {
if ($failed_login_attempts >= $maxFailedLoginAttempts) {
$errstr = $mod_strings['ERR_MAXLOGINATTEMPTS'];
} elseif (!$focus->is_twofaauthenticated()) {
$errstr = $mod_strings['ERR_INVALID_2FACODE'];
} else {
$errstr = $mod_strings['ERR_INVALID_PASSWORD'];
}
coreBOS_Session::set('login_error', $errstr);
}
cbEventHandler::do_action('corebos.audit.login.attempt', array(0, $focus->column_fields['user_name'], 'Login Attempt', 0, date('Y-m-d H:i:s')));
// go back to the login screen.
// create an error message for the user.
header('Location: index.php');
}
?>