Moving event_login() function inside the LoginSuccessHandler listener.

skala
Julio Montoya 12 years ago
parent b4d19ddd35
commit 151149345f
  1. 24
      main/inc/global.inc.php
  2. 50
      main/inc/lib/events.lib.inc.php
  3. 18
      src/ChamiloLMS/Component/Auth/LoginSuccessHandler.php
  4. 42
      src/ChamiloLMS/Controller/IndexController.php
  5. 84
      src/ChamiloLMS/Controller/UserPortalController.php

@ -4,7 +4,7 @@
/**
* This is a bootstrap file that loads all Chamilo dependencies including:
*
* - Chamilo settings in main/inc/conf/configuration.php or config/configuration.yml or config/configuration.php (in this order, using what if finds first)
* - Chamilo settings config/configuration.yml or config/configuration.php (in this order, using what if finds first)
* - Database (Using Doctrine DBAL/ORM)
* - Templates (Using Twig)
* - Loading language files (Using Symfony component)
@ -176,7 +176,10 @@ $app['debug'] = isset($_configuration['debug']) ? $_configuration['debug'] : fal
$app['show_profiler'] = isset($_configuration['show_profiler']) ? $_configuration['show_profiler'] : false;
// Enables assetic in order to load 1 compressed stylesheet or split files
$app['assetic.enabled'] = $app['debug'];
//$app['assetic.enabled'] = $app['debug'];
// Harcoded to false by default. Implementation is not finished yet.
$app['assetic.enabled'] = false;
// Dumps assets
$app['assetic.auto_dump_assets'] = false;
@ -217,13 +220,12 @@ $app['template.show_footer'] = true;
$app['template.show_learnpath'] = false;
$app['template.hide_global_chat'] = true;
$app['template.load_plugins'] = true;
$app['configuration'] = $_configuration;
$_plugins = array();
if ($alreadyInstalled) {
/** Including service providers */
require_once 'services.php';
@ -244,12 +246,7 @@ if ($alreadyInstalled) {
$_configuration['access_url'] = $details['id'];
}
}
//Session::write('url_id', $_configuration['access_url']);
//Session::write('url_info', api_get_current_access_url_info($_configuration['access_url']));
} else {
//Session::write('url_id', 1);
}
}
$charset = 'UTF-8';
@ -290,9 +287,7 @@ $app->error(
// Default layout.
$app['default_layout'] = $app['template_style'].'/layout/layout_1_col.tpl';
$app['template']->assign('error', array('code' => $code, 'message' => $message));
$response = $app['template']->render_layout('error.tpl');
return new Response($response);
@ -302,7 +297,7 @@ $app->error(
// Preserving the value of the global variable $charset.
$charset_initial_value = $charset;
// Section (tabs in the main chamilo menu)
// Section (tabs in the main Chamilo menu)
$app['this_section'] = SECTION_GLOBAL;
// Inclusion of internationalization libraries
@ -773,8 +768,6 @@ $charset = $charset_initial_value;
// For determing text direction correspondent to the current language we use now information from the internationalization library.
$text_dir = api_get_text_direction();
// Update of the logout_date field in the table track_e_login (needed for the calculation of the total connection time)
/** "Login as user" custom script */
// @todo move this code in a controller
if (!isset($_SESSION['login_as']) && isset($_user)) {
@ -803,9 +796,6 @@ if (!isset($_SESSION['login_as']) && isset($_user)) {
$now = api_get_utc_datetime();
$s_sql_update_logout_date = "UPDATE $tbl_track_login SET logout_date = '$now' WHERE login_id = $i_id_last_connection";
Database::query($s_sql_update_logout_date);
} else {
// it isn't, we should create a fresh entry
event_login();
}
}

@ -53,34 +53,54 @@ function event_open()
}
/**
* @author Sebastien Piraux <piraux_seb@hotmail.com>
* @desc Record information for login event
* (when an user identifies himself with username & password)
* @author Sebastien Piraux <piraux_seb@hotmail.com> old code
* @author Julio Montoya 2013
* @desc Record information for login event when an user identifies himself with username & password
*/
function event_login()
function event_login(\Entity\User $user)
{
$userId = $user->getUserId();
$TABLETRACK_LOGIN = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
$_user = api_get_user_info();
$reallyNow = api_get_utc_datetime();
$sql = "INSERT INTO ".$TABLETRACK_LOGIN." (login_user_id, login_ip, login_date, logout_date) VALUES
('".api_get_user_id()."',
('".$userId."',
'".Database::escape_string(api_get_real_ip())."',
'".$reallyNow."',
'".$reallyNow."'
)";
Database::query($sql);
$roles = $user->getRolesObj();
// auto subscribe
$user_status = $_user['status'] == SESSIONADMIN ? 'sessionadmin' :
$_user['status'] == COURSEMANAGER ? 'teacher' :
$_user['status'] == DRH ? 'DRH' : 'student';
$autoSubscribe = api_get_setting($user_status.'_autosubscribe');
if ($autoSubscribe) {
$autoSubscribe = explode('|', $autoSubscribe);
foreach ($autoSubscribe as $code) {
if (CourseManager::course_exists($code)) {
CourseManager::subscribe_user($_user['user_id'], $code);
/** @var \Entity\Role $role */
foreach ($roles as $role) {
$role = $role->getRole();
$userStatusParsed = 'student';
switch ($role) {
case 'ROLE_SESSION_MANAGER':
$userStatusParsed = 'sessionadmin';
break;
case 'ROLE_TEACHER':
$userStatusParsed = 'teacher';
break;
case 'ROLE_RRHH':
$userStatusParsed = 'DRH';
break;
}
$autoSubscribe = api_get_setting($userStatusParsed.'_autosubscribe');
if ($autoSubscribe) {
$autoSubscribe = explode('|', $autoSubscribe);
foreach ($autoSubscribe as $code) {
if (CourseManager::course_exists($code)) {
CourseManager::subscribe_user($userId, $code);
}
}
}
}

@ -35,17 +35,24 @@ class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
*/
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
/** @var \Entity\User $user */
$user = $token->getUser();
$userId = $user->getUserId();
event_login($user);
$session = $request->getSession();
// Setting last login datetime
$session->set('user_last_login_datetime', api_get_utc_datetime());
$response = null;
//$session = $request->getSession();
/* Possible values: index.php, user_portal.php, main/auth/courses.php */
$pageAfterLogin = api_get_setting('page_after_login');
$user = $token->getUser();
$userId = $user->getUserId();
$url = null;
if ($this->security->isGranted('ROLE_STUDENT') && !empty($pageAfterLogin)) {
switch($pageAfterLogin) {
switch ($pageAfterLogin) {
case 'index.php':
$url = $this->router->generate('index');
break;
@ -57,6 +64,7 @@ class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
break;
}
}
// Redirecting to a course or a session
if (api_get_setting('go_to_course_after_login') == 'true') {

@ -28,7 +28,6 @@ class IndexController extends CommonController
/** @var \Template $template */
$template = $app['template'];
/*
$token = $app['security']->getToken();
if (null !== $token) {
@ -216,46 +215,6 @@ class IndexController extends CommonController
return new Response($response, 200, array('Cache-Control' => 's-maxage=3600, public'));
}
/**
*
* @todo This piece of code should probably move to local.inc.php where the actual login procedure is handled.
* @todo Check if this code is used. I think this code is never executed because after clicking the submit button
* the code does the stuff in local.inc.php and then redirects to index.php or user_portal.php depending
* on api_get_setting('page_after_login').
* @deprecated seems not to be used
*/
function check_last_login()
{
if (!empty($_POST['submitAuth'])) {
// The user has been already authenticated, we are now to find the last login of the user.
if (!empty($this->user_id)) {
$track_login_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
$sql_last_login = "SELECT login_date
FROM $track_login_table
WHERE login_user_id = '".$this->user_id."'
ORDER BY login_date DESC LIMIT 1";
$result_last_login = Database::query($sql_last_login);
if (!$result_last_login) {
if (Database::num_rows($result_last_login) > 0) {
$user_last_login_datetime = Database::fetch_array($result_last_login);
$user_last_login_datetime = $user_last_login_datetime[0];
Session::write('user_last_login_datetime', $user_last_login_datetime);
}
}
Database::free_result($result_last_login);
if (api_is_platform_admin()) {
// Decode all open event informations and fill the track_c_* tables
include api_get_path(LIBRARY_PATH).'stats.lib.inc.php';
decodeOpenInfos();
}
}
} else {
// Only if login form was not sent because if the form is sent the user was already on the page.
event_open();
}
}
/**
* @param \Silex\Application $app
*/
@ -465,7 +424,6 @@ class IndexController extends CommonController
}
}
/**
* Reacts on a failed login.
* Displays an explanation with a link to the registration form.

@ -87,90 +87,6 @@ class UserPortalController extends CommonController
$response = $app['template']->render_template('userportal/index.tpl');
//return new Response($response, 200, array('Cache-Control' => 's-maxage=3600, private'));
return new Response($response, 200, array());
}
/**
* Redirects after login
*/
public function redirectAfterLogin()
{
// Get the courses list
$personal_course_list = \UserManager::get_personal_session_course_list(api_get_user_id());
$my_session_list = array();
$count_of_courses_no_sessions = 0;
$count_of_courses_with_sessions = 0;
foreach ($personal_course_list as $course) {
if (!empty($course['id_session'])) {
$my_session_list[$course['id_session']] = true;
$count_of_courses_with_sessions++;
} else {
$count_of_courses_no_sessions++;
}
}
$count_of_sessions = count($my_session_list);
if ($count_of_sessions == 1 && $count_of_courses_no_sessions == 0) {
$key = array_keys($personal_course_list);
$course_info = $personal_course_list[$key[0]];
$id_session = isset($course_info['id_session']) ? $course_info['id_session'] : 0;
$url = api_get_path(WEB_CODE_PATH).'session/index.php?session_id='.$id_session;
header('location:'.$url);
exit;
}
if (!isset($_SESSION['coursesAlreadyVisited']) && $count_of_sessions == 0 && $count_of_courses_no_sessions == 1) {
$key = array_keys($personal_course_list);
$course_info = $personal_course_list[$key[0]];
$course_directory = $course_info['course_info']['path'];
$id_session = isset($course_info['id_session']) ? $course_info['id_session'] : 0;
$url = api_get_path(WEB_COURSE_PATH).$course_directory.'/index.php?id_session='.$id_session;
header('location:'.$url);
exit;
}
}
public function check_last_login()
{
/**
* @todo This piece of code should probably move to local.inc.php where the actual login procedure is handled.
* @todo Check if this code is used. I think this code is never executed because after clicking the submit button
* the code does the stuff in local.inc.php and then redirects to index.php or user_portal.php depending
* on api_get_setting('page_after_login').
*/
if (!empty($_POST['submitAuth'])) {
// The user has been already authenticated, we are now to find the last login of the user.
if (!empty($this->user_id)) {
$track_login_table = Database :: get_main_table(TABLE_STATISTIC_TRACK_E_LOGIN);
$sql_last_login = "SELECT login_date
FROM $track_login_table
WHERE login_user_id = '".$this->user_id."'
ORDER BY login_date DESC LIMIT 1";
$result_last_login = Database::query($sql_last_login);
if (!$result_last_login) {
if (Database::num_rows($result_last_login) > 0) {
$user_last_login_datetime = Database::fetch_array($result_last_login);
$user_last_login_datetime = $user_last_login_datetime[0];
Session::write('user_last_login_datetime', $user_last_login_datetime);
}
}
Database::free_result($result_last_login);
if (api_is_platform_admin()) {
// decode all open event informations and fill the track_c_* tables
include api_get_path(LIBRARY_PATH).'stats.lib.inc.php';
decodeOpenInfos();
}
}
// End login -- if ($_POST['submitAuth'])
} else {
// Only if login form was not sent because if the form is sent the user was already on the page.
event_open();
}
}
}

Loading…
Cancel
Save