Fix manager invite administrateur

This commit is contained in:
simon lehericey 2019-08-09 11:41:36 +02:00
parent c08e356367
commit d0ac1c3301
6 changed files with 51 additions and 41 deletions

View file

@ -3,7 +3,9 @@ class Administrateurs::ActivateController < ApplicationController
def new
@token = params[:token]
@administrateur = Administrateur.find_inactive_by_token(@token)
user = User.with_reset_password_token(@token)
@administrateur = user&.administrateur
if @administrateur
# the administrateur activates its account from an email
@ -16,14 +18,16 @@ class Administrateurs::ActivateController < ApplicationController
def create
password = update_administrateur_params[:password]
administrateur = Administrateur.reset_password(
update_administrateur_params[:reset_password_token],
password
)
if administrateur && administrateur.errors.empty?
sign_in(administrateur, scope: :administrateur)
try_to_authenticate(User, administrateur.email, password)
user = User.reset_password_by_token({
password: password,
password_confirmation: password,
reset_password_token: update_administrateur_params[:reset_password_token]
})
if user&.administrateur&.errors&.empty?
sign_in(user, scope: :user)
flash.notice = "Mot de passe enregistré"
redirect_to admin_procedures_path
else

View file

@ -55,18 +55,6 @@ class Administrateur < ApplicationRecord
end
end
def invite!(administration_id)
if active?
raise "Impossible d'inviter un utilisateur déjà actif !"
end
reset_password_token = set_reset_password_token
AdministrationMailer.invite_admin(self, reset_password_token, administration_id).deliver_later
reset_password_token
end
def remind_invitation!
if active?
raise "Impossible d'envoyer un rappel d'invitation à un utilisateur déjà actif !"

View file

@ -9,29 +9,33 @@ class Administration < ApplicationRecord
def invite_admin(email)
password = SecureRandom.hex
administrateur = Administrateur.new({
email: email,
active: false,
password: password,
password_confirmation: password
})
if administrateur.save
AdministrationMailer.new_admin_email(administrateur, self).deliver_later
administrateur.invite!(id)
user = User.find_by(email: email)
user = User.create({
if user.nil?
# set confirmed_at otherwise admin confirmation doesnt work
# we somehow mess up using reset_password logic instead of
# confirmation_logic
# FIXME
user = User.create(
email: email,
password: password,
confirmed_at: Time.zone.now
})
Instructeur.create({
email: email,
user: user
})
)
end
administrateur
if user.errors.empty?
if user.instructeur.nil?
Instructeur.create!(email: email, user: user)
end
if user.administrateur.nil?
administrateur = Administrateur.create!(email: email, active: false, user: user)
AdministrationMailer.new_admin_email(administrateur, self).deliver_later
user.invite_administrateur!(id)
end
end
user
end
end

View file

@ -45,6 +45,18 @@ class User < ApplicationRecord
UserMailer.invite_instructeur(self, set_reset_password_token).deliver_later
end
def invite_administrateur!(administration_id)
if administrateur.active?
raise "Impossible d'inviter un utilisateur déjà actif !"
end
reset_password_token = set_reset_password_token
AdministrationMailer.invite_admin(self, reset_password_token, administration_id).deliver_later
reset_password_token
end
private
def link_invites!

View file

@ -1,7 +1,7 @@
describe Administrateurs::ActivateController, type: :controller do
describe '#new' do
let(:admin) { create(:administrateur) }
let(:token) { admin.send(:set_reset_password_token) }
let(:token) { admin.user.send(:set_reset_password_token) }
before { allow(controller).to receive(:trust_device) }

View file

@ -4,13 +4,15 @@ describe Administration, type: :model do
describe '#invite_admin' do
let(:administration) { create :administration }
let(:valid_email) { 'paul@tps.fr' }
subject { administration.invite_admin(valid_email) }
it {
expect(subject.errors).to be_empty
expect(subject).to be_persisted
expect(administration.invite_admin(valid_email).errors).not_to be_empty
user = subject
expect(user.errors).to be_empty
expect(user).to be_persisted
}
it { expect(administration.invite_admin(nil).errors).not_to be_empty }
it { expect(administration.invite_admin('toto').errors).not_to be_empty }