From 2c97b5fb184a4c90fc918bee2362fa6b4d324503 Mon Sep 17 00:00:00 2001 From: Shivam Singhal Date: Sun, 3 Nov 2024 03:43:26 +0530 Subject: [PATCH] Handle deleting a member from an org --- .../accounts/memberships_controller.rb | 25 +++++++++++++++++++ config/routes.rb | 1 + 2 files changed, 26 insertions(+) create mode 100644 app/controllers/accounts/memberships_controller.rb diff --git a/app/controllers/accounts/memberships_controller.rb b/app/controllers/accounts/memberships_controller.rb new file mode 100644 index 000000000..b666344b7 --- /dev/null +++ b/app/controllers/accounts/memberships_controller.rb @@ -0,0 +1,25 @@ +class Accounts::MembershipsController < SignedInApplicationController + before_action :require_write_access!, only: %i[destroy] + + def destroy + @membership = current_organization.memberships.find_by(id: params[:id]) + + unless helpers.can_current_user_remove_member?(@membership.user) + redirect_to teams_accounts_organization_path(current_organization), + flash: { error: "You don't have permission to remove this member" } + return + end + + if @membership.discarded? + redirect_to teams_accounts_organization_path(current_organization), + flash: { error: "Member was already removed" } + elsif @membership.discard + puts "MembershipsController inside foo destroy" + redirect_to teams_accounts_organization_path(current_organization), + notice: "Member #{@membership.user.email} has been removed" + else + redirect_to teams_accounts_organization_path(current_organization), + flash: {error: "#{@membership.errors.full_messages.to_sentence}"} + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 2a3a2f2ab..a21383dda 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,6 +50,7 @@ resources :teams, only: %i[create update destroy] resources :invitations, only: [:create] + resources :memberships, only: [:destroy] end resource :user, only: [:edit, :update] do