Skip to content

Commit

Permalink
Fixes #37987 - Filters dont inherit taxonomy on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaAga committed Nov 6, 2024
1 parent 7b14f55 commit f46f2a5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 7 deletions.
17 changes: 16 additions & 1 deletion app/models/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,22 @@ def enforce_inherited_taxonomies
def inherit_taxonomies!
self.organization_ids = role.organization_ids if resource_taxable_by_organization?
self.location_ids = role.location_ids if resource_taxable_by_location?
build_taxonomy_search
build_taxonomy_search_from_ids
end

def build_taxonomy_search_from_ids
orgs = build_taxonomy_search_string_from_ids('organization')
locs = build_taxonomy_search_string_from_ids('location')

taxonomies = [orgs, locs].reject { |t| t.blank? }
self.taxonomy_search = taxonomies.join(' and ').presence
end

def build_taxonomy_search_string_from_ids(name)
return '' unless send("resource_taxable_by_#{name}?")
relation = send("#{name}_ids")
return '' if relation.empty?
parenthesize("#{name}_id ^ (#{relation.join(',')})")
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class RegenerateTaxonomySearchForFiltersWithOverrideFalse < ActiveRecord::Migration[6.1]
def change
filters = Filter.where(role_id: Role.where(origin: nil).or(Role.where(builtin: 2))).where(override: false).where(taxonomy_search: nil)

filters.each do |filter|
filter.build_taxonomy_search_from_ids
filter.update_column(:taxonomy_search, filter.taxonomy_search)
end
end
end
11 changes: 10 additions & 1 deletion test/models/filter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,17 @@ class FilterTest < ActiveSupport::TestCase

test 'saving nilifies empty taxonomy search' do
f = FactoryBot.build(:filter, :resource_type => 'Domain')
f.role = FactoryBot.build(:role, :organizations => [FactoryBot.build(:organization)])
f.role = FactoryBot.build(:role)
f.save
assert_nil f.taxonomy_search
end

test 'creating a filter inherits taxonomies from the role' do
f = FactoryBot.create(:filter, :resource_type => 'Domain')
organization_test = FactoryBot.create(:organization)
location_test = FactoryBot.create(:location)
f.role = FactoryBot.create(:role, :organizations => [organization_test], :locations => [location_test])
f.save
assert_equal f.taxonomy_search(), "(organization_id ^ (#{organization_test.id})) and (location_id ^ (#{location_test.id}))"
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect, useMemo } from 'react';
import PropTypes from 'prop-types';
import { useDispatch } from 'react-redux';
import {
Expand Down Expand Up @@ -39,14 +39,26 @@ export const FiltersForm = ({ roleName, roleId, isNew, data, history }) => {
show_organizations: showOrgs = false,
show_locations: showLocations = false,
} = type;

const isTaxonomySearch = useMemo(
() => chosenOrgs.length || chosenLocations.length,
[chosenOrgs.length, chosenLocations.length]
);

useEffect(() => {
if (isTaxonomySearch) {
setIsUnlimited(false);
}
}, [isTaxonomySearch]);
const dispatch = useDispatch();
const [autocompleteQuery, setAutocompleteQuery] = useState(data.search || '');
const submit = async () => {
const params = {
filter: {
role_id: role,
search: isUnlimited ? null : autocompleteQuery,
unlimited: isUnlimited,
unlimited:
isUnlimited || (!autocompleteQuery.length && !isTaxonomySearch),
override: isOverride,
permission_ids: chosenPermissions,
organization_ids: chosenOrgs,
Expand Down Expand Up @@ -163,8 +175,20 @@ export const FiltersForm = ({ roleName, roleId, isNew, data, history }) => {
defaultLocations={data.locations}
/>
)}

{isGranular ? (
<>
{!!isTaxonomySearch && (
<>
<Alert
ouiaId="taxonomy-search-alert"
variant="info"
title={__(
"The filter is scoped to the selected organizations and locations, therefore can't be unlimited"
)}
/>
</>
)}
<FormGroup
label={__('Unlimited?')}
labelIcon={
Expand All @@ -190,11 +214,12 @@ export const FiltersForm = ({ roleName, roleId, isNew, data, history }) => {
>
<Checkbox
ouiaId="unlimited-checkbox"
isChecked={isUnlimited}
isChecked={isTaxonomySearch ? isUnlimited : false}
onChange={checked => {
setAutocompleteQuery('');
setIsUnlimited(checked);
}}
isDisabled={isTaxonomySearch}
aria-label="is unlimited"
id="unlimited-check"
name="unlimited"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { FiltersForm } from './FiltersForm';
const NewFiltersFormPage = ({ location: { search }, history }) => {
const { role_id: urlRoleId } = URI.parseQuery(search);
const {
response: { name: roleName, id: roleId },
response: { name: roleName, id: roleId, locations, organizations },
} = useAPI('get', `/api/v2/roles/${urlRoleId}`);
const breadcrumbOptions = {
breadcrumbItems: [
Expand All @@ -29,7 +29,7 @@ const NewFiltersFormPage = ({ location: { search }, history }) => {
isNew
roleName={roleName || ''}
roleId={urlRoleId}
data={{}}
data={{ locations, organizations }}
history={history}
/>
) : (
Expand Down

0 comments on commit f46f2a5

Please sign in to comment.