Skip to content

Latest commit

 

History

History
94 lines (57 loc) · 1.9 KB

File metadata and controls

94 lines (57 loc) · 1.9 KB

Moodle CLI Admin Promotion

A simple CLI utility for promoting an existing Moodle user to a Site Administrator.

Requirements

  • Shell access to the Moodle server
  • PHP CLI
  • A working Moodle installation
  • Permission to execute PHP scripts from the command line

Installation

Copy 'makeadmin.php' into your Moodle installation's 'admin/cli' directory.

moodle/
├── admin/
│   └── cli/
│       └── makeadmin.php
├── config.php
└── ...

Ensure the script includes the correct path to Moodle's 'config.php': php require(DIR . '/../../config.php');

Usage

Run the script from the Moodle root:

bash
php admin/cli/makeadmin.php <username>

Or from within the 'admin/cli' directory:

bash
php makeadmin.php <username>

Example

bash
php admin/cli/makeadmin.php jsmith

Output:

bash
User 'jsmith' (ID 42) is now a site administrator.

If the user is already an administrator:

bash
User 'jsmith' is already a site administrator.

If the user cannot be found:

bash
Error: User 'jsmith' not found.

What It Does

The script:

  1. Loads the Moodle environment.
  2. Looks up the specified username.
  3. Reads the current 'siteadmins' configuration.
  4. Adds the user's ID if it is not already present.
  5. Saves the updated administrator list.

No existing administrators are removed.

Exit Codes

Code Meaning
0 Success
1 Incorrect usage (missing username)
2 User not found

Notes

  • The user must already exist in Moodle.
  • The script only grants the Site Administrator role.
  • Running the script multiple times for the same user is safe; duplicate administrator entries are not created.

Disclaimer

This script modifies Moodle's global administrator configuration. Use it only if you have appropriate administrative access to the Moodle server.