Skip to content

Commit

Permalink
524 fix answer cloning issue (#525)
Browse files Browse the repository at this point in the history
* Fix cloning of answers dependent on action

* delete associated answers on rule deletion

Signed-off-by: Vanessa Fotso <vfotso@mitre.org>

* delete rule reviews and satisfactions

* exclude update rule count for component copy

* rubocop fix

Signed-off-by: Vanessa Fotso <vfotso@mitre.org>
Co-authored-by: freddyfeelgood <darrickw@vmware.com>
Co-authored-by: Vanessa Fotso <vfotso@mitre.org>
  • Loading branch information
3 people committed Jan 26, 2023
1 parent 73be75d commit 44e3217
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
11 changes: 8 additions & 3 deletions app/controllers/rules_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ def update

def destroy
if @rule.update(deleted_at: Time.zone.now)
@rule.additional_answers.destroy_all
@rule.reviews.destroy_all
@rule.satisfied_by.destroy_all
render json: { toast: 'Successfully deleted control.' }
else
render json: {
Expand Down Expand Up @@ -103,9 +106,11 @@ def revert

def create_or_duplicate
if authorize_author_project.nil? && rule_create_params[:duplicate]
rule = Rule.find(rule_create_params[:id]).amoeba_dup
rule.rule_id = nil
rule
rule = Rule.find(rule_create_params[:id])
rule.update_single_rule_clone(true)
new_rule = rule.amoeba_dup
new_rule.rule_id = nil
new_rule
elsif authorize_admin_project.nil?
srg = SecurityRequirementsGuide.find_by(id: @component.security_requirements_guide_id)
srg_rule = srg.parsed_benchmark.rule.find { |r| r.ident.reject(&:legacy).first.ident == 'CCI-000366' }
Expand Down
16 changes: 15 additions & 1 deletion app/models/rule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Rule < BaseRule
nullify :review_requestor_id
set locked: false

include_association :additional_answers
include_association :additional_answers, if: :single_rule_clone?
end

audited except: %i[component_id review_requestor_id created_at updated_at locked inspec_control_file],
Expand Down Expand Up @@ -53,6 +53,13 @@ class Rule < BaseRule

default_scope { where(deleted_at: nil) }

@single_rule_clone = false

# If rule clone not coming from a "copy component" action, allow "answers" to be also cloned
def update_single_rule_clone(rule_clone)
@single_rule_clone = rule_clone
end

def self.from_mapping(rule_mapping, component_id, idx, srg_rules)
rule = super(self, rule_mapping)
rule.audits.build(Audited.audit_class.create_initial_rule_audit_from_mapping(component_id))
Expand Down Expand Up @@ -244,6 +251,10 @@ def basic_fields

private

def single_rule_clone?
@single_rule_clone
end

def export_fixtext
satisfied_by.size.positive? ? satisfied_by.first.fixtext : fixtext
end
Expand Down Expand Up @@ -333,6 +344,9 @@ def apply_audit_comment
end

def update_component_rules_count
# don't update component rules count for copy or duplicate component actions
return unless @single_rule_clone

component.rules_count = component.rules.where(deleted_at: nil).size
component.save
end
Expand Down

0 comments on commit 44e3217

Please sign in to comment.