2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::AccountsController < Api::BaseController
|
2019-12-24 12:02:27 +00:00
|
|
|
include AuthHelper
|
|
|
|
|
2019-10-20 08:47:26 +00:00
|
|
|
skip_before_action :verify_authenticity_token, only: [:create]
|
2020-06-07 15:01:48 +00:00
|
|
|
skip_before_action :authenticate_user!, :set_current_user, :handle_with_exception,
|
2019-08-14 09:48:44 +00:00
|
|
|
only: [:create], raise: false
|
2020-03-29 06:46:31 +00:00
|
|
|
before_action :check_signup_enabled, only: [:create]
|
|
|
|
before_action :fetch_account, except: [:create]
|
2020-05-26 17:08:48 +00:00
|
|
|
before_action :check_authorization, except: [:create]
|
2019-08-14 09:48:44 +00:00
|
|
|
|
|
|
|
rescue_from CustomExceptions::Account::InvalidEmail,
|
|
|
|
CustomExceptions::Account::UserExists,
|
|
|
|
CustomExceptions::Account::UserErrors,
|
|
|
|
with: :render_error_response
|
|
|
|
|
|
|
|
def create
|
2020-07-26 07:24:50 +00:00
|
|
|
@user, @account = AccountBuilder.new(
|
2019-12-26 06:22:14 +00:00
|
|
|
account_name: account_params[:account_name],
|
2020-05-11 17:37:22 +00:00
|
|
|
email: account_params[:email],
|
2020-07-26 07:24:50 +00:00
|
|
|
confirmed: confirmed?,
|
|
|
|
user: current_user
|
2019-12-26 06:22:14 +00:00
|
|
|
).perform
|
2019-08-14 09:48:44 +00:00
|
|
|
if @user
|
2019-12-24 12:02:27 +00:00
|
|
|
send_auth_headers(@user)
|
2020-07-26 07:24:50 +00:00
|
|
|
render 'api/v1/accounts/create.json', locals: { resource: @user }
|
2019-08-14 09:48:44 +00:00
|
|
|
else
|
|
|
|
render_error_response(CustomExceptions::Account::SignupFailed.new({}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-29 06:46:31 +00:00
|
|
|
def show
|
|
|
|
render 'api/v1/accounts/show.json'
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2020-07-19 17:38:07 +00:00
|
|
|
@account.update!(account_params.slice(:name, :locale, :domain, :support_email))
|
2020-03-29 06:46:31 +00:00
|
|
|
end
|
|
|
|
|
2020-05-26 17:08:48 +00:00
|
|
|
def update_active_at
|
|
|
|
@current_account_user.active_at = Time.now.utc
|
|
|
|
@current_account_user.save!
|
|
|
|
head :ok
|
|
|
|
end
|
|
|
|
|
2019-08-14 09:48:44 +00:00
|
|
|
private
|
|
|
|
|
2020-03-29 06:46:31 +00:00
|
|
|
def check_authorization
|
|
|
|
authorize(Account)
|
|
|
|
end
|
|
|
|
|
2020-05-11 17:37:22 +00:00
|
|
|
def confirmed?
|
|
|
|
super_admin? && params[:confirmed]
|
|
|
|
end
|
|
|
|
|
2020-03-29 06:46:31 +00:00
|
|
|
def fetch_account
|
|
|
|
@account = current_user.accounts.find(params[:id])
|
2020-05-26 17:08:48 +00:00
|
|
|
@current_account_user = @account.account_users.find_by(user_id: current_user.id)
|
2020-03-29 06:46:31 +00:00
|
|
|
end
|
|
|
|
|
2019-12-24 12:02:27 +00:00
|
|
|
def account_params
|
2020-07-19 17:38:07 +00:00
|
|
|
params.permit(:account_name, :email, :name, :locale, :domain, :support_email)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2020-02-29 05:50:33 +00:00
|
|
|
|
|
|
|
def check_signup_enabled
|
|
|
|
raise ActionController::RoutingError, 'Not Found' if ENV.fetch('ENABLE_ACCOUNT_SIGNUP', true) == 'false'
|
|
|
|
end
|
2020-05-26 17:08:48 +00:00
|
|
|
|
|
|
|
def pundit_user
|
|
|
|
{
|
|
|
|
user: current_user,
|
|
|
|
account: @account,
|
|
|
|
account_user: @current_account_user
|
|
|
|
}
|
|
|
|
end
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|