diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb index 18597530238..5be4ebcdb98 100644 --- a/app/controllers/invitations_controller.rb +++ b/app/controllers/invitations_controller.rb @@ -8,10 +8,6 @@ def create @invitation = Current.family.invitations.build(invitation_params) @invitation.inviter = Current.user - if @invitation.role == "admin" && !Current.user.admin? - @invitation.role = "member" - end - if @invitation.save InvitationMailer.invite_email(@invitation).deliver_later flash[:notice] = t(".success") diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb index 104ac5aaad6..9a6c7c89e49 100644 --- a/app/controllers/registrations_controller.rb +++ b/app/controllers/registrations_controller.rb @@ -4,7 +4,7 @@ class RegistrationsController < ApplicationController layout "auth" before_action :set_user, only: :create - before_action :load_invitation, if: :invitation_token? + before_action :set_invitation before_action :claim_invite_code, only: :create, if: :invite_code_required? def new @@ -34,21 +34,18 @@ def create private - def load_invitation - token = params[:invitation] || params.dig(:user, :invitation) - @invitation = Invitation.pending.find_by!(token: token) - end - - def invitation_token? - params[:invitation].present? || params.dig(:user, :invitation).present? + def set_invitation + token = params[:invitation] || user_params(:invitation) + @invitation = Invitation.pending.find_by(token: token) end def set_user @user = User.new user_params.except(:invite_code, :invitation) end - def user_params - params.require(:user).permit(:name, :email, :password, :password_confirmation, :invite_code, :invitation) + def user_params(specific_param = nil) + params = self.params.require(:user).permit(:name, :email, :password, :password_confirmation, :invite_code, :invitation) + specific_param ? params[specific_param] : params end def claim_invite_code diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 6bb97e861f6..41770b50ac4 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -5,6 +5,7 @@ class Invitation < ApplicationRecord validates :email, presence: true, format: { with: URI::MailTo::EMAIL_REGEXP } validates :role, presence: true, inclusion: { in: %w[admin member] } validates :token, presence: true, uniqueness: true + validate :inviter_is_admin before_validation :generate_token, on: :create before_create :set_expiration @@ -29,4 +30,8 @@ def generate_token def set_expiration self.expires_at = 3.days.from_now end + + def inviter_is_admin + inviter.admin? + end end