diff --git a/app/controllers/base_chats_controller.rb b/app/controllers/base_chats_controller.rb index 73d3acc1..779365b8 100644 --- a/app/controllers/base_chats_controller.rb +++ b/app/controllers/base_chats_controller.rb @@ -38,6 +38,8 @@ def create # DELETE /chats/1 or /chats/1.json def destroy + authorize @chat + @chat.destroy! respond_to do |format| diff --git a/app/policies/chat_policy.rb b/app/policies/chat_policy.rb new file mode 100644 index 00000000..b2b89c48 --- /dev/null +++ b/app/policies/chat_policy.rb @@ -0,0 +1,30 @@ +# frozen_string_literal: true + +class ChatPolicy < ApplicationPolicy + attr_reader :user, :chat + + def initialize(user, chat) + @user = user + @chat = chat + end + + def create? + true # Users can create chats + end + + def show? + true # Users can view chats (may need to restrict this later) + end + + def update? + user.admin? || chat.user_id == user.id + end + + def edit? + update? + end + + def destroy? + user.admin? || chat.user_id == user.id + end +end diff --git a/app/views/chats/show.html.erb b/app/views/chats/show.html.erb index 3fef78f2..0e0b29f9 100644 --- a/app/views/chats/show.html.erb +++ b/app/views/chats/show.html.erb @@ -8,13 +8,15 @@ <%= link_to (@chat.first_message ? @chat.first_message.truncate(100) : "Chat"), @chat %> -
- <%= button_to @chat, method: :delete, data: { confirm: 'Are you sure?' }, class: 'text-red-500 hover:text-red-800 flex items-center' do %> - - - - <% end %> -
+ <% if policy(@chat).destroy? %> +
+ <%= button_to @chat, method: :delete, data: { turbo_confirm: 'Are you sure you want to delete this chat? This action cannot be undone.' }, class: 'text-red-500 hover:text-red-800 flex items-center' do %> + + + + <% end %> +
+ <% end %> <% if @chat.webhook %>