[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/course/ -> loginas.php (source)

   1  <?php // $Id: loginas.php,v 1.47.2.2 2008/01/10 15:09:10 skodak Exp $
   2        // Allows a teacher/admin to login as another user (in stealth mode)
   3  
   4      require_once ('../config.php');
   5      require_once ('lib.php');
   6  
   7  /// Reset user back to their real self if needed
   8      $return = optional_param('return', 0, PARAM_BOOL);   // return to the page we came from
   9  
  10      if (!empty($USER->realuser)) {
  11          if (!confirm_sesskey()) {
  12              print_error('confirmsesskeybad');
  13          }
  14  
  15          $USER = get_complete_user_data('id', $USER->realuser);
  16          load_all_capabilities();   // load all this user's normal capabilities
  17  
  18          if (isset($SESSION->oldcurrentgroup)) {      // Restore previous "current group" cache.
  19              $SESSION->currentgroup = $SESSION->oldcurrentgroup;
  20              unset($SESSION->oldcurrentgroup);
  21          }
  22          if (isset($SESSION->oldtimeaccess)) {        // Restore previous timeaccess settings
  23              $USER->timeaccess = $SESSION->oldtimeaccess;
  24              unset($SESSION->oldtimeaccess);
  25          }
  26          if (isset($SESSION->grade_last_report)) {    // Restore grade defaults if any
  27              $USER->grade_last_report = $SESSION->grade_last_report;
  28              unset($SESSION->grade_last_report);
  29          }
  30  
  31          if ($return and isset($_SERVER["HTTP_REFERER"])) { // That's all we wanted to do, so let's go back
  32              redirect($_SERVER["HTTP_REFERER"]);
  33          } else {
  34              redirect($CFG->wwwroot);
  35          }
  36      }
  37  
  38  ///-------------------------------------
  39  /// We are trying to log in as this user in the first place
  40  
  41      $id     = optional_param('id', SITEID, PARAM_INT);   // course id
  42      $userid = required_param('user', PARAM_INT);         // login as this user
  43  
  44      if (!confirm_sesskey()) {
  45          print_error('confirmsesskeybad');
  46      }
  47  
  48      if (! $course = get_record('course', 'id', $id)) {
  49          error("Course ID was incorrect");
  50      }
  51  
  52  /// User must be logged in
  53  
  54      $systemcontext = get_context_instance(CONTEXT_SYSTEM);
  55      $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
  56  
  57      require_login();
  58  
  59      if (has_capability('moodle/user:loginas', $systemcontext)) {
  60          if (has_capability('moodle/site:doanything', $systemcontext, $userid, false)) {
  61              print_error('nologinas');
  62          }
  63          $context = $systemcontext;
  64      } else {
  65          require_login($course);
  66          require_capability('moodle/user:loginas', $coursecontext);
  67          if (!has_capability('moodle/course:view', $coursecontext, $userid, false)) {
  68              error('This user is not in this course!');
  69          }
  70          if (has_capability('moodle/site:doanything', $coursecontext, $userid, false)) {
  71              print_error('nologinas');
  72          }
  73          $context = $coursecontext;
  74      }
  75  
  76  /// Remember current timeaccess settings for later
  77  
  78      if (isset($USER->timeaccess)) {
  79          $SESSION->oldtimeaccess = $USER->timeaccess;
  80      }
  81      if (isset($USER->grade_last_report)) {
  82          $SESSION->grade_last_report = $USER->grade_last_report;
  83      }
  84  
  85  /// Login as this user and return to course home page.
  86  
  87      $oldfullname = fullname($USER, true);
  88      $olduserid   = $USER->id;
  89  
  90  /// Create the new USER object with all details and reload needed capabilitites
  91      $USER = get_complete_user_data('id', $userid);
  92      $USER->realuser = $olduserid;
  93      $USER->loginascontext = $context;
  94      check_enrolment_plugins($USER);
  95      load_all_capabilities();   // reload capabilities
  96  
  97      if (isset($SESSION->currentgroup)) {    // Remember current cache setting for later
  98          $SESSION->oldcurrentgroup = $SESSION->currentgroup;
  99          unset($SESSION->currentgroup);
 100      }
 101  
 102      $newfullname = fullname($USER, true);
 103  
 104      add_to_log($course->id, "course", "loginas", "../user/view.php?id=$course->id&amp;user=$userid", "$oldfullname -> $newfullname");
 105  
 106      $strloginas    = get_string('loginas');
 107      $strloggedinas = get_string('loggedinas', '', $newfullname);
 108  
 109      print_header_simple($strloggedinas, '', build_navigation(array(array('name'=>$strloggedinas, 'link'=>'','type'=>'misc'))),
 110              '', '', true, '&nbsp;', navmenu($course));
 111      notice($strloggedinas, "$CFG->wwwroot/course/view.php?id=$course->id");
 112  
 113  
 114  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7