catalyst / moodle-auth_userkey

Log in to Moodle using one time user key based login URL. Auth plugin for organising simple SSO (single sign on) between moodle and your external web application.
https://moodle.org/plugins/auth_userkey
81 stars 53 forks source link

Allow login if a different session is open #102

Open IsycDev opened 8 months ago

IsycDev commented 8 months ago

Hello,

We are currently facing a problem when we try to connect to Moodle with a different user when there is another session open in Moodle. In our application integrated with Moodle, we allow the option to connect with a different user to see how they view the platform and, when accessing Moodle, the login does not work because there is an open session with another user.

In order for it to work, we have to make sure we log out of Moodle first, and then log back in through the plugin.

After investigating the currently working, we think the auth.php should be modified as follows:

$user = get_complete_user_data('id', $key->userid);
if ($user->suspended === "0" && $user->deleted === "0") {
   if (isloggedin()) {
          if ($key->userid <> $USER->id) {
            require_logout();
            $wantsurl = (!empty($wantsurl) ? '&wantsurl=' . $wantsurl : '');
            $redirecturl = $CFG->wwwroot.'/auth/userkey/login.php?key='.$keyvalue.$wantsurl;
            $this->redirect($redirecturl);
           } else {
            $this->redirect($redirecturl);
           }
    } else {
         $this->userkeymanager->delete_keys($key->userid);
         $user = get_complete_user_data('id', $key->userid);
         complete_user_login($user);

        // Identify this session as using user key auth method.
        $SESSION->userkey = true;
        $this->redirect($redirecturl);
     }
} else {
   require_logout();
   $this->redirect($CFG->wwwroot);
}
dmitriim commented 8 months ago

Hi @IsycDev If you could submit proposed changes as pull request this would speed up getting this issue resolved. Don't forget to add automated tests to support your change and test any possible edge cases.