shibboleth_session.class.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Shibboleth;
  3. use ChamiloSession as Session;
  4. use Database;
  5. use Event;
  6. /**
  7. * A Chamilo user session. Used as there is no session object so far provided by the core API.
  8. * Should be moved to the core library.Prefixed by Shibboleth to avoid name clashes.
  9. *
  10. * @license see /license.txt
  11. * @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
  12. */
  13. class ShibbolethSession
  14. {
  15. /**
  16. * @return ShibbolethSession
  17. */
  18. public static function instance()
  19. {
  20. static $result = false;
  21. if (empty($result)) {
  22. $result = new self();
  23. }
  24. return $result;
  25. }
  26. function is_logged_in()
  27. {
  28. return isset($_SESSION['_user']['user_id']);
  29. }
  30. function user()
  31. {
  32. return $_SESSION['_user'];
  33. }
  34. function logout()
  35. {
  36. $_SESSION['_user'] = array();
  37. online_logout(null, false);
  38. global $logoutInfo;
  39. Event::courseLogout($logoutInfo);
  40. }
  41. /**
  42. * Create a Shibboleth session for the user ID
  43. *
  44. * @param string $uid The user ID
  45. * @return array $_user The user infos array created when the user logs in
  46. */
  47. function login($uid)
  48. {
  49. /* This must be set for local.inc.php to register correctly the global variables in session
  50. * This is BAD. Logic should be migrated into a function and stop relying on global variables.
  51. */
  52. global $_uid, $is_allowedCreateCourse, $is_platformAdmin, $_real_cid, $is_courseAdmin;
  53. global $is_courseMember, $is_courseTutor, $is_session_general_coach, $is_allowed_in_course, $is_sessionAdmin, $_gid;
  54. $_uid = $uid;
  55. //is_allowedCreateCourse
  56. $user = User::store()->get_by_user_id($uid);
  57. if (empty($user)) {
  58. return;
  59. }
  60. $this->logout();
  61. Session::instance();
  62. Session::write('_uid', $_uid);
  63. global $_user;
  64. $_user = (array) $user;
  65. $_SESSION['_user'] = $_user;
  66. $_SESSION['_user']['user_id'] = $_uid;
  67. $_SESSION['noredirection'] = true;
  68. //must be called before 'init_local.inc.php'
  69. Event::eventLogin($_uid);
  70. //used in 'init_local.inc.php' this is BAD but and should be changed
  71. $loginFailed = false;
  72. $uidReset = true;
  73. $gidReset = true;
  74. $cidReset = false; //FALSE !!
  75. $mainDbName = Database :: get_main_database();
  76. $includePath = api_get_path(SYS_INC_PATH);
  77. $no_redirection = true;
  78. require("$includePath/local.inc.php");
  79. return $_user;
  80. }
  81. }