Merge branch 'MDL-62236-33-56' of git://github.com/FMCorz/moodle into MOODLE_33_STABLE
This commit is contained in:
@@ -140,6 +140,22 @@ $string['postrestparam'] = 'PHP code for REST (POST request)';
|
||||
$string['potusers'] = 'Not authorised users';
|
||||
$string['potusersmatching'] = 'Not authorised users matching';
|
||||
$string['print'] = 'Print all';
|
||||
$string['privacy:metadata:serviceusers'] = 'A list of users who can use a certain external services';
|
||||
$string['privacy:metadata:serviceusers:iprestriction'] = 'IP restricted to use the service';
|
||||
$string['privacy:metadata:serviceusers:timecreated'] = 'The date at which the record was created';
|
||||
$string['privacy:metadata:serviceusers:userid'] = 'The ID of the user';
|
||||
$string['privacy:metadata:serviceusers:validuntil'] = 'The date at which the authorisation ends';
|
||||
$string['privacy:metadata:tokens'] = 'A record of tokens for interacting with Moodle through web services or Mobile applications.';
|
||||
$string['privacy:metadata:tokens:creatorid'] = 'The ID of the user who created the token';
|
||||
$string['privacy:metadata:tokens:iprestriction'] = 'IP restricted to use this token';
|
||||
$string['privacy:metadata:tokens:lastaccess'] = 'The date at which the token was last used';
|
||||
$string['privacy:metadata:tokens:privatetoken'] = 'A more private token occasionally used to validate certain operations, such as SSO.';
|
||||
$string['privacy:metadata:tokens:timecreated'] = 'The date at which the token was created';
|
||||
$string['privacy:metadata:tokens:token'] = 'The user\'s token';
|
||||
$string['privacy:metadata:tokens:tokentype'] = 'The type of token';
|
||||
$string['privacy:metadata:tokens:userid'] = 'The ID of the user whose token it is';
|
||||
$string['privacy:metadata:tokens:validuntil'] = 'The date at which the token becomes invalid';
|
||||
$string['privacy:request:notexportedsecurity'] = 'Not exported for security reasons';
|
||||
$string['protocol'] = 'Protocol';
|
||||
$string['removefunction'] = 'Remove';
|
||||
$string['removefunctionconfirm'] = 'Do you really want to remove function "{$a->function}" from service "{$a->service}"?';
|
||||
|
||||
@@ -0,0 +1,296 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @package core_webservice
|
||||
* @copyright 2018 Frédéric Massart
|
||||
* @author Frédéric Massart <fred@branchup.tech>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
namespace core_webservice\privacy;
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
use context;
|
||||
use context_user;
|
||||
use core_privacy\local\metadata\collection;
|
||||
use core_privacy\local\request\approved_contextlist;
|
||||
use core_privacy\local\request\transform;
|
||||
use core_privacy\local\request\writer;
|
||||
|
||||
/**
|
||||
* Data provider class.
|
||||
*
|
||||
* @package core_webservice
|
||||
* @copyright 2018 Frédéric Massart
|
||||
* @author Frédéric Massart <fred@branchup.tech>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class provider implements
|
||||
\core_privacy\local\metadata\provider,
|
||||
\core_privacy\local\request\subsystem\provider {
|
||||
|
||||
/**
|
||||
* Returns metadata.
|
||||
*
|
||||
* @param collection $collection The initialised collection to add items to.
|
||||
* @return collection A listing of user data stored through this system.
|
||||
*/
|
||||
public static function get_metadata(collection $collection) {
|
||||
|
||||
$collection->add_database_table('external_tokens', [
|
||||
'token' => 'privacy:metadata:tokens:token',
|
||||
'privatetoken' => 'privacy:metadata:tokens:privatetoken',
|
||||
'tokentype' => 'privacy:metadata:tokens:tokentype',
|
||||
'userid' => 'privacy:metadata:tokens:userid',
|
||||
'creatorid' => 'privacy:metadata:tokens:creatorid',
|
||||
'iprestriction' => 'privacy:metadata:tokens:iprestriction',
|
||||
'validuntil' => 'privacy:metadata:tokens:validuntil',
|
||||
'timecreated' => 'privacy:metadata:tokens:timecreated',
|
||||
'lastaccess' => 'privacy:metadata:tokens:lastaccess',
|
||||
], 'privacy:metadata:tokens');
|
||||
|
||||
$collection->add_database_table('external_services_users', [
|
||||
'userid' => 'privacy:metadata:serviceusers:userid',
|
||||
'iprestriction' => 'privacy:metadata:serviceusers:iprestriction',
|
||||
'validuntil' => 'privacy:metadata:serviceusers:validuntil',
|
||||
'timecreated' => 'privacy:metadata:serviceusers:timecreated',
|
||||
], 'privacy:metadata:serviceusers');
|
||||
|
||||
return $collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of contexts that contain user information for the specified user.
|
||||
*
|
||||
* @param int $userid The user to search.
|
||||
* @return contextlist $contextlist The contextlist containing the list of contexts used in this plugin.
|
||||
*/
|
||||
public static function get_contexts_for_userid($userid) {
|
||||
$contextlist = new \core_privacy\local\request\contextlist();
|
||||
|
||||
$sql = "
|
||||
SELECT ctx.id
|
||||
FROM {external_tokens} t
|
||||
JOIN {context} ctx
|
||||
ON ctx.instanceid = t.userid
|
||||
AND ctx.contextlevel = :userlevel
|
||||
WHERE t.userid = :userid1
|
||||
OR t.creatorid = :userid2";
|
||||
$contextlist->add_from_sql($sql, ['userlevel' => CONTEXT_USER, 'userid1' => $userid, 'userid2' => $userid]);
|
||||
|
||||
$sql = "
|
||||
SELECT ctx.id
|
||||
FROM {external_services_users} su
|
||||
JOIN {context} ctx
|
||||
ON ctx.instanceid = su.userid
|
||||
AND ctx.contextlevel = :userlevel
|
||||
WHERE su.userid = :userid";
|
||||
$contextlist->add_from_sql($sql, ['userlevel' => CONTEXT_USER, 'userid' => $userid]);
|
||||
|
||||
return $contextlist;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export all user data for the specified user, in the specified contexts.
|
||||
*
|
||||
* @param approved_contextlist $contextlist The approved contexts to export information for.
|
||||
*/
|
||||
public static function export_user_data(approved_contextlist $contextlist) {
|
||||
global $DB;
|
||||
|
||||
$userid = $contextlist->get_user()->id;
|
||||
$contexts = array_reduce($contextlist->get_contexts(), function($carry, $context) use ($userid) {
|
||||
if ($context->contextlevel == CONTEXT_USER) {
|
||||
if ($context->instanceid == $userid) {
|
||||
$carry['has_mine'] = true;
|
||||
} else {
|
||||
$carry['others'][] = $context->instanceid;
|
||||
}
|
||||
}
|
||||
return $carry;
|
||||
}, [
|
||||
'has_mine' => false,
|
||||
'others' => []
|
||||
]);
|
||||
|
||||
$path = [get_string('webservices', 'core_webservice')];
|
||||
|
||||
// Exporting my stuff.
|
||||
if ($contexts['has_mine']) {
|
||||
|
||||
$data = [];
|
||||
|
||||
// Exporting my tokens.
|
||||
$sql = "
|
||||
SELECT t.*, s.name as externalservicename
|
||||
FROM {external_tokens} t
|
||||
JOIN {external_services} s
|
||||
ON s.id = t.externalserviceid
|
||||
WHERE t.userid = :userid
|
||||
ORDER BY t.id";
|
||||
$recordset = $DB->get_recordset_sql($sql, ['userid' => $userid]);
|
||||
foreach ($recordset as $record) {
|
||||
if (!isset($data['tokens'])) {
|
||||
$data['tokens'] = [];
|
||||
}
|
||||
$data['tokens'][] = static::transform_token($record);
|
||||
}
|
||||
$recordset->close();
|
||||
|
||||
// Exporting the services I have access to.
|
||||
$sql = "
|
||||
SELECT su.*, s.name as externalservicename
|
||||
FROM {external_services_users} su
|
||||
JOIN {external_services} s
|
||||
ON s.id = su.externalserviceid
|
||||
WHERE su.userid = :userid
|
||||
ORDER BY su.id";
|
||||
$recordset = $DB->get_recordset_sql($sql, ['userid' => $userid]);
|
||||
foreach ($recordset as $record) {
|
||||
if (!isset($data['services_user'])) {
|
||||
$data['services_user'] = [];
|
||||
}
|
||||
$data['services_user'][] = [
|
||||
'external_service' => $record->externalservicename,
|
||||
'ip_restriction' => $record->iprestriction,
|
||||
'valid_until' => $record->validuntil ? transform::datetime($record->validuntil) : null,
|
||||
'created_on' => transform::datetime($record->timecreated),
|
||||
];
|
||||
}
|
||||
$recordset->close();
|
||||
|
||||
if (!empty($data)) {
|
||||
writer::with_context(context_user::instance($userid))->export_data($path, (object) $data);
|
||||
};
|
||||
}
|
||||
|
||||
// Exporting the tokens I created.
|
||||
if (!empty($contexts['others'])) {
|
||||
list($insql, $inparams) = $DB->get_in_or_equal($contexts['others'], SQL_PARAMS_NAMED);
|
||||
$sql = "
|
||||
SELECT t.*, s.name as externalservicename
|
||||
FROM {external_tokens} t
|
||||
JOIN {external_services} s
|
||||
ON s.id = t.externalserviceid
|
||||
WHERE t.userid $insql
|
||||
AND t.creatorid = :userid1
|
||||
AND t.userid <> :userid2
|
||||
ORDER BY t.userid, t.id";
|
||||
$params = array_merge($inparams, ['userid1' => $userid, 'userid2' => $userid]);
|
||||
$recordset = $DB->get_recordset_sql($sql, $params);
|
||||
static::recordset_loop_and_export($recordset, 'userid', [], function($carry, $record) {
|
||||
$carry[] = static::transform_token($record);
|
||||
return $carry;
|
||||
}, function($userid, $data) use ($path) {
|
||||
writer::with_context(context_user::instance($userid))->export_related_data($path, 'created_by_you', (object) [
|
||||
'tokens' => $data
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all data for all users in the specified context.
|
||||
*
|
||||
* @param context $context The specific context to delete data for.
|
||||
*/
|
||||
public static function delete_data_for_all_users_in_context(context $context) {
|
||||
if ($context->contextlevel != CONTEXT_USER) {
|
||||
return;
|
||||
}
|
||||
static::delete_user_data($context->instanceid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete all user data for the specified user, in the specified contexts.
|
||||
*
|
||||
* @param approved_contextlist $contextlist The approved contexts and user information to delete information for.
|
||||
*/
|
||||
public static function delete_data_for_user(approved_contextlist $contextlist) {
|
||||
$userid = $contextlist->get_user()->id;
|
||||
foreach ($contextlist as $context) {
|
||||
if ($context->contextlevel == CONTEXT_USER && $context->instanceid == $userid) {
|
||||
static::delete_user_data($context->instanceid);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete user data.
|
||||
*
|
||||
* @param int $userid The user ID.
|
||||
* @return void
|
||||
*/
|
||||
protected static function delete_user_data($userid) {
|
||||
global $DB;
|
||||
$DB->delete_records('external_tokens', ['userid' => $userid]);
|
||||
$DB->delete_records('external_services_users', ['userid' => $userid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform a token entry.
|
||||
*
|
||||
* @param object $record The token record.
|
||||
* @return array
|
||||
*/
|
||||
protected static function transform_token($record) {
|
||||
$notexportedstr = get_string('privacy:request:notexportedsecurity', 'core_webservice');
|
||||
return [
|
||||
'external_service' => $record->externalservicename,
|
||||
'token' => $notexportedstr,
|
||||
'private_token' => $record->privatetoken ? $notexportedstr : null,
|
||||
'ip_restriction' => $record->iprestriction,
|
||||
'valid_until' => $record->validuntil ? transform::datetime($record->validuntil) : null,
|
||||
'created_on' => transform::datetime($record->timecreated),
|
||||
'last_access' => $record->lastaccess ? transform::datetime($record->lastaccess) : null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop and export from a recordset.
|
||||
*
|
||||
* @param \moodle_recordset $recordset The recordset.
|
||||
* @param string $splitkey The record key to determine when to export.
|
||||
* @param mixed $initial The initial data to reduce from.
|
||||
* @param callable $reducer The function to return the dataset, receives current dataset, and the current record.
|
||||
* @param callable $export The function to export the dataset, receives the last value from $splitkey and the dataset.
|
||||
* @return void
|
||||
*/
|
||||
protected static function recordset_loop_and_export(\moodle_recordset $recordset, $splitkey, $initial,
|
||||
callable $reducer, callable $export) {
|
||||
|
||||
$data = $initial;
|
||||
$lastid = null;
|
||||
|
||||
foreach ($recordset as $record) {
|
||||
if ($lastid && $record->{$splitkey} != $lastid) {
|
||||
$export($lastid, $data);
|
||||
$data = $initial;
|
||||
}
|
||||
$data = $reducer($data, $record);
|
||||
$lastid = $record->{$splitkey};
|
||||
}
|
||||
$recordset->close();
|
||||
|
||||
if (!empty($lastid)) {
|
||||
$export($lastid, $data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
/**
|
||||
* Data provider tests.
|
||||
*
|
||||
* @package core_webservice
|
||||
* @category test
|
||||
* @copyright 2018 Frédéric Massart
|
||||
* @author Frédéric Massart <fred@branchup.tech>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
global $CFG;
|
||||
|
||||
use core_privacy\tests\provider_testcase;
|
||||
use core_privacy\local\request\approved_contextlist;
|
||||
use core_privacy\local\request\transform;
|
||||
use core_privacy\local\request\writer;
|
||||
use core_webservice\privacy\provider;
|
||||
|
||||
require_once($CFG->dirroot . '/webservice/lib.php');
|
||||
|
||||
/**
|
||||
* Data provider testcase class.
|
||||
*
|
||||
* @package core_webservice
|
||||
* @category test
|
||||
* @copyright 2018 Frédéric Massart
|
||||
* @author Frédéric Massart <fred@branchup.tech>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class core_webservice_privacy_testcase extends provider_testcase {
|
||||
|
||||
public function setUp() {
|
||||
$this->resetAfterTest();
|
||||
}
|
||||
|
||||
public function test_get_contexts_for_userid() {
|
||||
$dg = $this->getDataGenerator();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
$u3 = $dg->create_user();
|
||||
$u4 = $dg->create_user();
|
||||
$u5 = $dg->create_user();
|
||||
$u1ctx = context_user::instance($u1->id);
|
||||
$u2ctx = context_user::instance($u2->id);
|
||||
$u3ctx = context_user::instance($u3->id);
|
||||
$u5ctx = context_user::instance($u5->id);
|
||||
|
||||
$s = $this->create_service();
|
||||
$this->create_token(['userid' => $u1->id]);
|
||||
$this->create_token(['userid' => $u1->id]);
|
||||
$this->create_token(['userid' => $u2->id, 'creatorid' => $u3->id]);
|
||||
$this->create_service_user(['externalserviceid' => $s->id, 'userid' => $u5->id]);
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
|
||||
$this->assertCount(1, $contextids);
|
||||
$this->assertTrue(in_array($u1ctx->id, $contextids));
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
|
||||
$this->assertCount(1, $contextids);
|
||||
$this->assertTrue(in_array($u2ctx->id, $contextids));
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u3->id)->get_contextids();
|
||||
$this->assertCount(1, $contextids);
|
||||
$this->assertTrue(in_array($u2ctx->id, $contextids));
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u4->id)->get_contextids();
|
||||
$this->assertCount(0, $contextids);
|
||||
|
||||
$contextids = provider::get_contexts_for_userid($u5->id)->get_contextids();
|
||||
$this->assertCount(1, $contextids);
|
||||
$this->assertTrue(in_array($u5ctx->id, $contextids));
|
||||
}
|
||||
|
||||
public function test_delete_data_for_user() {
|
||||
global $DB;
|
||||
|
||||
$dg = $this->getDataGenerator();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
$u1ctx = context_user::instance($u1->id);
|
||||
$u2ctx = context_user::instance($u2->id);
|
||||
|
||||
$s = $this->create_service();
|
||||
$this->create_token(['userid' => $u1->id, 'creatorid' => $u2->id]);
|
||||
$this->create_token(['userid' => $u1->id]);
|
||||
$this->create_token(['userid' => $u2->id]);
|
||||
$this->create_service_user(['externalserviceid' => $s->id, 'userid' => $u1->id]);
|
||||
$this->create_service_user(['externalserviceid' => $s->id, 'userid' => $u2->id]);
|
||||
|
||||
$this->assertEquals(2, $DB->count_records('external_tokens', ['userid' => $u1->id]));
|
||||
$this->assertEquals(1, $DB->count_records('external_tokens', ['userid' => $u2->id]));
|
||||
$this->assertTrue($DB->record_exists('external_services_users', ['userid' => $u1->id]));
|
||||
$this->assertTrue($DB->record_exists('external_services_users', ['userid' => $u2->id]));
|
||||
|
||||
// Delete in another context, nothing happens.
|
||||
provider::delete_data_for_user(new approved_contextlist($u2, 'core_webservice', [$u1ctx->id]));
|
||||
$this->assertEquals(2, $DB->count_records('external_tokens', ['userid' => $u1->id]));
|
||||
$this->assertEquals(1, $DB->count_records('external_tokens', ['userid' => $u2->id]));
|
||||
$this->assertTrue($DB->record_exists('external_services_users', ['userid' => $u1->id]));
|
||||
$this->assertTrue($DB->record_exists('external_services_users', ['userid' => $u2->id]));
|
||||
|
||||
// Delete in my context.
|
||||
provider::delete_data_for_user(new approved_contextlist($u2, 'core_webservice', [$u2ctx->id]));
|
||||
$this->assertEquals(2, $DB->count_records('external_tokens', ['userid' => $u1->id]));
|
||||
$this->assertEquals(0, $DB->count_records('external_tokens', ['userid' => $u2->id]));
|
||||
$this->assertTrue($DB->record_exists('external_services_users', ['userid' => $u1->id]));
|
||||
$this->assertFalse($DB->record_exists('external_services_users', ['userid' => $u2->id]));
|
||||
}
|
||||
|
||||
public function test_delete_data_for_all_users_in_context() {
|
||||
global $DB;
|
||||
|
||||
$dg = $this->getDataGenerator();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
$u1ctx = context_user::instance($u1->id);
|
||||
$u2ctx = context_user::instance($u2->id);
|
||||
|
||||
$s = $this->create_service();
|
||||
$this->create_token(['userid' => $u1->id, 'creatorid' => $u2->id]);
|
||||
$this->create_token(['userid' => $u1->id]);
|
||||
$this->create_token(['userid' => $u2->id]);
|
||||
$this->create_service_user(['externalserviceid' => $s->id, 'userid' => $u1->id]);
|
||||
$this->create_service_user(['externalserviceid' => $s->id, 'userid' => $u2->id]);
|
||||
|
||||
$this->assertEquals(2, $DB->count_records('external_tokens', ['userid' => $u1->id]));
|
||||
$this->assertEquals(1, $DB->count_records('external_tokens', ['userid' => $u2->id]));
|
||||
$this->assertTrue($DB->record_exists('external_services_users', ['userid' => $u1->id]));
|
||||
$this->assertTrue($DB->record_exists('external_services_users', ['userid' => $u2->id]));
|
||||
|
||||
provider::delete_data_for_all_users_in_context($u2ctx);
|
||||
$this->assertEquals(2, $DB->count_records('external_tokens', ['userid' => $u1->id]));
|
||||
$this->assertEquals(0, $DB->count_records('external_tokens', ['userid' => $u2->id]));
|
||||
$this->assertTrue($DB->record_exists('external_services_users', ['userid' => $u1->id]));
|
||||
$this->assertFalse($DB->record_exists('external_services_users', ['userid' => $u2->id]));
|
||||
|
||||
provider::delete_data_for_all_users_in_context($u1ctx);
|
||||
$this->assertEquals(0, $DB->count_records('external_tokens', ['userid' => $u1->id]));
|
||||
$this->assertEquals(0, $DB->count_records('external_tokens', ['userid' => $u2->id]));
|
||||
$this->assertFalse($DB->record_exists('external_services_users', ['userid' => $u1->id]));
|
||||
$this->assertFalse($DB->record_exists('external_services_users', ['userid' => $u2->id]));
|
||||
|
||||
}
|
||||
|
||||
public function test_export_data_for_user() {
|
||||
global $DB;
|
||||
|
||||
$dg = $this->getDataGenerator();
|
||||
$u1 = $dg->create_user();
|
||||
$u2 = $dg->create_user();
|
||||
$u1ctx = context_user::instance($u1->id);
|
||||
$u2ctx = context_user::instance($u2->id);
|
||||
|
||||
$path = [get_string('webservices', 'core_webservice')];
|
||||
$yearago = time() - YEARSECS;
|
||||
$hourago = time() - HOURSECS;
|
||||
|
||||
$s = $this->create_service(['name' => 'Party time!']);
|
||||
$this->create_token(['userid' => $u1->id, 'timecreated' => $yearago]);
|
||||
$this->create_token(['userid' => $u1->id, 'creatorid' => $u2->id, 'iprestriction' => '127.0.0.1',
|
||||
'lastaccess' => $hourago]);
|
||||
$this->create_token(['userid' => $u2->id, 'iprestriction' => '192.168.1.0/24', 'lastaccess' => $yearago,
|
||||
'externalserviceid' => $s->id]);
|
||||
$this->create_service_user(['externalserviceid' => $s->id, 'userid' => $u2->id]);
|
||||
|
||||
// User 1 exporting user 2 context does not give anything.
|
||||
writer::reset();
|
||||
provider::export_user_data(new approved_contextlist($u1, 'core_webservice', [$u2ctx->id]));
|
||||
$data = writer::with_context($u1ctx)->get_data($path);
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($u1ctx)->get_related_data($path, 'created_by_you');
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($u2ctx)->get_data($path);
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($u2ctx)->get_related_data($path, 'created_by_you');
|
||||
$this->assertEmpty($data);
|
||||
|
||||
// User 1 exporting their context.
|
||||
writer::reset();
|
||||
provider::export_user_data(new approved_contextlist($u1, 'core_webservice', [$u1ctx->id, $u2ctx->id]));
|
||||
$data = writer::with_context($u1ctx)->get_data($path);
|
||||
$this->assertFalse(isset($data->services_user));
|
||||
$this->assertCount(2, $data->tokens);
|
||||
$this->assertEquals(transform::datetime($yearago), $data->tokens[0]['created_on']);
|
||||
$this->assertEquals(null, $data->tokens[0]['ip_restriction']);
|
||||
$this->assertEquals(transform::datetime($hourago), $data->tokens[1]['last_access']);
|
||||
$this->assertEquals('127.0.0.1', $data->tokens[1]['ip_restriction']);
|
||||
$data = writer::with_context($u1ctx)->get_related_data($path, 'created_by_you');
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($u2ctx)->get_data($path);
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($u2ctx)->get_related_data($path, 'created_by_you');
|
||||
$this->assertEmpty($data);
|
||||
|
||||
// User 2 exporting their context.
|
||||
writer::reset();
|
||||
provider::export_user_data(new approved_contextlist($u2, 'core_webservice', [$u1ctx->id, $u2ctx->id]));
|
||||
$data = writer::with_context($u2ctx)->get_data($path);
|
||||
$this->assertCount(1, $data->tokens);
|
||||
$this->assertEquals('Party time!', $data->tokens[0]['external_service']);
|
||||
$this->assertEquals(transform::datetime($yearago), $data->tokens[0]['last_access']);
|
||||
$this->assertEquals('192.168.1.0/24', $data->tokens[0]['ip_restriction']);
|
||||
$this->assertCount(1, $data->services_user);
|
||||
$this->assertEquals('Party time!', $data->services_user[0]['external_service']);
|
||||
$data = writer::with_context($u1ctx)->get_related_data($path, 'created_by_you');
|
||||
$this->assertCount(1, $data->tokens);
|
||||
$this->assertEquals(transform::datetime($hourago), $data->tokens[0]['last_access']);
|
||||
$this->assertEquals('127.0.0.1', $data->tokens[0]['ip_restriction']);
|
||||
$data = writer::with_context($u1ctx)->get_data($path);
|
||||
$this->assertEmpty($data);
|
||||
$data = writer::with_context($u2ctx)->get_related_data($path, 'created_by_you');
|
||||
$this->assertEmpty($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a service.
|
||||
*
|
||||
* @param array $params The params.
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function create_service(array $params = []) {
|
||||
global $DB;
|
||||
static $i = 0;
|
||||
$record = (object) array_merge([
|
||||
'name' => 'Some service',
|
||||
'enabled' => '1',
|
||||
'requiredcapability' => '',
|
||||
'restrictedusers' => '0',
|
||||
'component' => 'core_webservice',
|
||||
'timecreated' => time(),
|
||||
'timemodified' => time(),
|
||||
'shortname' => 'service' . $i,
|
||||
'downloadfiles' => '1',
|
||||
'uploadfiles' => '1',
|
||||
], $params);
|
||||
$record->id = $DB->insert_record('external_services', $record);
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a service user.
|
||||
*
|
||||
* @param array $params The params.
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function create_service_user(array $params) {
|
||||
global $DB, $USER;
|
||||
static $i = 0;
|
||||
$record = (object) array_merge([
|
||||
'externalserviceid' => null,
|
||||
'userid' => $USER->id,
|
||||
'validuntil' => time() + YEARSECS,
|
||||
'iprestriction' => '',
|
||||
'timecreated' => time(),
|
||||
], $params);
|
||||
$record->id = $DB->insert_record('external_services_users', $record);
|
||||
return $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a token.
|
||||
*
|
||||
* @param array $params The params.
|
||||
* @return stdClass
|
||||
*/
|
||||
protected function create_token(array $params) {
|
||||
global $DB, $USER;
|
||||
$service = $DB->get_record('external_services', ['shortname' => MOODLE_OFFICIAL_MOBILE_SERVICE]);
|
||||
$record = (object) array_merge([
|
||||
'token' => random_string(64),
|
||||
'privatetoken' => random_string(64),
|
||||
'tokentype' => EXTERNAL_TOKEN_PERMANENT,
|
||||
'contextid' => SYSCONTEXTID,
|
||||
'externalserviceid' => $service->id,
|
||||
'userid' => $USER->id,
|
||||
'validuntil' => time() + YEARSECS,
|
||||
'iprestriction' => null,
|
||||
'sid' => null,
|
||||
'timecreated' => time(),
|
||||
'lastaccess' => time(),
|
||||
'creatorid' => $USER->id,
|
||||
], $params);
|
||||
$record->id = $DB->insert_record('external_tokens', $record);
|
||||
return $record;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user