How to add a site admin via the command line

How to add a site admin via the command line

by Doulos Xavier -
Number of replies: 11

How to add a site admin via the command line, I am able to see the user ID's by following the process

  1. php cfg.php --name=siteadmins  which gives me the ID

The running the following command 

SELECT id, username, firstname, lastname, email
    -> FROM mdl_user
    -> WHERE id IN (38,5,418,421);

I can see the users, how do I add another site admin via the command line?

Average of ratings: -
In reply to Doulos Xavier

Re: How to add a site admin via the command line

by Doulos Xavier -
I reset one of the user's name but when I login I don't see I am part of the 'Site Administrator' it is not showing the menus that I see in different LMS
In reply to Doulos Xavier

Re: How to add a site admin via the command line

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Have you thought about using Moosh (https://moosh-online.com/) if you want to do this kind of thing.

I'm very reluctant to suggest manipulating the database directly. Firstly, because it is plain dangerous. Secondly, because so much stuff is cached in Moodle - you can change the DB and nothing happens because the related data is in the cache.
Average of ratings: Useful (1)
In reply to Howard Miller

Re: How to add a site admin via the command line

by Doulos Xavier -
Hi Howard, I managed to gain the Site Admin access, by adding this line '$CFG->siteadmins = '4539' in the config.php, now it is showing my name in the Site Administrators. Where '4539' is my id. But now I am unable to add back the other users.  Any pointers?
Average of ratings: Useful (1)
In reply to Doulos Xavier

Re: How to add a site admin via the command line

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
The problem is that the config setting is a comma separated list. So you are overriding the setting through the admin interface and effectively saying "admin is just 4359".

At a pinch, you could simply add the additional IDs in config.php, e.g....

$CFG->siteadmins='4359,1234,7890';

Change/add the numbers to the user ids required. However, I'm going to suggest that what you did to solve whatever problem you had in the first place may not have been the best solution.
Average of ratings: Useful (2)
In reply to Howard Miller

Re: How to add a site admin via the command line

by Ken Task -
Picture of Particularly helpful Moodlers

@Howard ...

"I'm going to suggest that what you did to solve whatever problem you had in the first place may not have been the best solution."

+20 to that!

As is normal for the OP who posted, details are forever not forth coming ... 

User ID's 38,5,418,421 doesn't include user id 2, which, as we know, was the original id of the user that installed the site.

So am wondering ... did OP delete the row in mdl_user table for ID 2?

Must be the OP's command line only server which has yet to be migrated.   OP did mention 'other server' ... hmmmm, wonder if that's the one installed with Softac and a course was restored that had his account in the course as whatever ... expected his account to be made admin level.   More hmmmmmmmm.

@OP ... did you tell the owner of the site that you were doing this?   If not, that's worse than doing a non-evasive port scan!!!!   Don't answer that for that would be 'exposing' yourself. :|

Yep, learning to admin a moodle as well as migrate a site is a rather tall mountain ... 

Average of ratings: Useful (1)
In reply to Ken Task

Re: How to add a site admin via the command line

by Doulos Xavier -

Hi Ken, one of the site admins removed my name without proper approval. So I had to take control of the site. 

No DB changes were made. All the users who were earlier part of  Site Admin are still there. I need to bring back to the old state without (ability to add Site Admins) any pointers is appreciated.

In reply to Doulos Xavier

Re: How to add a site admin via the command line

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Ask the person who deleted you to put you back?
In reply to Howard Miller

Re: How to add a site admin via the command line

by Doulos Xavier -

The person who removed me when I login with his credentials I don't see him as Site Administrator

In reply to Doulos Xavier

Re: How to add a site admin via the command line

by Howard Miller -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
As previously explained - your 'emergency' setting in config.php will lock out everybody else. So, assuming nothing else changed, removing that should restore all the other admins and one of them can reinstate you.

The "right" way to deal with this sort of thing is via the "primary" site administrator. This is the administrator you set up when the site is created, has id=2, and (if you didn't change it) username 'admin'. Personally, I would always recommend keeping that user as a separate admin not connected to any real user. Keep it under lock and key for emergencies (such as this). If you loose the password, there is a command line tool to recover it.
In reply to Howard Miller

Re: How to add a site admin via the command line

by Doulos Xavier -
Thanks, I am able to add the other users based on their id's but how can I recover the old functionality
In reply to Doulos Xavier

Re: How to add a site admin via the command line

by Doulos Xavier -
This is fixed, after I added the ID in the config.php file, I logged into Moodle, then I commented out the line where it says $CFG->siteadmins = '4539'. Logged back in, I can see the user as Site Admin.