2

I'm trying to create a pending user account when an event occurs

$newUser = new User([
  'firstName' => $submission->firstName->getValue(),
  'lastName' => $submission->lastName->getValue(),
  'username' => $email,
  'email' => $email,
  'password' => Yii::$app->security->generatePasswordHash($submission->password->getValue()),
  'isCandidate' => true,
]);

Craft::$app->getElements()->saveElement($newUser, false);

but that always creates an enabled user.

I know that I want to do Craft::$app->getUsers()->sendActivationEmail($newUser); after the fact to send the activation email that works.

1 Answer 1

5

Rolling with this does what I need

$newUser = new User([
  'firstName' => $submission->firstName->getValue(),
  'lastName' => $submission->lastName->getValue(),
  'username' => $email,
  'email' => $email,
  'password' => Yii::$app->security->generatePasswordHash($email),
  'isCandidate' => true,
  'pending' => true,
]);

$newUser->validate(null, false) && Craft::$app->getElements()->saveElement($newUser, false);

Craft::$app->getUsers()->sendNewEmailVerifyEmail($newUser);

I was a bit confused because after sending the verification email, my user was always enabled. Turns out my local mail testing tool was verifying the validity of the link in the email by doing a GET request 🙈 thus enabling the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.