Creating a user and adding them to a group
Permalink
I have created a form that allows a user to make another user. So, I call:
After creating the user, I flag a few things. For example...
I also want to add the user to a group, but this seems to be failing.
I am using:
Am I missing something?
$userRegistrationService = Core::make('Concrete\Core\User\RegistrationServiceInterface'); $userinfo = [ 'uName' => $uname, 'uEmail' => $email, 'uPassword' => $password, 'uIsValidated' => '1' ]; $user = $userRegistrationService->create($userinfo);
After creating the user, I flag a few things. For example...
$u = new User(); $ui = UserInfo::getByID($u->getUserID()); $user->setAttribute('sponsor',$ui->getUserName());
I also want to add the user to a group, but this seems to be failing.
I am using:
$membergroup = \Concrete\Core\User\Group\Group::getByName('Member'); $user->enterGroup($membergroup);
Am I missing something?
Try this
I get the same result, sadly. Creates the user, adds the attributes, doesn't add the group.
Does the user group 'Member' already exist or are you wanting to create it within the code?
This group already exists.
So I think I may have found the issue. I'm using the UserInfo object, and I need to use User.
The User object I have is the logged in user, so I need to get the correct user (the one I just created).
I can't getByID on the UserObject. What's the best way to do this?
I have tried:
But, obviously, this doesn't work.
Then I tried:
Which kinda worked, but resulted in the error: Call to a member function enterGroup() on null
The User object I have is the logged in user, so I need to get the correct user (the one I just created).
I can't getByID on the UserObject. What's the best way to do this?
I have tried:
$client = User::getByID($user); $client->enterGroup($membergroup);
But, obviously, this doesn't work.
Then I tried:
$client = User::getByUserID($user); $client->enterGroup($membergroup);
Which kinda worked, but resulted in the error: Call to a member function enterGroup() on null
So, what I am trying so far is:
Which results in:
Call to a member function enterGroup() on null
I'm obviously not returning what I expect. What's the easiest way to return the ID of the user I just created?
$uname = $_GET['firstname'] . $_GET['lastname']; $email = $_GET['email']; $password = $_GET['password']; $userRegistrationService = Core::make('Concrete\Core\User\RegistrationServiceInterface'); $userinfo = [ 'uName' => $uname, 'uEmail' => $email, 'uPassword' => $password]; $user = $userRegistrationService->create($userinfo); $u = new User(); // Get logged in user $ui = UserInfo::getByID($u->getUserID()); // Get userinfo for logged in user $user->setAttribute('sponsor',$ui->getUserName()); // Set sponsor field $membergroup = Group::getByName('Member'); $clientid = UserInfo::getByUserName($uname); $client = User::getByUserID($clientid); $client->enterGroup($membergroup);
Which results in:
Call to a member function enterGroup() on null
I'm obviously not returning what I expect. What's the easiest way to return the ID of the user I just created?