Moodle
  1. Moodle
  2. MDL-37177

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

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.4
    • Fix Version/s: 2.4.2
    • Component/s: General
    • Labels:
    • Rank:
      46750

      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()

        Activity

        Hide
        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
        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 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 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
        Helen Foster added a comment -

        Thanks Matteo for your help. Adding a patch label.

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

        Thanks Matteo,
        I tested and it works very well

        Show
        Moien Abadi added a comment - Thanks Matteo, I tested and it works very well
        Hide
        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
        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
        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
        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
        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
        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
        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
        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
        Frédéric Massart added a comment -

        Thanks Andrew, pushing forward!

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

        Thanks Fred,

        This fix is in integration now.

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

        All working Fred

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

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

        Regards, Damyon

        Show
        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: