Hoping to get help adding a user to a group programatically (via API)

Permalink
Hi,

I am really hoping that I can get some help to understand why the following code correctly registers a new C5 user that can login as expected, but, does not also place them in the group.

public function registerUserAndAddToGroup($email, $password, $username, $group) {

// register new user and assign to either Distributor, serviceCentre or Customer group based on $group

// Create new C5 user based on this forms posted data and return user info object in $ui
$data = array('uName' => $username, 'uEmail' => $email, 'uPassword' => $password);
$ui = UserInfo::add($data);

// Get user object $u from user info object $ui and add $u to the group $group passed to this function above
$u = $ui->getUserObject();
// Next get the group name and place in $g
$g = Group::getByName($group);
// Finally enter $u into the group $g
$u->enterGroup($g);

}

To further complete things, the site i'm working on is in a shared hosting environment and I can't seem to get to or write to the error log, all attempts to get C5 logging working after reading the forums have failed and so too have my attempts to get FirePHP running after again reading the forums and much googling. The code above works except that the part associated with adding the user to a group fails silently.

I was wondering if the problem is related to scope in some way? The above "add user and group" function that I have added is part of a class in the models folder of an existing addon, ie, /packages/3rdpartyaddon/models/extend.php. Essentially, that 3rd party addon's view.php renders a form that posts it's data via AJAX to a PHP form processing script, in that processing script I'm calling my function above.

Thanks in advance! I understand that this post is kinda vague, but i'm hoping that there is something very obvious that I am missing despite much reading of the C5 API today and all the forum posts that I could find relating to adding users to groups via API.

Kind regards,
Ben

 
mnakalay replied on at Permalink Reply
mnakalay
Hi,

I'm not 100% sure but I think you need to modify
$ui = UserInfo::add($data);

to
$ui = UserInfo::register($data);


I'm basing this on the fact that it's what they do in the registration controller and that the register function is commented as "slightly different than add. this is public facing"
whitecliffs replied on at Permalink Reply
Hi mnakalay,

Thanks for the info, I'm still grinding away with no result yet.
mnakalay replied on at Permalink Reply
mnakalay
Maybe it's a problem with your settings. Are registrations set on "sign up and go" and no email verification required?

Another value you can add to your $data is $data['uIsValidated'] and set it to 1. That might help.
whitecliffs replied on at Permalink Reply
Hi,

Thanks to mnakalay for getting back and trying to help out!

In the end I had not loaded the groups model as I had assumed it would be available by default.

This was a silly assumption brought on by the fact that adding a new C5 user did not seem to require any models, libraries or helpers to be in place. If some are required to add a new C5 user then they must have been inherited and included earlier and I did not realise.

Thanks anyway,
Ben