diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a4baaa4d..5463f01f 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -7,7 +7,14 @@ class UsersController < ApplicationController authorize_resource :user, only: :create def index - @pagy, @users = pagy(@users.active.order(id: :desc)) + @users = + if params[:status] == 'archived' + @users.archived + else + @users.active + end + + @pagy, @users = pagy(@users.order(id: :desc)) end def new; end @@ -15,10 +22,19 @@ class UsersController < ApplicationController def edit; end def create + 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) + + @user = existing_user + end + if @user.save UserMailer.invitation_email(@user).deliver_later! - redirect_to settings_users_path, notice: 'User has been invited' + redirect_back fallback_location: settings_users_path, notice: 'User has been invited' else render turbo_stream: turbo_stream.replace(:modal, template: 'users/new'), status: :unprocessable_entity end @@ -27,11 +43,11 @@ class UsersController < ApplicationController def update return redirect_to settings_users_path, notice: 'Unable to update user.' if Docuseal.demo? - attrs = user_params.compact_blank + attrs = user_params.compact_blank.merge(user_params.slice(:archived_at)) attrs.delete(:role) if !role_valid?(attrs[:role]) || current_user == @user if @user.update(attrs) - redirect_to settings_users_path, notice: 'User has been updated' + redirect_back fallback_location: settings_users_path, notice: 'User has been updated' else render turbo_stream: turbo_stream.replace(:modal, template: 'users/edit'), status: :unprocessable_entity end @@ -44,7 +60,7 @@ class UsersController < ApplicationController @user.update!(archived_at: Time.current) - redirect_to settings_users_path, notice: 'User has been removed' + redirect_back fallback_location: settings_users_path, notice: 'User has been removed' end private @@ -65,6 +81,6 @@ class UsersController < ApplicationController end def user_params - params.require(:user).permit(:email, :first_name, :last_name, :password, :role) + params.require(:user).permit(:email, :first_name, :last_name, :password, :role, :archived_at) end end diff --git a/app/models/user.rb b/app/models/user.rb index c9a433c1..237ebc41 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -65,6 +65,7 @@ class User < ApplicationRecord attribute :uuid, :string, default: -> { SecureRandom.uuid } scope :active, -> { where(archived_at: nil) } + scope :archived, -> { where.not(archived_at: nil) } scope :admins, -> { where(role: ADMIN_ROLE) } def access_token diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index b2e6e6ae..06e13425 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -2,7 +2,7 @@ <%= render 'shared/settings_nav' %>
-

Users

+

<%= params[:status].to_s.titleize %> Users

<% if can?(:create, User.new(account: current_account)) %> <%= link_to new_user_path, class: 'btn btn-primary btn-md gap-2', data: { turbo_frame: 'modal' } do %> @@ -55,17 +55,39 @@ Edit <% end %> <% end %> - <% if can?(:destroy, user) && user != current_user %> + <% if params[:status].blank? && can?(:destroy, user) && user != current_user %> <%= button_to user_path(user), method: :delete, class: 'btn btn-outline btn-error btn-xs', title: 'Delete', data: { turbo_confirm: 'Are you sure?' } do %> Remove <% end %> <% end %> + <% if params[:status].present? && can?(:manage, user) && user != current_user %> + <%= button_to user_path(user), method: :put, params: { user: { archived_at: nil } }, class: 'btn btn-outline btn-secondary btn-xs', title: 'Unarchive', data: { turbo_confirm: 'Are you sure?' } do %> + Unarchive + <% end %> + <% end %> <% end %>
- <%= render 'shared/pagination', pagy: @pagy, items_name: 'users' %> + <% view_archived_html = capture do %> + <% if current_account.users.archived.exists? %> +
+ <% if params[:status] == 'archived' %> + View Active + <% else %> + View Archived + <% end %> +
+ <% end %> + <% end %> + <% if @pagy.pages > 1 %> + <%= render 'shared/pagination', pagy: @pagy, items_name: 'users', left_additional_html: view_archived_html %> + <% else %> +
+ <%= view_archived_html %> +
+ <% end %>
diff --git a/config/routes.rb b/config/routes.rb index b7ba10af..1b5be80d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -131,6 +131,8 @@ Rails.application.routes.draw do resources :notifications, only: %i[index create], controller: 'notifications_settings' resource :esign, only: %i[show create new update destroy], controller: 'esign_settings' resources :users, only: %i[index] + resources :archived_users, only: %i[index], path: 'users/:status', controller: 'users', + defaults: { status: :archived } resource :personalization, only: %i[show create], controller: 'personalization_settings' resources :api, only: %i[index create], controller: 'api_settings' resource :webhooks, only: %i[show create update], controller: 'webhook_settings'