From 088bc961328f4d876971994102cde52c1ad49246 Mon Sep 17 00:00:00 2001 From: Seb Bacon Date: Thu, 12 Jul 2012 13:04:27 +0100 Subject: Support regular expressions in CensorRules; also support 'global' CensorRules that aren't attached to a User or Request or Public Body (but don't expose this in the admin UI). Fixes #33 --- app/models/censor_rule.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'app/models/censor_rule.rb') diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb index a477d2568..cedbd767e 100644 --- a/app/models/censor_rule.rb +++ b/app/models/censor_rule.rb @@ -9,6 +9,7 @@ # public_body_id :integer # text :text not null # replacement :text not null +# regexp :boolean # last_edit_editor :string(255) not null # last_edit_comment :text not null # created_at :datetime not null @@ -28,6 +29,8 @@ class CensorRule < ActiveRecord::Base belongs_to :user belongs_to :public_body + named_scope :regexps, {:conditions => {:regexp => true}} + def binary_replacement self.text.gsub(/./, 'x') end @@ -36,8 +39,10 @@ class CensorRule < ActiveRecord::Base if text.nil? return nil end - text.gsub!(self.text, self.replacement) + to_replace = regexp? ? Regexp.new(self.text, Regexp::MULTILINE) : self.text + text.gsub!(to_replace, self.replacement) end + def apply_to_binary!(binary) if binary.nil? return nil @@ -45,9 +50,8 @@ class CensorRule < ActiveRecord::Base binary.gsub!(self.text, self.binary_replacement) end - def validate - if self.info_request.nil? && self.user.nil? && self.public_body.nil? + if !self.regexp? && self.info_request.nil? && self.user.nil? && self.public_body.nil? errors.add("Censor must apply to an info request a user or a body; ") end end -- cgit v1.2.3 From 5ad7ebf25cc684d68325681db4471e522ac31569 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 14 Aug 2012 11:20:51 +0100 Subject: Add concept of global censor rules as orthogonal to regex censor rules. Apply global rules to every request, not regex rules. --- app/models/censor_rule.rb | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'app/models/censor_rule.rb') diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb index cedbd767e..4548a3a71 100644 --- a/app/models/censor_rule.rb +++ b/app/models/censor_rule.rb @@ -29,7 +29,19 @@ class CensorRule < ActiveRecord::Base belongs_to :user belongs_to :public_body - named_scope :regexps, {:conditions => {:regexp => true}} + # a flag to allow the require_user_request_or_public_body validation to be skipped + attr_accessor :allow_global + validate :require_user_request_or_public_body, :unless => proc{ |rule| rule.allow_global == true } + + named_scope :global, {:conditions => {:info_request_id => nil, + :user_id => nil, + :public_body_id => nil}} + + def require_user_request_or_public_body + if !self.regexp? && self.info_request.nil? && self.user.nil? && self.public_body.nil? + errors.add("Censor must apply to an info request a user or a body; ") + end + end def binary_replacement self.text.gsub(/./, 'x') @@ -50,15 +62,15 @@ class CensorRule < ActiveRecord::Base binary.gsub!(self.text, self.binary_replacement) end - def validate - if !self.regexp? && self.info_request.nil? && self.user.nil? && self.public_body.nil? - errors.add("Censor must apply to an info request a user or a body; ") + def for_admin_column + self.class.content_columns.each do |column| + yield(column.human_name, self.send(column.name), column.type.to_s, column.name) end end - def for_admin_column - self.class.content_columns.each do |column| - yield(column.human_name, self.send(column.name), column.type.to_s, column.name) + def is_global? + return true if (info_request_id.nil? && user_id.nil? && public_body_id.nil?) + return false end - end + end -- cgit v1.2.3 From c6d5020a9a48226feaccd856df0c8f4584d5fd08 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 14 Aug 2012 14:14:35 +0100 Subject: Handle regexp rules when running censor rules on binary files. --- app/models/censor_rule.rb | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'app/models/censor_rule.rb') diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb index 4548a3a71..4e96dc55a 100644 --- a/app/models/censor_rule.rb +++ b/app/models/censor_rule.rb @@ -43,10 +43,6 @@ class CensorRule < ActiveRecord::Base end end - def binary_replacement - self.text.gsub(/./, 'x') - end - def apply_to_text!(text) if text.nil? return nil @@ -59,7 +55,8 @@ class CensorRule < ActiveRecord::Base if binary.nil? return nil end - binary.gsub!(self.text, self.binary_replacement) + to_replace = regexp? ? Regexp.new(self.text, Regexp::MULTILINE) : self.text + binary.gsub!(to_replace){ |match| match.gsub(/./, 'x') } end def for_admin_column -- cgit v1.2.3 From 2aa3d7bfd76e774555207a81cd5e39513f965bf1 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 14 Aug 2012 15:29:45 +0100 Subject: Make global validation work correctly for regex censor rules. --- app/models/censor_rule.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/models/censor_rule.rb') diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb index 4e96dc55a..02b85dc70 100644 --- a/app/models/censor_rule.rb +++ b/app/models/censor_rule.rb @@ -38,7 +38,7 @@ class CensorRule < ActiveRecord::Base :public_body_id => nil}} def require_user_request_or_public_body - if !self.regexp? && self.info_request.nil? && self.user.nil? && self.public_body.nil? + if self.info_request.nil? && self.user.nil? && self.public_body.nil? errors.add("Censor must apply to an info request a user or a body; ") end end -- cgit v1.2.3 From 21082cc55d3d61edce660ea7a73ec80380359e2f Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 14 Aug 2012 16:31:03 +0100 Subject: Add basic validation for regexp censor rules that a valid regexp can be created with the text of the rule. --- app/models/censor_rule.rb | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'app/models/censor_rule.rb') diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb index 02b85dc70..3a15bf532 100644 --- a/app/models/censor_rule.rb +++ b/app/models/censor_rule.rb @@ -32,6 +32,7 @@ class CensorRule < ActiveRecord::Base # a flag to allow the require_user_request_or_public_body validation to be skipped attr_accessor :allow_global validate :require_user_request_or_public_body, :unless => proc{ |rule| rule.allow_global == true } + validate :require_valid_regexp, :if => proc{ |rule| rule.regexp? == true } named_scope :global, {:conditions => {:info_request_id => nil, :user_id => nil, @@ -43,11 +44,23 @@ class CensorRule < ActiveRecord::Base end end + def require_valid_regexp + begin + self.make_regexp() + rescue RegexpError => e + errors.add(:text, e.message) + end + end + + def make_regexp + return Regexp.new(self.text, Regexp::MULTILINE) + end + def apply_to_text!(text) if text.nil? return nil end - to_replace = regexp? ? Regexp.new(self.text, Regexp::MULTILINE) : self.text + to_replace = regexp? ? self.make_regexp() : self.text text.gsub!(to_replace, self.replacement) end @@ -55,7 +68,7 @@ class CensorRule < ActiveRecord::Base if binary.nil? return nil end - to_replace = regexp? ? Regexp.new(self.text, Regexp::MULTILINE) : self.text + to_replace = regexp? ? self.make_regexp() : self.text binary.gsub!(to_replace){ |match| match.gsub(/./, 'x') } end -- cgit v1.2.3 From 495a29cd593ac0a270ebb3bf1c1ff85f03b52e31 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 14 Aug 2012 16:41:52 +0100 Subject: Validate presence of text to replace in censor rules. --- app/models/censor_rule.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'app/models/censor_rule.rb') diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb index 3a15bf532..da3f49760 100644 --- a/app/models/censor_rule.rb +++ b/app/models/censor_rule.rb @@ -33,6 +33,7 @@ class CensorRule < ActiveRecord::Base attr_accessor :allow_global validate :require_user_request_or_public_body, :unless => proc{ |rule| rule.allow_global == true } validate :require_valid_regexp, :if => proc{ |rule| rule.regexp? == true } + validates_presence_of :text named_scope :global, {:conditions => {:info_request_id => nil, :user_id => nil, -- cgit v1.2.3