How to get administrator rights back

I have seen, that some people had similar problem with roles system in Moodle 1.8. They were making some changes in roles settings and suddenly they lost their's administrators rights when they log in next time.

It did not happened to me, yet ;), but it is better to be prepared! I found really helpful little script made by one of the core moodle developers Petr Skoda (skodak) here
This script add administrators right back to user with login name 'admin'

Step by step approach:
1. Open your favourite text editor
2. Copy there this script


require 'config.php';

$systemcontext = get_context_instance(CONTEXT_SYSTEM);
$adminroleid = create_role('new admin6', 'admin6', 'delete me later', ' moodle/legacy:admin');
$user = get_record('user', 'username', 'admin');
role_assign($adminroleid, $user->id, 0, $systemcontext->id);
echo 'done';

3. Save it as createadmin.php
4.Upload this file to your moodle server to moodle directory, where the config.php is located
5.Set proper rights for this file to be accessible
6.Access this file with your browser with calling url http://yourmoodle.com/createadmin.php

What does this script do? In the first place, it loads all needed settings from config.php and includes files with core functions.

Then you set you want to modify whole Site context.

Thsn you are creating new temporary role ( with line beginning with $adminroleid = create_rol ), that you can delete after you regain your administrator's rights.

You can add administrators rights to every user on your moodle you want. Just paste in line
$user = get_record('user', 'username', 'admin'); wanted username instead of 'admin'.

Last line will add admin role to your 'admin' user.

This script is working in Moodle 1.8 and higher.

Do you not forget!
Any changes made in roles system, like allowing new capabilities for specified role etc. take effect after affected user (with modified role) logout/login again!

To delete higher mentioned file from your server when you don't need it.

No comments: