Move login functions in UserManager

- Add php file to test that script is logged as first admin user.
pull/2487/head
jmontoyaa 9 years ago
parent 6641d11ff1
commit 48e2daf08b
  1. 109
      main/admin/user_list.php
  2. 15
      main/inc/lib/online.inc.php
  3. 95
      main/inc/lib/usermanager.lib.php
  4. 18
      tests/scripts/login_as_admin.php

@ -156,8 +156,36 @@ $this_section = SECTION_PLATFORM_ADMIN;
if ($action == 'login_as') {
$check = Security::check_token('get');
if (isset($_GET['user_id']) && $check) {
$result = loginUser($_GET['user_id']);
if ($result == false) {
$result = UserManager::loginAsUser($_GET['user_id']);
if ($result) {
$userInfo = api_get_user_info();
$firstname = $userInfo['firstname'];
$lastname = $userInfo['lastname'];
$userId = $userInfo['id'];
if (api_is_western_name_order()) {
$message = sprintf(
get_lang('AttemptingToLoginAs'),
$firstname,
$lastname,
$userId
);
} else {
$message = sprintf(
get_lang('AttemptingToLoginAs'),
$lastname,
$firstname,
$userId
);
}
$target_url = api_get_path(WEB_PATH)."user_portal.php";
$message .= '<br />'.sprintf(get_lang('LoginSuccessfulGoToX'), '<a href="'.$target_url.'">'.$target_url.'</a>');
Display :: display_header(get_lang('UserList'));
echo Display::return_message($message, 'normal', false);
Display :: display_footer();
exit;
} else {
api_not_allowed(true);
}
}
@ -368,83 +396,6 @@ function prepare_user_sql_query($is_count)
return $sql;
}
/**
* Make sure this function is protected because it does NOT check password!
*
* This function defines globals.
* @param int $userId
*
* @return false|null False on failure, redirection on success
* @author Evie Embrechts
* @author Yannick Warnier <yannick.warnier@dokeos.com>
*/
function loginUser($userId)
{
$userId = intval($userId);
$userInfo = api_get_user_info($userId);
// Check if the user is allowed to 'login_as'
$canLoginAs = api_can_login_as($userId);
if (!$canLoginAs || empty($userInfo)) {
return false;
}
$firstname = $userInfo['firstname'];
$lastname = $userInfo['lastname'];
if (api_is_western_name_order()) {
$message = sprintf(
get_lang('AttemptingToLoginAs'),
$firstname,
$lastname,
$userId
);
} else {
$message = sprintf(
get_lang('AttemptingToLoginAs'),
$lastname,
$firstname,
$userId
);
}
if ($userId) {
// Logout the current user
LoginDelete(api_get_user_id());
Session::erase('_user');
Session::erase('is_platformAdmin');
Session::erase('is_allowedCreateCourse');
Session::erase('_uid');
// Cleaning session variables
$_user['firstName'] = $userInfo['firstname'];
$_user['lastName'] = $userInfo['lastname'];
$_user['mail'] = $userInfo['email'];
$_user['official_code'] = $userInfo['official_code'];
$_user['picture_uri'] = $userInfo['picture_uri'];
$_user['user_id'] = $userId;
$_user['id'] = $userId;
$_user['status'] = $userInfo['status'];
// Filling session variables with new data
Session::write('_uid', $userId);
Session::write('_user', $userInfo);
Session::write('is_platformAdmin', (bool) (UserManager::is_admin($userId)));
Session::write('is_allowedCreateCourse', (bool) ($userInfo['status'] == 1));
// will be useful later to know if the user is actually an admin or not (example reporting)
Session::write('login_as', true);
$target_url = api_get_path(WEB_PATH)."user_portal.php";
$message .= '<br />'.sprintf(get_lang('LoginSuccessfulGoToX'), '<a href="'.$target_url.'">'.$target_url.'</a>');
Display :: display_header(get_lang('UserList'));
echo Display::return_message($message, 'normal', false);
Display :: display_footer();
exit;
}
}
/**
* Get the total number of users on the platform
* @see SortableTable#get_total_number_of_items()

@ -130,7 +130,7 @@ function online_logout($user_id = null, $logout_redirect = false)
Database::query($sql);
}
LoginDelete($user_id); //from inc/lib/online.inc.php - removes the "online" status
UserManager::loginDelete($user_id);
//the following code enables the use of an external logout function.
//example: define a $extAuthSource['ldap']['logout']="file.php" in configuration.php
@ -160,19 +160,6 @@ function online_logout($user_id = null, $logout_redirect = false)
}
}
/**
* Remove all login records from the track_e_online stats table, for the given user ID.
* @param int User ID
* @param integer $user_id
* @return void
*/
function LoginDelete($user_id)
{
$online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
$user_id = intval($user_id);
$query = "DELETE FROM ".$online_table." WHERE login_user_id = $user_id";
Database::query($query);
}
/**
* @param int $user_id

@ -7,6 +7,7 @@ use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
use Symfony\Component\Security\Core\Encoder\EncoderFactory;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
use Symfony\Component\Security\Core\Encoder\PlaintextPasswordEncoder;
use ChamiloSession as Session;
/**
*
@ -5298,4 +5299,98 @@ SQL;
return Display::tabsOnlyLink($headers, $optionSelected);
}
}
/**
* Make sure this function is protected because it does NOT check password!
*
* This function defines globals.
* @param int $userId
* @param bool $checkIfUserCanLoginAs
* @return array
* @author Evie Embrechts
* @author Yannick Warnier <yannick.warnier@dokeos.com>
*/
public static function loginAsUser($userId, $checkIfUserCanLoginAs = true)
{
$userId = intval($userId);
$userInfo = api_get_user_info($userId);
// Check if the user is allowed to 'login_as'
$canLoginAs = true;
if ($checkIfUserCanLoginAs) {
$canLoginAs = api_can_login_as($userId);
}
if (!$canLoginAs || empty($userInfo)) {
return false;
}
if ($userId) {
// Logout the current user
self::loginDelete(api_get_user_id());
Session::erase('_user');
Session::erase('is_platformAdmin');
Session::erase('is_allowedCreateCourse');
Session::erase('_uid');
// Cleaning session variables
$_user['firstName'] = $userInfo['firstname'];
$_user['lastName'] = $userInfo['lastname'];
$_user['mail'] = $userInfo['email'];
$_user['official_code'] = $userInfo['official_code'];
$_user['picture_uri'] = $userInfo['picture_uri'];
$_user['user_id'] = $userId;
$_user['id'] = $userId;
$_user['status'] = $userInfo['status'];
// Filling session variables with new data
Session::write('_uid', $userId);
Session::write('_user', $userInfo);
Session::write('is_platformAdmin', (bool) UserManager::is_admin($userId));
Session::write('is_allowedCreateCourse', (bool) ($userInfo['status'] == 1));
// will be useful later to know if the user is actually an admin or not (example reporting)
Session::write('login_as', true);
return true;
}
return false;
}
/**
* Remove all login records from the track_e_online stats table,
* for the given user ID.
* @param int User ID
* @param integer $user_id
* @return void
*/
public static function loginDelete($user_id)
{
$online_table = Database::get_main_table(TABLE_STATISTIC_TRACK_E_ONLINE);
$user_id = intval($user_id);
$query = "DELETE FROM ".$online_table." WHERE login_user_id = $user_id";
Database::query($query);
}
/**
* Login as first admin user registered in the platform
* @return array
*/
public static function logInAsFirstAdmin()
{
$adminList = self::get_all_administrators();
if (!empty($adminList)) {
$userInfo = current($adminList);
if (!empty($userInfo)) {
$result = self::loginAsUser($userInfo['user_id'], false);
if ($result && api_is_platform_admin()) {
return api_get_user_info();
}
}
}
return [];
}
}

@ -0,0 +1,18 @@
<?php
/* For licensing terms, see /license.txt */
exit;
if (PHP_SAPI != 'cli') {
die('This script can only be executed from the command line');
}
require_once __DIR__.'/../../main/inc/global.inc.php';
$userInfo = UserManager::logInAsFirstAdmin();
if (api_is_platform_admin()) {
echo 'Logged as admin user: '.$userInfo['complete_name'];
} else {
echo 'NOT logged as admin ';
}
Loading…
Cancel
Save