shibboleth_controller.class.php 4.0 KB

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