demarches-normaliennes/app/controllers/invites_controller.rb

74 lines
2.1 KiB
Ruby
Raw Normal View History

class InvitesController < ApplicationController
before_action :authenticate_user!, only: [:create]
before_action :store_user_location!, only: [:show]
2016-09-13 12:17:56 +02:00
def create
email = params[:invite_email].downcase
2022-05-24 11:59:14 +02:00
@dossier = current_user.dossiers.visible_by_user.find(params[:dossier_id])
2016-12-13 14:22:54 +01:00
2018-10-10 09:23:08 +02:00
invite = Invite.create(
2022-05-24 11:59:14 +02:00
dossier: @dossier,
2018-03-22 09:31:07 +01:00
user: User.find_by(email: email),
email: email,
message: params[:invite_message],
2018-03-22 09:31:07 +01:00
email_sender: current_user.email
)
if invite.valid?
2020-09-25 11:10:12 +02:00
# The notification is sent through an after commit hook in order to avoir concurrency issues
flash.notice = "Une invitation a été envoyée à #{invite.email}."
else
flash.alert = invite.errors.full_messages
end
respond_to do |format|
2022-05-24 11:59:14 +02:00
format.html { redirect_back(fallback_location: helpers.url_for_dossier(@dossier)) }
format.turbo_stream
end
end
2016-09-13 12:17:56 +02:00
def show
if user_signed_in?
erase_user_location!
dossier = Dossier.joins(:invites)
.find_by!(invites: { email: current_user.email, id: params[:id] })
2016-09-13 12:17:56 +02:00
2022-05-24 11:59:14 +02:00
redirect_to helpers.url_for_dossier(dossier)
elsif params[:email].present? && !User.find_by(email: params[:email])
redirect_to new_user_registration_path(user: { email: params[:email] })
else
authenticate_user!
2018-01-11 19:04:39 +01:00
end
rescue ActiveRecord::RecordNotFound
flash.alert = t('errors.messages.dossier_not_found')
redirect_to dossiers_path
end
def destroy
invite = Invite.find(params[:id])
2022-05-24 11:59:14 +02:00
@dossier = invite.dossier
if @dossier.user == current_user
invite.destroy!
2021-05-26 15:16:30 +02:00
flash.notice = "Lautorisation de #{invite.email} vient dêtre révoquée."
else
flash.alert = "Vous ne pouvez pas révoquer cette autorisation"
end
respond_to do |format|
2022-05-24 11:59:14 +02:00
format.html { redirect_back(fallback_location: helpers.url_for_dossier(@dossier)) }
format.turbo_stream
end
end
private
def store_user_location!
store_location_for(:user, request.fullpath)
end
def erase_user_location!
clear_stored_location_for(:user)
2016-09-13 12:17:56 +02:00
end
end