Skip to content

Commit

Permalink
Merge branch 'MDL-55923-master' of git://github.com/jleyva/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Oct 10, 2016
2 parents 576ce0a + 9d382a9 commit 886207f
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 3 deletions.
5 changes: 5 additions & 0 deletions admin/settings/security.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@
$temp->add(new admin_setting_configcheckbox('passwordchangelogout',
new lang_string('passwordchangelogout', 'admin'),
new lang_string('passwordchangelogout_desc', 'admin'), 0));

$temp->add(new admin_setting_configcheckbox('passwordchangetokendeletion',
new lang_string('passwordchangetokendeletion', 'admin'),
new lang_string('passwordchangetokendeletion_desc', 'admin'), 0));

$temp->add(new admin_setting_configcheckbox('groupenrolmentkeypolicy', new lang_string('groupenrolmentkeypolicy', 'admin'), new lang_string('groupenrolmentkeypolicy_desc', 'admin'), 1));
$temp->add(new admin_setting_configcheckbox('disableuserimages', new lang_string('disableuserimages', 'admin'), new lang_string('configdisableuserimages', 'admin'), 0));
$temp->add(new admin_setting_configcheckbox('emailchangeconfirmation', new lang_string('emailchangeconfirmation', 'admin'), new lang_string('configemailchangeconfirmation', 'admin'), 1));
Expand Down
2 changes: 2 additions & 0 deletions lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,8 @@
$string['order4'] = 'Fourth';
$string['passwordchangelogout'] = 'Log out after password change';
$string['passwordchangelogout_desc'] = 'If enabled, when a password is changed, all browser sessions are terminated, apart from the one in which the new password is specified. (This setting does not affect password changes via bulk user upload.)';
$string['passwordchangetokendeletion'] = 'Remove web service access tokens after password change';
$string['passwordchangetokendeletion_desc'] = 'If enabled, when a password is changed, all the user web service access tokens are deleted.';
$string['passwordpolicy'] = 'Password policy';
$string['passwordresettime'] = 'Maximum time to validate password reset request';
$string['passwordreuselimit'] = 'Password rotation limit';
Expand Down
2 changes: 2 additions & 0 deletions lang/en/moodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,8 @@
$string['showtheselogs'] = 'Show these logs';
$string['showthishelpinlanguage'] = 'Show this help in language: {$a}';
$string['schedule'] = 'Schedule';
$string['signoutofotherservices'] = 'Sign out everywhere';
$string['signoutofotherservices_help'] = 'If ticked, the account will be signed out of all devices and systems which use web services, such as the mobile app.';
$string['since'] = 'Since';
$string['sincelast'] = 'since last login';
$string['site'] = 'Site';
Expand Down
1 change: 1 addition & 0 deletions lang/en/webservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
$string['usernameoridnousererror'] = 'No users were found with this username/user id.';
$string['usernameoridoccurenceerror'] = 'More than one user was found with this username. Please enter the user id.';
$string['usernotallowed'] = 'The user is not allowed for this service. First you need to allow this user on the {$a}\'s allowed users administration page.';
$string['userservices'] = 'User services: {$a}';
$string['usersettingssaved'] = 'User settings saved';
$string['validuntil'] = 'Valid until';
$string['validuntil_help'] = 'If set, the service will be inactivated after this date for this user.';
Expand Down
6 changes: 4 additions & 2 deletions lib/moodlelib.php
Original file line number Diff line number Diff line change
Expand Up @@ -4570,8 +4570,10 @@ function update_internal_user_password($user, $password, $fasthash = false) {
\core\event\user_password_updated::create_from_user($user)->trigger();

// Remove WS user tokens.
require_once($CFG->dirroot.'/webservice/lib.php');
webservice::delete_user_ws_tokens($user->id);
if (!empty($CFG->passwordchangetokendeletion)) {
require_once($CFG->dirroot.'/webservice/lib.php');
webservice::delete_user_ws_tokens($user->id);
}
}

return true;
Expand Down
5 changes: 5 additions & 0 deletions login/change_password.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
require_once($CFG->dirroot.'/user/lib.php');
require_once('change_password_form.php');
require_once($CFG->libdir.'/authlib.php');
require_once($CFG->dirroot.'/webservice/lib.php');

$id = optional_param('id', SITEID, PARAM_INT); // current course
$return = optional_param('return', 0, PARAM_BOOL); // redirect after password change
Expand Down Expand Up @@ -122,6 +123,10 @@
\core\session\manager::kill_user_sessions($USER->id, session_id());
}

if (!empty($data->signoutofotherservices)) {
webservice::delete_user_ws_tokens($USER->id);
}

// Reset login lockout - we want to prevent any accidental confusion here.
login_unlock_account($USER);

Expand Down
5 changes: 5 additions & 0 deletions login/change_password_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ function definition() {
$mform->addRule('newpassword2', get_string('required'), 'required', null, 'client');
$mform->setType('newpassword2', PARAM_RAW);

if (empty($CFG->passwordchangetokendeletion) and !empty(webservice::get_active_tokens($USER->id))) {
$mform->addElement('advcheckbox', 'signoutofotherservices', get_string('signoutofotherservices'));
$mform->addHelpButton('signoutofotherservices', 'signoutofotherservices');
$mform->setDefault('signoutofotherservices', 1);
}

// hidden optional params
$mform->addElement('hidden', 'id', 0);
Expand Down
4 changes: 4 additions & 0 deletions user/editadvanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require_once($CFG->dirroot.'/user/editlib.php');
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/lib.php');
require_once($CFG->dirroot.'/webservice/lib.php');

// HTTPS is required in this page when $CFG->loginhttps enabled.
$PAGE->https_required();
Expand Down Expand Up @@ -218,6 +219,9 @@
// the problem here is we do not want to logout admin here when changing own password.
\core\session\manager::kill_user_sessions($usernew->id, session_id());
}
if (!empty($usernew->signoutofotherservices)) {
webservice::delete_user_ws_tokens($usernew->id);
}
}
}

Expand Down
15 changes: 15 additions & 0 deletions user/editadvanced_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,21 @@ public function definition() {

$mform->disabledIf('newpassword', 'auth', 'in', $cannotchangepass);

// Check if the user has active external tokens.
if ($userid and empty($CFG->passwordchangetokendeletion)) {
if ($tokens = webservice::get_active_tokens($userid)) {
$services = '';
foreach ($tokens as $token) {
$services .= format_string($token->servicename) . ',';
}
$services = get_string('userservices', 'webservice', rtrim($services, ','));
$mform->addElement('advcheckbox', 'signoutofotherservices', get_string('signoutofotherservices'), $services);
$mform->addHelpButton('signoutofotherservices', 'signoutofotherservices');
$mform->disabledIf('signoutofotherservices', 'newpassword', 'eq', '');
$mform->setDefault('signoutofotherservices', 1);
}
}

$mform->addElement('advcheckbox', 'preference_auth_forcepasswordchange', get_string('forcepasswordchange'));
$mform->addHelpButton('preference_auth_forcepasswordchange', 'forcepasswordchange');
$mform->disabledIf('preference_auth_forcepasswordchange', 'createpassword', 'checked');
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

defined('MOODLE_INTERNAL') || die();

$version = 2016100700.00; // YYYYMMDD = weekly release date of this DEV branch.
$version = 2016100700.01; // YYYYMMDD = weekly release date of this DEV branch.
// RR = release increments - 00 in DEV branches.
// .XX = incremental changes.

Expand Down
15 changes: 15 additions & 0 deletions webservice/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,22 @@ public function remove_external_function_from_service($functionname, $serviceid)

}

/**
* Return a list with all the valid user tokens for the given user, it only excludes expired tokens.
*
* @param string $userid user id to retrieve tokens from
* @return array array of token entries
* @since Moodle 3.2
*/
public static function get_active_tokens($userid) {
global $DB;

$sql = 'SELECT t.*, s.name as servicename FROM {external_tokens} t JOIN
{external_services} s ON t.externalserviceid = s.id WHERE
t.userid = :userid AND (t.validuntil IS NULL OR t.validuntil > :now)';
$params = array('userid' => $userid, 'now' => time());
return $DB->get_records_sql($sql, $params);
}
}

/**
Expand Down

0 comments on commit 886207f

Please sign in to comment.