/auth/shibboleth/index.php

https://github.com/jarednipper/HSU-common-code · PHP · 97 lines · 58 code · 27 blank · 12 comment · 20 complexity · 0f8beed2a1342ad516a60a7c1fbc3a30 MD5 · raw file

  1. <?php // $Id: index.php,v 1.15.2.5 2009/10/09 11:07:22 exe-cutor Exp $
  2. // Designed to be redirected from moodle/login/index.php
  3. require('../../config.php');
  4. if (isloggedin() && $USER->username != 'guest') { // Nothing to do
  5. if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
  6. $urltogo = $SESSION->wantsurl; /// Because it's an address in this site
  7. unset($SESSION->wantsurl);
  8. } else {
  9. $urltogo = $CFG->wwwroot.'/'; /// Go to the standard home page
  10. unset($SESSION->wantsurl); /// Just in case
  11. }
  12. redirect($urltogo);
  13. }
  14. $pluginconfig = get_config('auth/shibboleth');
  15. $shibbolethauth = get_auth_plugin('shibboleth');
  16. // Check whether Shibboleth is configured properly
  17. if (empty($pluginconfig->user_attribute)) {
  18. print_error('shib_not_set_up_error', 'auth');
  19. }
  20. /// If we can find the Shibboleth attribute, save it in session and return to main login page
  21. if (!empty($_SERVER[$pluginconfig->user_attribute])) { // Shibboleth auto-login
  22. $frm->username = strtolower($_SERVER[$pluginconfig->user_attribute]);
  23. $frm->password = substr(base64_encode($_SERVER[$pluginconfig->user_attribute]),0,8);
  24. // The random password consists of the first 8 letters of the base 64 encoded user ID
  25. // This password is never used unless the user account is converted to manual
  26. /// Check if the user has actually submitted login data to us
  27. if ($shibbolethauth->user_login($frm->username, $frm->password)) {
  28. $USER = authenticate_user_login($frm->username, $frm->password);
  29. $USER->loggedin = true;
  30. $USER->site = $CFG->wwwroot; // for added security, store the site in the
  31. update_user_login_times();
  32. // Don't show username on login page
  33. set_moodle_cookie('nobody');
  34. set_login_session_preferences();
  35. unset($SESSION->lang);
  36. $SESSION->justloggedin = true;
  37. add_to_log(SITEID, 'user', 'login', "view.php?id=$USER->id&course=".SITEID, $USER->id, 0, $USER->id);
  38. if (user_not_fully_set_up($USER)) {
  39. $urltogo = $CFG->wwwroot.'/user/edit.php?id='.$USER->id.'&amp;course='.SITEID;
  40. // We don't delete $SESSION->wantsurl yet, so we get there later
  41. } else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
  42. $urltogo = $SESSION->wantsurl; /// Because it's an address in this site
  43. unset($SESSION->wantsurl);
  44. } else {
  45. $urltogo = $CFG->wwwroot.'/'; /// Go to the standard home page
  46. unset($SESSION->wantsurl); /// Just in case
  47. }
  48. /// Go to my-moodle page instead of homepage if mymoodleredirect enabled
  49. if (!has_capability('moodle/site:config',get_context_instance(CONTEXT_SYSTEM)) and !empty($CFG->mymoodleredirect) and !isguest()) {
  50. if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') {
  51. $urltogo = $CFG->wwwroot.'/my/';
  52. }
  53. }
  54. check_enrolment_plugins($USER);
  55. load_all_capabilities(); /// This is what lets the user do anything on the site :-)
  56. redirect($urltogo);
  57. exit;
  58. }
  59. else {
  60. // For some weird reason the Shibboleth user couldn't be authenticated
  61. }
  62. }
  63. // If we can find any (user independent) Shibboleth attributes but no user
  64. // attributes we probably didn't receive any user attributes
  65. elseif (!empty($_SERVER['HTTP_SHIB_APPLICATION_ID']) || !empty($_SERVER['Shib-Application-ID'])) {
  66. print_error('shib_no_attributes_error', 'auth' , '', '\''.$pluginconfig->user_attribute.'\', \''.$pluginconfig->field_map_firstname.'\', \''.$pluginconfig->field_map_lastname.'\' and \''.$pluginconfig->field_map_email.'\'');
  67. } else {
  68. print_error('shib_not_set_up_error', 'auth');
  69. }
  70. ?>