Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #55: [UPD] make userkey respect suspended status of users #87

Open
wants to merge 3 commits into
base: MOODLE_33PLUS
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,15 @@ public function user_login_userkey() {
$this->userkeymanager->delete_keys($key->userid);

$user = get_complete_user_data('id', $key->userid);
if (!empty($user->suspended)) {
$failurereason = AUTH_LOGIN_SUSPENDED;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you really need this variable? Could be passed as is to the code below.


// Trigger login failed event.
$event = \core\event\user_login_failed::create(array('userid' => $user->id,
'other' => array('username' => $user->username, 'reason' => $failurereason)));
$event->trigger();
throw new invalid_parameter_exception('User suspended');
Copy link
Member

@dmitriim dmitriim Aug 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this error will be displayed to a user, can we please replace this with moodle_exception + add a lang string so it could be localised.

}
complete_user_login($user);

// Identify this session as using user key auth method.
Expand Down
19 changes: 19 additions & 0 deletions tests/auth_plugin_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,25 @@ public function test_throwing_exception_if_user_is_not_exist() {
$this->expectExceptionMessage('Invalid parameter value detected (User is not exist)');
$actual = $this->auth->get_login_url($user);
}
/**
* Test that auth plugin throws correct exception if we trying to request not existing user.
*/
public function test_throwing_exception_if_user_is_suspended() {
global $USER, $SESSION;

$user = $this->getDataGenerator()->create_user();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is already created user for the test. You can use $this->user.

$user->suspended = 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to save this state to DB.

$this->setUser($user);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are you trying to test here? I don't think you need set a user here and then try to login again, but using a key.

$this->assertEquals($USER->id, $user->id);

$this->create_user_private_key();
$_POST['key'] = 'TestKey';

$this->expectException(invalid_parameter_exception::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

before checking this, can you please check that login failed event has been triggered?

$this->expectExceptionMessage('Invalid parameter value detected (User is suspended)');

$this->auth->user_login_userkey();
}

/**
* Test that auth plugin throws correct exception if we trying to request user,
Expand Down