Trying to recover a site - lost admin account

Trying to recover a site - lost admin account

by Steven Ouellette -
Number of replies: 14
Hello all,

I am trying to help a colleague rebuild a site. We have some backups from 6/29/09, both the moodledata and the MySQL data that are from a different URL. (The online site was deleted by the host during a "transfer of power" gap.)

So here are the steps I followed:

  1. Installed a blank Moodle instance
  2. Dropped all the tables, then imported the MySQL backup file
  3. Uploaded and decompressed the moodledata file over the blank one
It looks like the information is all there. The problem is that we can't get into an admin account.

I tried the "reset password" script, which reports that "admin" is not a valid account. Neither is "administrator."

I also tried (like a monkey who doesn't know coding):
<?php
require 'config.php';

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

But I think that doesn't work if there is no "admin" account in the first place.

I can't create a new account to elevate to admin, since I get reCAPTCHA Input Error: Invalid Referrer (which I think I know how to fix....once I have an admin account!!!!)

Is there a way to create an admin account manually in the database using MySQLAdmin? Or even to figure out the username of an account that has admin access, in order to reset its password? (I don't know where the usernames are actually stored.) Or turn off reCAPTCHA in the database or where ever so that I can create a new account to elevate?

If you could make it simple enough for a monkey to follow, I would appreciate it. Computers I know, but relational databases make my head hurt.

Ook, ook!

Thanks in advance!!!
Average of ratings: -
In reply to Steven Ouellette

Re: Trying to recover a site - lost admin account

by Colin Fraser -
Picture of Documentation writers Picture of Testers

I have no way of testing this, I am not at my own computer, so it is pretty much from memory, but even so, it should be close enough to get you going again.

Download and install phpMyAdmin, it is easier than trying to do it command line.

Run phpMyAdmin and go to the Moodle database, look for the mdl_user table. Look for the select statement, under the SQL tab and add into the line

SELECT * from mdl_user and delete everything else in the Select statement. When the data displays,m look for a known role, a person you know has admin rights and you know their password. If you cannot find one, then go to the Guest Role. In the table you will find the fields roleid and contextid. Change those values to 1 and 1. Click the edit icon at the start of the row, and then edit the fields. Save and exit. This SHOULD get you going again and will allow you to log in as Guest and using the guest password or if you want to delete the password altogether, you may do so. Try to log in as Guest and this should give you admin rights.

If you are getting the same error, then there is another table, and the name escapes me at the moment, which defines the roles and you may have to hunt for it. If that table is not there, then you are dead in the water, no amount of tweaking your database is going to fix that.

Sorry, but  that is all I can give you at the moment. I will see if I can get in later to offer more detail, but it might not be for a day or more now... Good luck.  

 

In reply to Colin Fraser

Re: Trying to recover a site - lost admin account

by Colin Fraser -
Picture of Documentation writers Picture of Testers
See Memory is not always accurate, I was going to wait for a while but I did not know what time I would be able to access a Moodle database to check my thinking.

What you can get from the mdl_usr table is the userid numbers of the roles you may want to try and enter the Moodle with.

WRONG, WRONG WRONG..black eye it is not the mdl_user table you need to change the roleid and contextid.. try the mdl_role_assignments table..Sorry.. blush and it is there you can add in the needed userid.

Use the guest role ID number as the userID. All three fields need to be cojoined as roleid = 1 being the administrator's role, contextid = 1 for the context of the role, that is everywhere.. and userid = 1 or whatever your guest user id number is.

Look for the mdl_role table and that will tell you what the roles and their ids are, 1 being an admin and 7 being an authenticated user.

If some tables of your Moodle are corrupted, then create a new database and using phpMyAdmin to transfer tables, replace those that are not working. As long as the data they contain are not critical, then it should be OK.


In reply to Colin Fraser

Re: Trying to recover a site - lost admin account

by Steven Ouellette -
Hi Colin, and thanks!

OK, I see the results of the query and I can see that there are two admins, but I can't see the usernames there, only the IDs. If I can get the usernames, then I can reset the password for them, I think.

I tried, monkey-like, to edit the password change script to select a user by ID, but it still gives me invalid user:

<?php
include('config.php');


////////////////////////////////////////////////////////////////////

$username = 'admin';
$newpassword = 'XXXXXXX';

////////////////////////////////////////////////////////////////////




if ($userobj = get_record_select('user', 'username', "id=1", 'password')) {
$userobj->password = md5($newpassword);
$userobj->username = $username;
update_record('user', $userobj);
echo 'Password changed';
} else {
echo 'Invalid username';
}

?>


I figure I made some dumb syntax error since the last computer language I learned for real was FORTRAN....

I also can't log into the guest account - it says invalid login.

I may have to rethink my "someone who doesn't know how a TV works should not be allowed to watch one" schtick, since I clearly only have a limited idea of what is going on behind the scenes in Moodle...

Any more help would be greatly appreciated!!!
In reply to Steven Ouellette

Re: Trying to recover a site - lost admin account

by Jon Witts -
Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
Search the mdl_user table for the matching id and that row of the table will hold the user name...
In reply to Jon Witts

Re: Trying to recover a site - lost admin account

by Colin Fraser -
Picture of Documentation writers Picture of Testers
Alternatively there is always the sledgehammer approach..have you tried  the adminfix.php script?
In reply to Colin Fraser

Re: Trying to recover a site - lost admin account

by Erik Ringmar -
Hi there,

I tried using the sledgehammer script, but I get an error:

get_record() not available anymore

Could it be that that command isn't available in 2.02? Does anyone know of a work-around?

yours always,

Erik
In reply to Erik Ringmar

Re: Trying to recover a site - lost admin account

by Colin Fraser -
Picture of Documentation writers Picture of Testers

Hi Lin, the adminfix.php script has, AFAIK, not yet been updated for Moodle 2.0.x, I am not sure the original author, The Purple Blob, is still around and instrested in maintaining the script. Alternatively, I have not heard anyone volunteering to update it for use in Moodle 2.x - but then, all the experts are busy elsewhere, so I suspect that this is pretty low on the priority list right now.

As an alternate, have you installed phpMyAdmin? If you have not, then I suggest you do so. If you have then open the Moodle 2.x.x database and look for the table mdl_user. If you have allowed the Guest role, then the password is "guest". Copy the password there, and then find an Admin User and past that key to the  Admin User's password field. Save it, exit and you should be able to enter the Moodle with the password "guest". The condition here is you may have had to have also changed your password policy. If not, then find a user who's password you know, and use that one. Remember after this to change that password and make sure no-one knows about the phpMyAdmin. (This technique has actually worked for me, although the host at the time vented on my employer, who spewed a little on me, but I did the job so who cares what they said, they only paid me, I was running the show!)  Good luck.

In reply to Jon Witts

Re: Trying to recover a site - lost admin account

by Steven Ouellette -
Ahhhhhhhha! The last table I have is mdl_stats_daily. I think I am missing the rest of the tables.

That would make a difference, I bet...tongueout

Let me see if the MySQL backup has more in it. It may have been disconnected.
In reply to Steven Ouellette

Re: Trying to recover a site - lost admin account

by Steven Ouellette -
That was the problem - I uploaded the backup MySQL file in plain text, and I think the upload timed out before it was done.

I uploaded it in zipped format and it had no problem - all the tables were there, and the admin user was showing up again. I easily reset the password using the script, and we are totally recovered.

Thanks all!!
In reply to Steven Ouellette

Please help! I lost my Admin rights - Moodle 2

by Ronel Bornman -

Hi there,

I went into permission for blogs and add managers to Edit a blog.  When I return to the home page I realised that my admin rights were gone.

Please can you assist?  How do I retrive that?

In reply to Ronel Bornman

Re: Please help! I lost my Admin rights - Moodle 2

by Bill Moede -

I have had that happen with 1.9.  The original admin login should still work.  Log in under that name and reset your admin rights.

 

In reply to Bill Moede

Re: Please help! I lost my Admin rights - Moodle 2

by Ronel Bornman -

Hi Bill,

My technical person advised me that he deleted the original admin use from the database and added me as the administrator!!  Not sure why????  Hence the original admin user does not exist!!!!

Is there a way to add an administrator to the user table on the data base or where do we find the user roles table - can you assign roles from the back-end?

I activated back-ups on Moodle a week ago, can those back-up be restored by any one or can it only be the Administrator?

Many thanks

In reply to Steven Ouellette

Solution: 2.x mysql import on new server, passwords don't work

by Chris Collman -
Picture of Documentation writers

Hi all,

Do not forget to check $CFG->passwordsaltmain in moodle/config.php  if you are trying to "restore" stuff on a new clean Moodle using a mysql import of data.   When you create a new Moodle, the passwordsaltmain will be "unique".   Copy the old variable to replace the new one and you will be all set.

I had crashed a localhost 2.1.2+ moodle.  did a phpmysql export on the old.   Got all the old into the clean install on a new localhost.  But my passwords would not work for any user, including guest!    I was not happy and then remebered the line in the config.php file.   Sweet. 

There were a few other things I need to work through.  But at least I can log in as admin and as a super user.  

Chris