shibboleth_session.class.php 2.4 KB

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