[#1677] No need to use emails when we have users

This commit is contained in:
Frederic Merizen 2018-03-29 15:40:58 +02:00
parent a489bbeb44
commit 884c430619
7 changed files with 34 additions and 25 deletions

View file

@ -10,9 +10,11 @@ class UsersController < ApplicationController
dossier = Dossier.find(dossier_id)
return dossier if dossier.owner?(current_user.email) || dossier.invite_by_user?(current_user.email)
if !dossier.owner_or_invite?(current_user)
raise ActiveRecord::RecordNotFound
end
raise ActiveRecord::RecordNotFound
dossier
end
def authorized_routes?(controller)

View file

@ -190,12 +190,8 @@ class Dossier < ApplicationRecord
en_instruction? || accepte? || refuse? || sans_suite?
end
def owner?(email)
user.email == email
end
def invite_by_user?(email)
(invites_user.pluck :email).include? email
def owner_or_invite?(user)
self.user == user || invites_user.find_by(user_id: user.id).present?
end
def can_be_en_construction?

View file

@ -1,5 +1,5 @@
- if !@facade.dossier.read_only?
- if user_signed_in? && (@facade.dossier.owner?(current_user.email) || @facade.dossier.invite_by_user?(current_user.email))
- if user_signed_in? && (@facade.dossier.owner_or_invite?(current_user))
%a#maj_carte.action{ href: "/users/dossiers/#{@facade.dossier.id}/carte" }
.col-lg-2.col-md-2.col-sm-2.col-xs-2.action
= 'éditer'.upcase

View file

@ -1,5 +1,5 @@
- if !@facade.dossier.read_only?
- if user_signed_in? && (@facade.dossier.owner?(current_user.email) || @facade.dossier.invite_by_user?(current_user.email))
- if user_signed_in? && (@facade.dossier.owner_or_invite?(current_user))
= link_to modifier_dossier_path(@facade.dossier), class: 'action', id: 'maj_infos' do
#edit-dossier.col-lg-2.col-md-2.col-sm-2.col-xs-2.action
= "éditer".upcase

View file

@ -1,5 +1,5 @@
- if !@facade.dossier.read_only?
- if user_signed_in? && (@facade.dossier.owner?(current_user.email) || @facade.dossier.invite_by_user?(current_user.email))
- if user_signed_in? && (@facade.dossier.owner_or_invite?(current_user))
- if @facade.procedure.cerfa_flag? || @facade.dossier.types_de_piece_justificative.size > 0
.col-lg-4.col-md-4.col-sm-4.col-xs-4.action
%a#maj_pj.action{ "data-target" => "#upload-pj-modal",

View file

@ -101,7 +101,7 @@
Pièce non fournie
- if !@facade.dossier.read_only?
- if user_signed_in? && (@facade.dossier.owner?(current_user.email) || @facade.dossier.invite_by_user?(current_user.email))
- if user_signed_in? && (@facade.dossier.owner_or_invite?(current_user))
- if @facade.procedure.cerfa_flag? || @facade.dossier.types_de_piece_justificative.size > 0
.row
.col-xs-4

View file

@ -404,28 +404,39 @@ describe Dossier do
end
end
describe '#invite_by_user?' do
let(:dossier) { create :dossier }
let(:invite_user) { create :user, email: user_invite_email }
let(:invite_gestionnaire) { create :user, email: gestionnaire_invite_email }
let(:user_invite_email) { 'plup@plop.com' }
let(:gestionnaire_invite_email) { 'plap@plip.com' }
describe '#owner_or_invite?' do
let(:owner) { create(:user) }
let(:dossier) { create(:dossier, user: owner) }
let(:invite_user) { create(:user) }
let(:invite_gestionnaire) { create(:user) }
before do
create :invite, dossier: dossier, user: invite_user, email: invite_user.email, type: 'InviteUser'
create :invite, dossier: dossier, user: invite_gestionnaire, email: invite_gestionnaire.email, type: 'InviteGestionnaire'
create(:invite, dossier: dossier, user: invite_user, type: 'InviteUser')
create(:invite, dossier: dossier, user: invite_gestionnaire, type: 'InviteGestionnaire')
end
subject { dossier.invite_by_user? email }
subject { dossier.owner_or_invite?(user) }
context 'when email is present on invite list' do
let(:email) { user_invite_email }
context 'when user is owner' do
let(:user) { owner }
it { is_expected.to be_truthy }
end
context 'when email is present on invite list' do
let(:email) { gestionnaire_invite_email }
context 'when user was invited by user' do
let(:user) { invite_user }
it { is_expected.to be_truthy }
end
context 'when user was invited by gestionnaire (legacy, no new invitations happen)' do
let(:user) { invite_gestionnaire }
it { is_expected.to be_falsey }
end
context 'when user is quidam' do
let(:user) { create(:user) }
it { is_expected.to be_falsey }
end