Naner
Naner

Reputation: 1292

How to manually create a user in Moodle?

I am trying to create an authentication plugin, but I am having problems when the user does not exist in the moodle database. Therefore I am trying to find a way to manually create a user.

I tried:

$user            = new StdClass();
$user->username  = $ucUser;
$user->auth      = 'ucauth';
$user->firstname = "First";
$user->lastname  = "Last";
$user->id        = $DB->insert_record('user', $user);

But it didn't work... I got an insert error. What else do I need in the $user object?

Upvotes: 5

Views: 5139

Answers (4)

Sandipa Mukherjee
Sandipa Mukherjee

Reputation: 36

First, include the moodle user lib file at the top of the code snippet.

require_once($CFG->dirroot.'/user/lib.php');

Then make an object of user details in a variable $user. example

$user = new stdClass();
$user->firstname = "test";
$user->lastname = "test";
$user->email = "[email protected]";
$user->address = "abcd";

And all other fields which you would like to save. Then Call this below code. It will return the user-created ID. After that, you can use that id to pull the user details or something else.

$user->id = user_create_user($user, false, false);

Upvotes: 0

EAmez
EAmez

Reputation: 895

There are some values that needs to be setted when creating a user manually. I was facing the same situation and ended up with this solution:

global $DB;    
$user             = new StdClass();
$user->email      = strtolower('someemail'); //MOODLE requires lowercase
$user->username   = strtolower('someusername');//MOODLE requires lowercase
$user->password   = hash_internal_user_password('somepassword');
$user->lastname   = 'somelastname';
$user->firstname  = 'somename';
// These values are required. 
// Default values are stored in moodle config files but... this is easier.
$user->auth       = 'manual';
$user->confirmed  = 1;
$user->mnethostid = 1;
$user->country    = 'ES'; //Or another country
$user->lang       = 'es'; //Or another country
$user->timecreated = time();
$user->maildisplay= 0;

$user->id = $DB->insert_record('user', $user); // returns new userid

If you prefer, you can retrieve the whole user's data:

$lastid = $DB->insert_record('user', $user);
$user2 = get_complete_user_data('id', $lastid);

Upvotes: 1

gnuwings
gnuwings

Reputation: 950

User object should have the following values:

$user             = new StdClass();
$user->auth       = 'manual';
$user->confirmed  = 1;
$user->mnethostid = 1;
$user->email      = "email";
$user->username   = "username";
$user->password   = md5('password');
$user->lastname   = "lastname";
$user->firstname  = "firstname";
$user->id         = $DB->insert_record('user', $user);

Please try this.

Upvotes: 8

limoragni
limoragni

Reputation: 2776

Well, I recommend you to use authenticate_user_login($username, null) this is going to give you an empty user, wich you can complete later on in the proccess. Then you can use complete_user_login($user); and if you want to send the user to the edit page something like

if (user_not_fully_set_up($USER)) {
            $urltogo = $CFG->wwwroot.'/user/edit.php';
        }else{
            $urltogo = $CFG->wwwroot.'/';
        }

    redirect($urltogo);

I don't know exactly what are you trying to achieve. But I've made a plugin to connect with an external web service and it took me a while to figure out how to do it properly. I'm able to help you with anything you need.

Upvotes: 2

Related Questions