Skip to content

Commit

Permalink
fixing data integrity constrains for some associations to User model,…
Browse files Browse the repository at this point in the history
… currently it is possible to delete user which still referred.

BZ https://bugzilla.redhat.com/show_bug.cgi?id=1677744
  • Loading branch information
yrudman committed Feb 21, 2019
1 parent 1680ffb commit ca181e0
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class User < ApplicationRecord
include TimezoneMixin
include CustomActionsMixin

before_destroy :check_reference, :prepend => true

has_many :miq_approvals, :as => :approver
has_many :miq_approval_stamps, :class_name => "MiqApproval", :foreign_key => :stamper_id
has_many :miq_requests, :foreign_key => :requester_id
Expand Down Expand Up @@ -110,6 +112,18 @@ def validate
before_validation :dummy_password_for_external_auth
before_destroy :destroy_subscribed_widget_sets

def check_reference
present_ref = []
%w(miq_requests vms miq_widgets miq_templates).each do |association|
present_ref << association.classify unless public_send(association).first.nil?
end

unless present_ref.empty?
errors.add(:base, "user with id [#{id}] has references to other models: #{present_ref.join(" ")}")
throw :abort
end
end

def current_group_by_description=(group_description)
if group_description
desired_group = miq_groups.detect { |g| g.description == group_description }
Expand Down

0 comments on commit ca181e0

Please sign in to comment.