From f79555ecc63ad672b233b214469b0a9a10046660 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Mon, 2 Sep 2019 18:20:32 +0200 Subject: [PATCH] Fixed issue #2728 - Put _type, _doc and _id to reserved words for object managaer becasue of elasticssearch internal use. --- app/models/object_manager/attribute.rb | 4 ++-- spec/models/object_manager/attribute_spec.rb | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/models/object_manager/attribute.rb b/app/models/object_manager/attribute.rb index 031102816..59df0a108 100644 --- a/app/models/object_manager/attribute.rb +++ b/app/models/object_manager/attribute.rb @@ -899,13 +899,13 @@ is certain attribute used by triggers, overviews or schedulers def check_name return if !name - raise 'Name can\'t get used, *_id and *_ids are not allowed' if name.match?(/_(id|ids)$/i) || name.match?(/^id$/i) + raise 'Name can\'t get used, *_id and *_ids are not allowed' if name.match?(/.+?_(id|ids)$/i) raise 'Spaces in name are not allowed' if name.match?(/\s/) raise 'Only letters from a-z, numbers from 0-9, and _ are allowed' if !name.match?(/^[a-z0-9_]+$/) raise 'At least one letters is needed' if !name.match?(/[a-z]/) # do not allow model method names as attributes - reserved_words = %w[destroy true false integer select drop create alter index table varchar blob date datetime timestamp url icon initials avatar permission validate subscribe unsubscribe translate search] + reserved_words = %w[destroy true false integer select drop create alter index table varchar blob date datetime timestamp url icon initials avatar permission validate subscribe unsubscribe translate search _type _doc _id id] raise "#{name} is a reserved word, please choose a different one" if name.match?(/^(#{reserved_words.join('|')})$/) # fixes issue #2236 - Naming an attribute "attribute" causes ActiveRecord failure diff --git a/spec/models/object_manager/attribute_spec.rb b/spec/models/object_manager/attribute_spec.rb index 5be19f6a4..a6c41b29d 100644 --- a/spec/models/object_manager/attribute_spec.rb +++ b/spec/models/object_manager/attribute_spec.rb @@ -57,7 +57,7 @@ RSpec.describe ObjectManager::Attribute, type: :model do end.to raise_error 'attribute is a reserved word, please choose a different one' end - %w[destroy true false integer select drop create alter index table varchar blob date datetime timestamp url icon initials avatar permission validate subscribe unsubscribe translate search].each do |reserved_word| + %w[destroy true false integer select drop create alter index table varchar blob date datetime timestamp url icon initials avatar permission validate subscribe unsubscribe translate search _type _doc _id id].each do |reserved_word| it "rejects Zammad reserved word '#{reserved_word}'" do expect do ObjectManager::Attribute.add attributes_for :object_manager_attribute_text, name: reserved_word @@ -65,6 +65,14 @@ RSpec.describe ObjectManager::Attribute, type: :model do end end + %w[someting_id something_ids].each do |reserved_word| + it "rejects word '#{reserved_word}' which is used for database references" do + expect do + ObjectManager::Attribute.add attributes_for :object_manager_attribute_text, name: reserved_word + end.to raise_error "Name can't get used, *_id and *_ids are not allowed" + end + end + it 'rejects duplicate attribute name of conflicting types' do attribute = attributes_for :object_manager_attribute_text ObjectManager::Attribute.add attribute