Using Craft::Pro Edition, I want to add a specific UserGroup to an existing User in my module. I tried it this way, but the group is not saved.

$userGroup = Craft::$app->userGroups->getGroupByHandle('partner');
$success = Craft::$app->elements->saveElement($user);

After (successful) saveElement(), $user->getGroups() still returns an empty array.

When I add the group in the Admin CP, it works, but it needs a password confirmation.


You can assign a user to certain groups via

Craft::$app->getUsers()->assignUserToGroups($user->id, [1,2,3]);

Would assign them to the groups with the ids 1, 2 and 3

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .