Skip to content

Commit

Permalink
MDL-72325 user: Introduce new core_user::awaiting_action() method
Browse files Browse the repository at this point in the history
The method allows to check if the user is fully ready to use the site or
whether there is an action (such as filling the missing profile field,
changing password or agreeing to the site policy) needed.
  • Loading branch information
mudrd8mz committed Aug 12, 2021
1 parent 0d0e66d commit 7a8eae0
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/classes/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -1134,4 +1134,39 @@ public static function clean_preference($value, $preferencename) {
}
}

/**
* Is the user expected to perform an action to start using Moodle properly?
*
* This covers cases such as filling the profile, changing password or agreeing to the site policy.
*
* @param stdClass $user User object, defaults to the current user.
* @return bool
*/
public static function awaiting_action(stdClass $user = null): bool {
global $USER;

if ($user === null) {
$user = $USER;
}

if (user_not_fully_set_up($user)) {
// Awaiting the user to fill all fields in the profile.
return true;
}

if (get_user_preferences('auth_forcepasswordchange', false, $user)) {
// Awaiting the user to change their password.
return true;
}

if (empty($user->policyagreed) && !is_siteadmin($user)) {
$manager = new \core_privacy\local\sitepolicy\manager();

if ($manager->is_defined(isguestuser($user))) {
return true;
}
}

return false;
}
}
49 changes: 49 additions & 0 deletions lib/tests/user_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -770,4 +770,53 @@ public function test_is_real_user() {
$this->assertFalse(\core_user::is_real_user(core_user::get_support_user()->id, true));
}

/**
* Tests for the {@see \core_user::awaiting_action()} method.
*/
public function test_awaiting_action() {
global $CFG, $DB, $USER;

$guest = \core_user::get_user($CFG->siteguest);
$student = $this->getDataGenerator()->create_user();
$teacher = $this->getDataGenerator()->create_user();
$manager = $this->getDataGenerator()->create_user();
$admin = get_admin();

$this->getDataGenerator()->role_assign($DB->get_field('role', 'id', ['shortname' => 'manager']),
$manager->id, \context_system::instance()->id);

// Scenario: Guests required to agree to site policy.
$this->assertFalse(\core_user::awaiting_action($guest));

$CFG->sitepolicyguest = 'https://example.com';
$this->assertTrue(\core_user::awaiting_action($guest));

$guest->policyagreed = 1;
$this->assertFalse(\core_user::awaiting_action($guest));

// Scenario: Student required to fill their profile.
$this->assertFalse(\core_user::awaiting_action($student));

$student->firstname = '';
$this->assertTrue(\core_user::awaiting_action($student));

$student->firstname = 'Alice';
$this->assertFalse(\core_user::awaiting_action($student));

// Scenario: Teacher force to change their password.
$this->assertFalse(\core_user::awaiting_action($teacher));

set_user_preference('auth_forcepasswordchange', 1, $teacher);
$this->assertTrue(\core_user::awaiting_action($teacher));

unset_user_preference('auth_forcepasswordchange', $teacher);
$this->assertFalse(\core_user::awaiting_action($teacher));

// Scenario: Admins do not need to agree to the policy but others do.
$this->assertFalse(\core_user::awaiting_action($admin));
$this->assertFalse(\core_user::awaiting_action($manager));
$CFG->sitepolicy = 'https://example.com';
$this->assertFalse(\core_user::awaiting_action($admin));
$this->assertTrue(\core_user::awaiting_action($manager));
}
}
3 changes: 3 additions & 0 deletions lib/upgrade.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ information provided here is intended especially for developers.
completion_info::internal_set_data() to reaggregate completions that have been marked for instant course completion.
* The following functions have been finally deprecated and can not be used anymore:
- generate_uuid
* New method \core_user::awaiting_action() has been introduced to check if the user is fully ready to use the site or
whether there is an action (such as filling the missing profile field, changing password or agreeing to the site
policy) needed.

=== 3.11.2 ===
* For security reasons, filelib has been updated so all requests now use emulated redirects.
Expand Down

0 comments on commit 7a8eae0

Please sign in to comment.