Merge branch 'matrix-user-prefix' of https://github.com/geichelberger/moodle

This commit is contained in:
Huong Nguyen
2025-08-20 08:44:41 +07:00
5 changed files with 52 additions and 2 deletions
@@ -0,0 +1,38 @@
<?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/>.
/**
* Admin setting for communication_matrix that validates the user prefix that Matrix allow for.
*
* @package communication_matrix
* @copyright 2025 David Woloszyn <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class communication_matrix_admin_setting_user_prefix_configtext extends admin_setting_configtext {
#[\Override]
public function validate($data) {
// Must contain at least one non-number.
// Must not start with an underscore (_).
// Must only contain a-z, 0-9, ., _, =, -, /.
// Reference https://spec.matrix.org/latest/appendices/#user-identifiers.
if (!preg_match('/^(?![0-9]+$)(?!_)[a-z0-9._=\/\-]+$/', $data)) {
return get_string('matrixinvalidcharacter', 'communication_matrix');
}
return parent::validate($data);
}
}
@@ -64,7 +64,9 @@ class matrix_user_manager {
// Matrix/Synapse servers will not allow numeric usernames.
if (is_numeric($username)) {
$username = self::MATRIX_USER_PREFIX . $username;
$configprefix = get_config('communication_matrix', 'matrixuserprefix');
$prefix = !empty($configprefix) ? $configprefix : self::MATRIX_USER_PREFIX;
$username = $prefix . $username;
}
$homeserver = get_config('communication_matrix', 'matrixhomeservername');
@@ -27,12 +27,15 @@ $string['matrixhomeservername'] = 'Homeserver name';
$string['matrixhomeservername_desc'] = 'The part after <code>@user:</code> in your Matrix ID (e.g. <code>example.com</code> in <code>@user:example.com</code>)';
$string['matrixhomeserverurl'] = 'Homeserver URL';
$string['matrixhomeserverurl_desc'] = 'Server URL for connecting and creating accounts e.g. https://matrix.example.com.';
$string['matrixinvalidcharacter'] = 'Invalid character';
$string['matrixaccesstoken'] = 'Access token';
$string['matrixaccesstoken_desc'] = 'Access token for the account which will perform actions on the homeserver.';
$string['matrixelementurl'] = 'Element web URL';
$string['matrixroomtopic'] = 'Room topic';
$string['matrixroomtopic_help'] = 'A short description of what this room is for.';
$string['matrixuserid'] = 'Matrix user ID';
$string['matrixuserprefix'] = 'User prefix';
$string['matrixuserprefix_desc'] = 'Prefix for numeric usernames (e.g., \'12345\' becomes \'user12345\'). See <a href="https://spec.matrix.org/latest/appendices/#user-identifiers" target="_blank">Matrix\'s documentation</a> for allowed characters.';
$string['matrix:moderator'] = 'Matrix moderator';
$string['pluginname'] = 'Matrix';
$string['privacy:metadata'] = 'The Matrix communication plugin does not store any personal data.';
@@ -43,4 +43,11 @@ if ($hassiteconfig) {
// Element web URL.
$name = new lang_string('matrixelementurl', 'communication_matrix');
$settings->add(new admin_setting_configtext('communication_matrix/matrixelementurl', $name, '', ''));
// User prefix for numeric usernames.
$name = new lang_string('matrixuserprefix', 'communication_matrix');
$desc = new lang_string('matrixuserprefix_desc', 'communication_matrix');
$default = \communication_matrix\matrix_user_manager::MATRIX_USER_PREFIX;
$settings->add(new communication_matrix_admin_setting_user_prefix_configtext('communication_matrix/matrixuserprefix',
$name, $desc, $default, PARAM_RAW_TRIMMED));
}
@@ -147,7 +147,7 @@ final class matrix_user_manager_test extends \advanced_testcase {
null,
'https://matrix.example.org',
'123456',
'@' . matrix_user_manager::MATRIX_USER_PREFIX . '123456:matrix.example.org',
'@user123456:matrix.example.org',
],
];
}