Adds User#owns?

This commit is contained in:
gregoirenovel 2018-05-30 18:26:23 +02:00
parent a007d6e3eb
commit 4e9565a5df
5 changed files with 40 additions and 9 deletions

View file

@ -73,7 +73,7 @@ module NewUser
@dossier.en_construction!
NotificationMailer.send_initiated_notification(@dossier).deliver_later
redirect_to merci_dossier_path(@dossier)
elsif owns_dossier?
elsif current_user.owns?(dossier)
redirect_to users_dossier_recapitulatif_path(@dossier)
else
redirect_to users_dossiers_invite_path(@dossier.invite_for_user(current_user))
@ -142,7 +142,7 @@ module NewUser
end
def ensure_ownership!
if !owns_dossier?
if !current_user.owns?(dossier)
forbidden!
end
end
@ -154,7 +154,7 @@ module NewUser
end
def forbid_invite_submission!
if passage_en_construction? && !owns_dossier?
if passage_en_construction? && !current_user.owns?(dossier)
forbidden!
end
end
@ -172,10 +172,6 @@ module NewUser
params.require(:dossier).permit(:autorisation_donnees)
end
def owns_dossier?
dossier.user_id == current_user.id
end
def passage_en_construction?
dossier.brouillon? && !draft?
end

View file

@ -38,6 +38,10 @@ class User < ApplicationRecord
loged_in_with_france_connect.present?
end
def owns?(dossier)
dossier.user_id == id
end
def invite?(dossier_id)
invites.pluck(:dossier_id).include?(dossier_id.to_i)
end

View file

@ -14,7 +14,7 @@
.dossier-state= @facade.dossier.display_state
.split-hr-left
- if @facade.dossier.user == current_user
- if current_user.owns?(@facade.dossier)
.text-center.mt-1
= link_to ask_deletion_dossier_path(@facade.dossier), method: :post, class: "btn btn-danger", data: { confirm: delete_dossier_confirm(@facade.dossier) } do
Supprimer définitivement

View file

@ -58,7 +58,7 @@
class: 'button send',
data: { action: 'draft', disable_with: 'Envoi...' }
- if dossier.user == current_user
- if current_user.owns?(dossier)
= f.button 'Soumettre le dossier',
class: 'button send primary',
data: { action: 'submit', disable_with: 'Envoi...' }

View file

@ -31,6 +31,37 @@ describe User, type: :model do
end
end
describe '#owns?' do
let(:owner) { create(:user) }
let(:dossier) { create(:dossier, user: owner) }
let(:invite_user) { create(:user) }
let(:invite_gestionnaire) { create(:user) }
subject { user.owns?(dossier) }
context 'when user is owner' do
let(:user) { owner }
it { is_expected.to be_truthy }
end
context 'when user was invited by user' do
before do
create(:invite, dossier: dossier, user: invite_user, type: 'InviteUser')
end
let(:user) { invite_user }
it { is_expected.to be_falsy }
end
context 'when user is quidam' do
let(:user) { create(:user) }
it { is_expected.to be_falsey }
end
end
describe '#invite?' do
let(:dossier) { create :dossier }
let(:user) { dossier.user }