FEATURE: API endpoint for inviting an admin

This commit is contained in:
Sam 2014-11-24 15:42:39 +11:00
parent c139498235
commit 1c498eb491
3 changed files with 42 additions and 0 deletions

View File

@ -288,6 +288,35 @@ class Admin::UsersController < Admin::AdminController
render json: success_json
end
def invite_admin
email = params[:email]
unless user = User.find_by_email(email)
name = params[:name] if params[:name].present?
username = params[:username] if params[:username].present?
user = User.new(email: email)
user.password = SecureRandom.hex
user.username = UserNameSuggester.suggest(username || name || email)
user.name = User.suggest_name(name || username || email)
end
user.active = true
user.save!
user.grant_admin!
user.change_trust_level!(4)
user.email_tokens.update_all confirmed: true
email_token = user.email_tokens.create(email: user.email)
Jobs.enqueue(:user_email,
type: :account_created,
user_id: user.id,
email_token: email_token.token)
render json: success_json
end
private
def fetch_user

View File

@ -84,6 +84,7 @@ Discourse::Application.routes.draw do
post "users/sync_sso" => "users#sync_sso", constraints: AdminConstraint.new
post "users/invite_admin" => "users#invite_admin", constraints: AdminConstraint.new
resources :impersonate, constraints: AdminConstraint.new

View File

@ -427,6 +427,18 @@ describe Admin::UsersController do
end
context ".invite_admin" do
it 'should invite admin' do
xhr :post, :invite_admin, name: 'Bill', username: 'bill22', email: 'bill@bill.com'
response.should be_success
u = User.find_by(email: 'bill@bill.com')
u.name.should == "Bill"
u.username.should == "bill22"
u.admin.should == true
end
end
end
it 'can sync up sso' do