shibboleth_controller.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace Shibboleth;
  3. use \Redirect;
  4. use \Display;
  5. use IndexManager;
  6. /**
  7. * Controller for the Shibboleth authentication system.
  8. *
  9. * @license see /license.txt
  10. * @author Laurent Opprecht <laurent@opprecht.info>, Nicolas Rod for the University of Geneva
  11. */
  12. class ShibbolethController
  13. {
  14. /**
  15. *
  16. * @return ShibbolethController
  17. */
  18. public static function instance()
  19. {
  20. static $result = false;
  21. if (empty($result))
  22. {
  23. $result = new self();
  24. }
  25. return $result;
  26. }
  27. /**
  28. * Log user in with Shibboleth authentication
  29. */
  30. function login()
  31. {
  32. if (Shibboleth::session()->is_logged_in())
  33. {
  34. Redirect::home();
  35. }
  36. $user = Shibboleth::store()->get_user();
  37. if ($user->is_empty())
  38. {
  39. $message = get_lang('SystemCouldNotLogYouIn');
  40. Shibboleth::display()->error_page($message);
  41. }
  42. $is_new_user = !User::store()->shibboleth_id_exists($user->unique_id);
  43. if ($is_new_user && empty($user->email) && Shibboleth::config()->is_email_mandatory)
  44. {
  45. $form = ShibbolethEmailForm::instance();
  46. if ($email = $form->get_email())
  47. {
  48. $user->email = $email;
  49. }
  50. else
  51. {
  52. $content = $form->display();
  53. Shibboleth::display()->page($content);
  54. }
  55. }
  56. Shibboleth::save($user);
  57. $chamilo_user = User::store()->get_by_shibboleth_id($user->unique_id);
  58. Shibboleth::session()->login($chamilo_user->user_id);
  59. if ($is_new_user && $user->status_request)
  60. {
  61. Shibboleth::redirect('/main/auth/shibboleth/app/view/request.php');
  62. }
  63. else
  64. {
  65. Shibboleth::redirect();
  66. }
  67. }
  68. /**
  69. * Log user in using the standard Chamilo way of logging in.
  70. * Useful when the normal login screen is removed from the user interface
  71. * - replaced by Shibboleth login - and user want to login using a standard
  72. * account
  73. */
  74. public function admin_login()
  75. {
  76. $title = get_lang('InternalLogin');
  77. if (Shibboleth::session()->is_logged_in())
  78. {
  79. $message = get_lang('AlreadyLoggedIn');
  80. Shibboleth::display()->message_page($message, $title);
  81. }
  82. $index_manager = new IndexManager('');
  83. $html = $index_manager->display_login_form();
  84. Shibboleth::display()->page($html, $title);
  85. }
  86. /**
  87. * Display the request new status page to administrator for new users.
  88. */
  89. public function request_status()
  90. {
  91. /*
  92. * That may happen if a user visit that url again.
  93. */
  94. if (!Shibboleth::session()->is_logged_in())
  95. {
  96. Shibboleth::redirect();
  97. }
  98. $user = Shibboleth::session()->user();
  99. if ($user['status'] == Shibboleth::TEACHER_STATUS)
  100. {
  101. //Maximum user right is reached.
  102. Shibboleth::redirect();
  103. }
  104. $form = ShibbolethStatusRequestForm::instance();
  105. if ($form->cancelled())
  106. {
  107. Shibboleth::redirect();
  108. }
  109. if ($reason = $form->get_reason())
  110. {
  111. $subject = get_lang('RequestStatus');
  112. $status = $form->get_status();
  113. $status = Shibboleth::format_status($status);
  114. $message = <<<EOT
  115. New status: $status
  116. Reason:
  117. $reason
  118. EOT;
  119. $success = Shibboleth::email_admin($subject, $message);
  120. if ($success)
  121. {
  122. $request_submitted = get_lang('RequestSubmitted');
  123. Shibboleth::display()->message_page($request_submitted);
  124. }
  125. else
  126. {
  127. $request_failed = get_lang('RequestFailed');
  128. Shibboleth::display()->error_page($request_failed);
  129. }
  130. }
  131. $title = get_lang('RequestStatus');
  132. Display :: display_header($title);
  133. echo $form->display();
  134. Display :: display_footer();
  135. }
  136. }