From 31f267963a74582c87f1119c7b412dbbce2c2460 Mon Sep 17 00:00:00 2001 From: skodak Date: Mon, 18 Sep 2006 17:40:22 +0000 Subject: [PATCH] MDL-6568 shortname added to roles --- admin/roles/manage.html | 2 ++ admin/roles/manage.php | 10 ++++++---- lib/accesslib.php | 24 +++++++++++++++--------- lib/db/install.xml | 7 ++++--- lib/db/mysql.php | 24 ++++++++++++++++++++++++ lib/db/mysql.sql | 1 + lib/db/postgres7.sql | 1 + lib/moodlelib.php | 8 ++++++++ version.php | 2 +- 9 files changed, 62 insertions(+), 17 deletions(-) diff --git a/admin/roles/manage.html b/admin/roles/manage.html index cb6d3cc92b..41efee245e 100755 --- a/admin/roles/manage.html +++ b/admin/roles/manage.html @@ -3,6 +3,8 @@
Role Name: +  +Role short name (ASCII):
Role Description: htmleditor, 10, 50, 50, 10, 'description', "$role->description"); ?> diff --git a/admin/roles/manage.php b/admin/roles/manage.php index 8d6e1132b9..6679ee6f0b 100755 --- a/admin/roles/manage.php +++ b/admin/roles/manage.php @@ -7,10 +7,11 @@ admin_externalpage_setup('defineroles', $adminroot); - $roleid = optional_param('roleid', 0, PARAM_INT); // if set, we are editing a role + $roleid = optional_param('roleid', 0, PARAM_INT); // if set, we are editing a role + $name = optional_param('name', '', PARAM_MULTILANG); // new role name + $shortname = optional_param('shortname', '', PARAM_SAFEDIR); // new role shortname + $description = optional_param('description', '', PARAM_MULTILANG); // new role desc $action = optional_param('action', '', PARAM_ALPHA); - $name = optional_param('name', '', PARAM_ALPHA); // new role name - $description = optional_param('description', '', PARAM_NOTAGS); // new role desc $confirm = optional_param('confirm', 0, PARAM_BOOL); $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID); @@ -37,7 +38,7 @@ switch ($action) { case 'add': - $newrole = create_role($name, $description); + $newrole = create_role($name, $shortname, $description); $ignore = array('roleid', 'sesskey', 'action', 'name', 'description', 'contextid'); @@ -142,6 +143,7 @@ } else { $action='add'; $role->name=''; + $role->shortname=''; $role->description=''; } diff --git a/lib/accesslib.php b/lib/accesslib.php index d7de7c5449..2860e87266 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -887,15 +887,15 @@ function moodle_install_roles() { // Create default/legacy roles and capabilities. // (1 legacy capability per legacy role at system level). - $adminrole = create_role(get_string('administrator'), get_string('administratordescription'), 'moodle/legacy:admin'); + $adminrole = create_role(get_string('administrator'), 'admin', get_string('administratordescription'), 'moodle/legacy:admin'); if (!assign_capability('moodle/site:doanything', CAP_ALLOW, $adminrole, $systemcontext->id)) { error('Could not assign moodle/site:doanything to the admin role'); } - $coursecreatorrole = create_role(get_string('coursecreators'), get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator'); - $noneditteacherrole = create_role(get_string('noneditingteacher'), get_string('noneditingteacherdescription'), 'moodle/legacy:teacher'); - $editteacherrole = create_role(get_string('defaultcourseteacher'), get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher'); - $studentrole = create_role(get_string('defaultcoursestudent'), get_string('defaultcoursestudentdescription'), 'moodle/legacy:student'); - $guestrole = create_role(get_string('guest'), get_string('guestdescription'), 'moodle/legacy:guest'); + $coursecreatorrole = create_role(get_string('coursecreators'), 'coursecreator', get_string('coursecreatorsdescription'), 'moodle/legacy:coursecreator'); + $editteacherrole = create_role(get_string('defaultcourseteacher'), 'editingteacher', get_string('defaultcourseteacherdescription'), 'moodle/legacy:editingteacher'); + $noneditteacherrole = create_role(get_string('noneditingteacher'), 'teacher', get_string('noneditingteacherdescription'), 'moodle/legacy:teacher'); + $studentrole = create_role(get_string('defaultcoursestudent'), 'student', get_string('defaultcoursestudentdescription'), 'moodle/legacy:student'); + $guestrole = create_role(get_string('guest'), 'guest', get_string('guestdescription'), 'moodle/legacy:guest'); // Look inside user_admin, user_creator, user_teachers, user_students and @@ -1168,11 +1168,12 @@ function get_local_override($roleid, $contextid, $capability) { /** * function that creates a role * @param name - role name + * @param shortname - role short name * @param description - role description * @param legacy - optional legacy capability * @return id or false */ -function create_role($name, $description, $legacy='') { +function create_role($name, $shortname, $description, $legacy='') { // check for duplicate role name @@ -1180,7 +1181,12 @@ function create_role($name, $description, $legacy='') { error('there is already a role with this name!'); } + if ($role = get_record('role','shortname', $shortname)) { + error('there is already a role with this shortname!'); + } + $role->name = $name; + $role->shortname = $shortname; $role->description = $description; $context = get_context_instance(CONTEXT_SYSTEM, SITEID); @@ -2207,7 +2213,7 @@ function get_roles_used_in_context($context) { global $CFG; - return get_records_sql('SELECT distinct r.id, r.name + return get_records_sql('SELECT distinct r.id, r.name, r.shortname FROM '.$CFG->prefix.'role_assignments ra, '.$CFG->prefix.'role r WHERE r.id = ra.roleid @@ -2313,7 +2319,7 @@ function get_user_roles($context, $userid=0, $checkparentcontexts=true) { $contexts = ' ra.contextid = \''.$context->id.'\''; } - return get_records_sql('SELECT ra.*, r.name + return get_records_sql('SELECT ra.*, r.name, r.shortname FROM '.$CFG->prefix.'role_assignments ra, '.$CFG->prefix.'role r, '.$CFG->prefix.'context c diff --git a/lib/db/install.xml b/lib/db/install.xml index 9272e5efea..a4885dc4dd 100644 --- a/lib/db/install.xml +++ b/lib/db/install.xml @@ -1,5 +1,5 @@ - @@ -915,8 +915,9 @@ - - + + + diff --git a/lib/db/mysql.php b/lib/db/mysql.php index e633749037..91972cc13f 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -2224,6 +2224,30 @@ function main_upgrade($oldversion=0) { delete_records('config', 'name', 'requestedstudentsname'); } + if ($oldversion < 2006091804) { + $roles = get_records('role'); + $first = array_shift($roles); + if (!empty($first->shortname)) { + // shortnames already exist + } else { + table_column('role', '', 'shortname', 'varchar', '100', '', '', 'not null', 'name'); + $legacy_names = array('admin', 'coursecreator', 'editingteacher', 'teacher', 'student', 'guest'); + foreach ($legacy_names as $name) { + if ($roles = get_roles_with_capability('moodle/legacy:'.$name, CAP_ALLOW)) { + $i = ''; + foreach ($roles as $role) { + if (empty($role->shortname)) { + $updated = new object(); + $updated->id = $role->id; + $updated->shortname = $name.$i; + update_record('role', $updated); + $i++; + } + } + } + } + } + } return $result; } diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index 57cfd7ba5e..ba379b8690 100644 --- a/lib/db/mysql.sql +++ b/lib/db/mysql.sql @@ -910,6 +910,7 @@ CREATE TABLE prefix_blog_tag_instance ( CREATE TABLE prefix_role ( `id` int(10) unsigned NOT NULL auto_increment, `name` varchar(255) NOT NULL default '', + `shortname` varchar(100) NOT NULL default '', `description` text NOT NULL default '', `sortorder` int(10) unsigned NOT NULL default '0', KEY `sortorder` (`sortorder`), diff --git a/lib/db/postgres7.sql b/lib/db/postgres7.sql index 97fa7686ad..f4dd5694ae 100644 --- a/lib/db/postgres7.sql +++ b/lib/db/postgres7.sql @@ -681,6 +681,7 @@ CREATE INDEX prefix_bti_tagid_idx ON prefix_blog_tag_instance (tagid); CREATE TABLE prefix_role ( id SERIAL PRIMARY KEY, name varchar(255) NOT NULL default '', + shortname varchar(100) NOT NULL default '', description text NOT NULL default '', sortorder integer NOT NULL default '0' ); diff --git a/lib/moodlelib.php b/lib/moodlelib.php index db5dc75198..89d298578d 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -137,6 +137,11 @@ define('PARAM_FORMAT', 0x0004); */ define('PARAM_NOTAGS', 0x0008); + /** + * PARAM_MULTILANG - general plain text compatible with multilang filter, no other html tags. + */ +define('PARAM_MULTILANG', 0x0009); + /** * PARAM_FILE - safe file name, all dangerous chars are stripped, protects against XSS, SQL injections and directory traversals */ @@ -368,6 +373,9 @@ function clean_param($param, $type) { case PARAM_NOTAGS: // Strip all tags return strip_tags($param); + case PARAM_MULTILANG: // leave only tags needed for multilang + return clean_param(strip_tags($param, ''), PARAM_CLEAN); + case PARAM_SAFEDIR: // Remove everything not a-zA-Z0-9_- return eregi_replace('[^a-zA-Z0-9_-]', '', $param); diff --git a/version.php b/version.php index 66f810ec73..4a70d7c39a 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2006091800; // YYYYMMDD = date + $version = 2006091804; // YYYYMMDD = date // XY = increments within a single day $release = '1.7 dev'; // Human-friendly version name -- 2.39.5