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

Security and testing fixes #21

Merged
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
37 changes: 27 additions & 10 deletions classes/external.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ class external extends external_api {
* @throws dml_exception
*/
public static function set_order(string $hash, int $sortorder) : array {
global $USER;

require_capability('block/user_favorites:edit', context_user::instance($USER->id), $USER);

// Parameter validation.
$params = self::validate_parameters(self::set_order_parameters(), array('hash' => $hash, 'sortorder' => $sortorder));

$favorites = new favorites();
$favorites->set_order($hash, $sortorder);
$favorites->set_order($params['hash'], $params['sortorder']);

return [
'result_code' => self::RESPONSE_CODE_SUCCESS,
Expand Down Expand Up @@ -115,23 +122,26 @@ public static function set_order_returns() : external_single_structure {
public static function set_url(string $hash, string $title, int $blockid, array $optional) : array {
global $USER;

require_capability('block/user_favorites:add', context_block::instance($blockid), $USER);
// Parameter validation.
$params = self::validate_parameters(self::set_url_parameters(), array('hash' => $hash, 'title' => $title, 'blockid' => $blockid, 'optional' => $optional));

require_capability('block/user_favorites:add', context_block::instance($params['blockid']), $USER);
$favorites = new favorites();
if (!empty($optional['url'])) {
if (!empty($params['optional']['url'])) {

if (!filter_var($optional['url'], FILTER_VALIDATE_URL)) {
if (!filter_var($params['optional']['url'], FILTER_VALIDATE_URL)) {
throw new moodle_exception('Incorrect url.');
}

$favorites->set_by_url($optional['url'], $title);
$favorites->set_by_url($params['optional']['url'], $params['title']);

return [
'result_code' => self::RESPONSE_CODE_SUCCESS,
];
}

// Update url title.
$favorites->set_title($hash, $title);
$favorites->set_title($params['hash'], $params['title']);

return [
'result_code' => self::RESPONSE_CODE_SUCCESS,
Expand Down Expand Up @@ -183,10 +193,13 @@ public static function set_url_returns() : external_single_structure {
public static function delete_url(string $hash, int $blockid) : array {
global $USER;

require_capability('block/user_favorites:delete', context_block::instance($blockid), $USER);
// Parameter validation.
$params = self::validate_parameters(self::delete_url_parameters(), array('hash' => $hash, 'blockid' => $blockid));

require_capability('block/user_favorites:delete', context_block::instance($params['blockid']), $USER);

$favorites = new favorites();
$favorites->delete_by_hash($hash);
$favorites->delete_by_hash($params['hash']);

return [
'result_code' => self::RESPONSE_CODE_SUCCESS,
Expand Down Expand Up @@ -230,15 +243,19 @@ public static function delete_url_returns() : external_single_structure {
*/
public static function get_content(string $url, int $blockid) : array {
global $PAGE, $USER;
$context = context_block::instance($blockid);

// Parameter validation.
$params = self::validate_parameters(self::get_content_parameters(), array('url' => $url, 'blockid' => $blockid));

$context = context_block::instance($params['blockid']);
require_capability('block/user_favorites:view', $context, $USER);

$favorites = new favorites();
$PAGE->set_context($context);
$renderer = $PAGE->get_renderer('block_user_favorites');

return [
'content' => $renderer->render_favorites(new output_favorites($favorites, $url)),
'content' => $renderer->render_favorites(new output_favorites($favorites, $params['url'])),
'result_code' => self::RESPONSE_CODE_SUCCESS,
];
}
Expand Down
13 changes: 13 additions & 0 deletions db/access.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@
],
],

'block/user_favorites:edit' => [
'riskbitmask' => RISK_SPAM | RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_BLOCK,
'archetypes' => [
'teacher' => CAP_ALLOW,
'editingteacher' => CAP_ALLOW,
'manager' => CAP_ALLOW,
'student' => CAP_ALLOW,
'user' => CAP_ALLOW,
],
],

'block/user_favorites:delete' => [
'riskbitmask' => RISK_SPAM | RISK_XSS,

Expand Down
44 changes: 22 additions & 22 deletions db/install.xml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="blocks/user_favorites/db" VERSION="20190624" COMMENT="XMLDB file for Moodle blocks/user_favorites"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="block_user_favorites" COMMENT="User favourites">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="userid" TYPE="int" LENGTH="11" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="title" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="hash" TYPE="char" LENGTH="33" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="sortorder" TYPE="int" LENGTH="11" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="created_at" TYPE="int" LENGTH="11" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="userid" UNIQUE="false" FIELDS="userid"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
<TABLES>
<TABLE NAME="block_user_favorites" COMMENT="User favourites">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="userid" TYPE="int" LENGTH="11" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="title" TYPE="char" LENGTH="100" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="hash" TYPE="char" LENGTH="33" NOTNULL="true" SEQUENCE="false"/>
<FIELD NAME="sortorder" TYPE="int" LENGTH="11" NOTNULL="false" SEQUENCE="false"/>
<FIELD NAME="created_at" TYPE="int" LENGTH="11" NOTNULL="true" SEQUENCE="false"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="userid" UNIQUE="false" FIELDS="userid"/>
</INDEXES>
</TABLE>
</TABLES>
</XMLDB>
1 change: 1 addition & 0 deletions lang/en/block_user_favorites.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
$string['user_favorites:add'] = 'Add favourite';
$string['user_favorites:delete'] = 'Delete favourite';
$string['user_favorites:view'] = 'View favorites';
$string['user_favorites:edit'] = 'Edit favorites';

// Buttons.
$string['btn:delete'] = 'Delete favourite';
Expand Down
1 change: 1 addition & 0 deletions lang/nl/block_user_favorites.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
$string['user_favorites:add'] = 'Favoriet toevoegen';
$string['user_favorites:delete'] = 'Verwijder favoriet';
$string['user_favorites:view'] = 'Bekijk favoriet';
$string['user_favorites:edit'] = 'Bewerk favoriet';

// Buttons.
$string['btn:delete'] = 'Verwijder favoriet';
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

defined('MOODLE_INTERNAL') || die;

$plugin->version = 2023050900;
$plugin->version = 2023091300;
$plugin->requires = 2017111300;
$plugin->component = 'block_user_favorites';
$plugin->release = '4.1.2';
Expand Down
Loading