2020-06-07 08:28:05 +00:00
|
|
|
class Api::V1::AccountsController < Api::BaseController
|
2019-12-24 12:02:27 +00:00
|
|
|
include AuthHelper
|
|
|
|
|
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-12-21 08:35:19 +00:00
|
|
|
user_full_name: account_params[:user_full_name],
|
2020-05-11 17:37:22 +00:00
|
|
|
email: account_params[:email],
|
2021-06-07 11:56:08 +00:00
|
|
|
user_password: account_params[:password],
|
2020-07-26 07:24:50 +00:00
|
|
|
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
|
2021-01-17 18:29:09 +00:00
|
|
|
@latest_chatwoot_version = ::Redis::Alfred.get(::Redis::Alfred::LATEST_CHATWOOT_VERSION)
|
2020-03-29 06:46:31 +00:00
|
|
|
render 'api/v1/accounts/show.json'
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2020-11-01 07:23:25 +00:00
|
|
|
@account.update!(account_params.slice(:name, :locale, :domain, :support_email, :auto_resolve_duration))
|
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 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
|
2021-06-07 11:56:08 +00:00
|
|
|
params.permit(:account_name, :email, :name, :password, :locale, :domain, :support_email, :auto_resolve_duration, :user_full_name)
|
2019-08-14 09:48:44 +00:00
|
|
|
end
|
2020-02-29 05:50:33 +00:00
|
|
|
|
|
|
|
def check_signup_enabled
|
2022-01-12 03:20:23 +00:00
|
|
|
raise ActionController::RoutingError, 'Not Found' if GlobalConfigService.load('ENABLE_ACCOUNT_SIGNUP', 'false') == 'false'
|
2020-02-29 05:50:33 +00:00
|
|
|
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
|