DEV: Confirm email when creating users via the API

DEV: Confirm email when creating users via the API

This commit is for a frequently requested task on meta so that only 1 API call is needed instead of 3!

In order to create a user via the api and not have them receive an activation email you can pass in the active=true flag. This prevents sending an email, but it is only half of the solution and puts the db in a weird state where it has an active user with an unconfirmed email.

This commit fixes that and ensures that if the active=true flag is set the user’s email is also marked as confirmed.

This change only applies to admins using the API.

Related topics on meta:

diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index b3acc1f..29d7bc9 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -507,8 +507,10 @@ class UsersController < ApplicationController
       session["user_created_message"] = activation.message
       session[SessionController::ACTIVATE_USER_KEY] = user.id
 
-      # If the user was created as active, they might need to be approved
-      user.create_reviewable if user.active?
+      # If the user was created as active this will
+      # ensure their email is confirmed and
+      # add them to the review queue if they need to be approved
+      user.activate if user.active?
 
       render json: {
         success: true,
diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rb
index d35e8f9..507c28f 100644
--- a/spec/requests/users_controller_spec.rb
+++ b/spec/requests/users_controller_spec.rb
@@ -768,6 +768,7 @@ describe UsersController do
           json = JSON.parse(response.body)
 
           new_user = User.find(json["user_id"])
+          email_token = new_user.email_tokens.active.where(email: new_user.email).first
 
           expect(json['active']).to be_truthy
 
@@ -775,6 +776,7 @@ describe UsersController do
           expect(new_user.approved).to eq(true)
           expect(new_user.approved_by_id).to eq(admin.id)
           expect(new_user.approved_at).to_not eq(nil)
+          expect(email_token.confirmed?).to eq(true)
         end
 
         it "will create a reviewable when a user is created as active but not approved" do

GitHub sha: fec68d3d

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/creating-active-users-via-the-api-gem/33133/43

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663/39

This commit has been mentioned on Discourse Meta. There might be relevant details there:

https://meta.discourse.org/t/creating-user-via-api/36133/11