2023-05-21 17:59:36 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
class UsersController < ApplicationController
|
2024-04-28 10:27:44 +03:00
|
|
|
load_and_authorize_resource :user, only: %i[index edit update destroy]
|
2023-09-17 00:45:57 +03:00
|
|
|
|
2024-04-28 10:27:44 +03:00
|
|
|
before_action :build_user, only: %i[new create]
|
|
|
|
|
authorize_resource :user, only: %i[new create]
|
2023-05-21 17:59:36 +03:00
|
|
|
|
|
|
|
|
def index
|
2024-04-19 16:08:12 +03:00
|
|
|
@users =
|
|
|
|
|
if params[:status] == 'archived'
|
|
|
|
|
@users.archived
|
|
|
|
|
else
|
|
|
|
|
@users.active
|
|
|
|
|
end
|
|
|
|
|
|
2024-04-28 10:27:44 +03:00
|
|
|
@pagy, @users = pagy(@users.where(account: current_account).order(id: :desc))
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
2023-09-17 00:45:57 +03:00
|
|
|
def new; end
|
2023-05-21 17:59:36 +03:00
|
|
|
|
|
|
|
|
def edit; end
|
|
|
|
|
|
|
|
|
|
def create
|
2024-04-19 16:08:12 +03:00
|
|
|
existing_user = User.accessible_by(current_ability).find_by(email: @user.email)
|
|
|
|
|
|
|
|
|
|
if existing_user
|
|
|
|
|
existing_user.archived_at = nil
|
|
|
|
|
existing_user.assign_attributes(user_params)
|
2024-05-30 22:18:32 +03:00
|
|
|
existing_user.account = current_account
|
2024-04-19 16:08:12 +03:00
|
|
|
|
|
|
|
|
@user = existing_user
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-21 17:59:36 +03:00
|
|
|
if @user.save
|
|
|
|
|
UserMailer.invitation_email(@user).deliver_later!
|
|
|
|
|
|
2024-04-19 16:08:12 +03:00
|
|
|
redirect_back fallback_location: settings_users_path, notice: 'User has been invited'
|
2023-05-21 17:59:36 +03:00
|
|
|
else
|
|
|
|
|
render turbo_stream: turbo_stream.replace(:modal, template: 'users/new'), status: :unprocessable_entity
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2023-07-09 16:27:31 +03:00
|
|
|
return redirect_to settings_users_path, notice: 'Unable to update user.' if Docuseal.demo?
|
|
|
|
|
|
2024-04-19 16:08:12 +03:00
|
|
|
attrs = user_params.compact_blank.merge(user_params.slice(:archived_at))
|
2023-12-30 22:51:26 +02:00
|
|
|
attrs.delete(:role) if !role_valid?(attrs[:role]) || current_user == @user
|
2023-10-07 15:03:53 +03:00
|
|
|
|
2023-12-30 22:51:26 +02:00
|
|
|
if @user.update(attrs)
|
2024-04-19 16:08:12 +03:00
|
|
|
redirect_back fallback_location: settings_users_path, notice: 'User has been updated'
|
2023-05-21 17:59:36 +03:00
|
|
|
else
|
|
|
|
|
render turbo_stream: turbo_stream.replace(:modal, template: 'users/edit'), status: :unprocessable_entity
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def destroy
|
2023-07-09 16:27:31 +03:00
|
|
|
if Docuseal.demo? || @user.id == current_user.id
|
|
|
|
|
return redirect_to settings_users_path, notice: 'Unable to remove user'
|
|
|
|
|
end
|
|
|
|
|
|
2023-12-30 00:37:03 +02:00
|
|
|
@user.update!(archived_at: Time.current)
|
2023-05-21 17:59:36 +03:00
|
|
|
|
2024-04-19 16:08:12 +03:00
|
|
|
redirect_back fallback_location: settings_users_path, notice: 'User has been removed'
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2023-12-30 22:51:26 +02:00
|
|
|
def role_valid?(role)
|
|
|
|
|
User::ROLES.include?(role)
|
|
|
|
|
end
|
|
|
|
|
|
2023-09-17 00:45:57 +03:00
|
|
|
def build_user
|
|
|
|
|
@user = current_account.users.find_by(email: user_params[:email])&.tap do |user|
|
|
|
|
|
user.assign_attributes(user_params)
|
2023-12-30 00:37:03 +02:00
|
|
|
user.archived_at = nil
|
2023-09-17 00:45:57 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@user ||= current_account.users.new(user_params)
|
|
|
|
|
|
|
|
|
|
@user
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def user_params
|
2024-04-28 10:27:44 +03:00
|
|
|
if params.key?(:user)
|
|
|
|
|
params.require(:user).permit(:email, :first_name, :last_name, :password,
|
|
|
|
|
:role, :archived_at, :account_id)
|
|
|
|
|
else
|
|
|
|
|
{}
|
|
|
|
|
end
|
2023-05-21 17:59:36 +03:00
|
|
|
end
|
|
|
|
|
end
|