Uploaded image for project: 'Moodle'
  1. Moodle
  2. MDL-37177

"Invalid user" error when non-logged user attempts to access teacher profile

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 2.4
    • Fix Version/s: 2.4.2
    • Component/s: General
    • Labels:
    • Testing Instructions:
      Hide
      1. Set forceloginforprofiles and forcelogin to no.
      2. Enrol a teacher in a course
      3. Make sure you can view the link to your private files in the navigation block
      4. Logout and click on a teacher's name in the list of available courses on the front page
      5. Make sure you can view the profile
      Show
      Set forceloginforprofiles and forcelogin to no. Enrol a teacher in a course Make sure you can view the link to your private files in the navigation block Logout and click on a teacher's name in the list of available courses on the front page Make sure you can view the profile
    • Affected Branches:
      MOODLE_24_STABLE
    • Fixed Branches:
      MOODLE_24_STABLE
    • Pull from Repository:
    • Pull Master Branch:
      MDL-37177-master

      Description

      I am setting up the latest Moodle 2.4+, but I cannot set teacher profile to show on front page for site visitors. I have set forceloginforprofiles to NO, but a site visitor on clicking TEACHER from the front page is redirected to this, all in a red block:

      Invalid user
      More information about this error

      I discussed this problem with Mary Cooch in https://moodle.org/mod/forum/discuss.php?d=218080 and she replicated the problem, which does not exist in Moodle 2.3.

      Steps to reproduce:

      1. Set forceloginforprofiles to no then log out
      2. Click on a teacher's name in the list of available courses on the front page

      The following error is obtained:

      Invalid user

      Debug info: SELECT id FROM

      {user}

      WHERE id = ? AND deleted = ?
      [array (
      0 => 0,
      1 => 0,
      )]
      Error code: invaliduser
      Stack trace:
      line 1357 of /lib/dml/moodle_database.php: dml_missing_record_exception thrown
      line 1333 of /lib/dml/moodle_database.php: call to moodle_database->get_record_select()
      line 6120 of /lib/accesslib.php: call to moodle_database->get_record()
      line 2158 of /lib/navigationlib.php: call to context_user::instance()
      line 1223 of /lib/navigationlib.php: call to global_navigation->load_for_user()
      line 3114 of /lib/navigationlib.php: call to global_navigation->initialise()
      line 700 of /lib/pagelib.php: call to settings_navigation->__construct()
      line 717 of /lib/pagelib.php: call to moodle_page->magic_get_settingsnav()
      line 137 of /user/profile.php: call to moodle_page->__get()

        Gliffy Diagrams

          Activity

          Hide
          tsala Helen Foster added a comment -

          Erlyn, thanks for reporting this issue. I'm increasing the priority and adding the issue to our stable backlog.

          Show
          tsala Helen Foster added a comment - Erlyn, thanks for reporting this issue. I'm increasing the priority and adding the issue to our stable backlog.
          Hide
          matteo Matteo Scaramuccia added a comment -

          Hi Erlyn,
          in the mean time you can try this quick and dirty fix:

          diff --git a/lib/navigationlib.php b/lib/navigationlib.php
          index b4e69c6..6c63517 100644
          --- a/lib/navigationlib.php
          +++ b/lib/navigationlib.php
          @@ -2155,10 +2155,12 @@ class global_navigation extends navigation_node {
                       $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages');
                   }
           
          -        $context = context_user::instance($USER->id);
          -        if ($iscurrentuser && has_capability('moodle/user:manageownfiles', $context)) {
          -            $url = new moodle_url('/user/files.php');
          -            $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING);
          +        if (isloggedin()) {
          +            $context = context_user::instance($USER->id);
          +            if ($iscurrentuser && has_capability('moodle/user:manageownfiles', $context)) {
          +                $url = new moodle_url('/user/files.php');
          +                $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING);
          +            }
                   }
           
                   // Add a node to view the users notes if permitted
           

          This quick fix is dirty because:

          • it is not reviewed
          • it is not tested
          • the code could be probably better refactored

          HTH,
          Matteo

          Show
          matteo Matteo Scaramuccia added a comment - Hi Erlyn, in the mean time you can try this quick and dirty fix: diff --git a/lib/navigationlib.php b/lib/navigationlib.php index b4e69c6..6c63517 100644 --- a/lib/navigationlib.php +++ b/lib/navigationlib.php @@ -2155,10 +2155,12 @@ class global_navigation extends navigation_node { $usernode->add(get_string('messages', 'message'), $url, self::TYPE_SETTING, null, 'messages'); }   - $context = context_user::instance($USER->id); - if ($iscurrentuser && has_capability('moodle/user:manageownfiles', $context)) { - $url = new moodle_url('/user/files.php'); - $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING); + if (isloggedin()) { + $context = context_user::instance($USER->id); + if ($iscurrentuser && has_capability('moodle/user:manageownfiles', $context)) { + $url = new moodle_url('/user/files.php'); + $usernode->add(get_string('myfiles'), $url, self::TYPE_SETTING); + } }   // Add a node to view the users notes if permitted   This quick fix is dirty because: it is not reviewed it is not tested the code could be probably better refactored HTH, Matteo
          Hide
          tsala Helen Foster added a comment -

          Thanks Matteo for your help. Adding a patch label.

          Show
          tsala Helen Foster added a comment - Thanks Matteo for your help. Adding a patch label.
          Hide
          moien.abadi Moien Abadi added a comment -

          Thanks Matteo,
          I tested and it works very well

          Show
          moien.abadi Moien Abadi added a comment - Thanks Matteo, I tested and it works very well
          Hide
          ebaack Erlyn Baack added a comment -

          Hi Matteo, Thanks, but I do not know how to apply this info. For me its application will have to wait until it's put into a future 2.4+ Moodle download. Thanks!

          Show
          ebaack Erlyn Baack added a comment - Hi Matteo, Thanks, but I do not know how to apply this info. For me its application will have to wait until it's put into a future 2.4+ Moodle download. Thanks!
          Hide
          fred Frédéric Massart added a comment - - edited

          This is a patch for 2.4 and master. The issue was that we were trying to fetch the context of $USER which is not authenticated, hence the exception. This issue does not happen in 2.3 because the context fetch uses IGNORE_MISSING.

          Show
          fred Frédéric Massart added a comment - - edited This is a patch for 2.4 and master. The issue was that we were trying to fetch the context of $USER which is not authenticated, hence the exception. This issue does not happen in 2.3 because the context fetch uses IGNORE_MISSING .
          Hide
          andyjdavis Andrew Davis added a comment -

          [Y] Syntax
          [NA] Output
          [Y] Whitespace
          [NA] Language
          [NA] Databases
          [?] Testing
          [Y] Security
          [NA] Documentation
          [Y] Git
          [Y] Sanity check

          Looks perfect. You are go for integration.

          Show
          andyjdavis Andrew Davis added a comment - [Y] Syntax [NA] Output [Y] Whitespace [NA] Language [NA] Databases [?] Testing [Y] Security [NA] Documentation [Y] Git [Y] Sanity check Looks perfect. You are go for integration.
          Hide
          andyjdavis Andrew Davis added a comment -

          Oh, maybe add something to the testing instructions to check that if block gets executed when it should. The change is so minor though it's barely worth doing.

          Show
          andyjdavis Andrew Davis added a comment - Oh, maybe add something to the testing instructions to check that if block gets executed when it should. The change is so minor though it's barely worth doing.
          Hide
          fred Frédéric Massart added a comment -

          Thanks Andrew, pushing forward!

          Show
          fred Frédéric Massart added a comment - Thanks Andrew, pushing forward!
          Hide
          damyon Damyon Wiese added a comment -

          Thanks Fred,

          This fix is in integration now.

          Show
          damyon Damyon Wiese added a comment - Thanks Fred, This fix is in integration now.
          Hide
          phalacee Jason Fowler added a comment -

          All working Fred

          Show
          phalacee Jason Fowler added a comment - All working Fred
          Hide
          damyon Damyon Wiese added a comment -

          Congratulations! This issue has been resolved. Thanks for helping to make Moodle better for everyone!

          Regards, Damyon

          Show
          damyon Damyon Wiese added a comment - Congratulations! This issue has been resolved. Thanks for helping to make Moodle better for everyone! Regards, Damyon

            People

            • Votes:
              2 Vote for this issue
              Watchers:
              8 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Fix Release Date:
                11/Mar/13