aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/censor_rule.rb51
-rw-r--r--app/models/incoming_message.rb16
-rw-r--r--app/models/info_request.rb27
-rw-r--r--app/models/public_body.rb7
4 files changed, 58 insertions, 43 deletions
diff --git a/app/models/censor_rule.rb b/app/models/censor_rule.rb
index a477d2568..da3f49760 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,33 +29,59 @@ class CensorRule < ActiveRecord::Base
belongs_to :user
belongs_to :public_body
- def binary_replacement
- self.text.gsub(/./, 'x')
+ # 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 }
+ validates_presence_of :text
+
+ named_scope :global, {:conditions => {:info_request_id => nil,
+ :user_id => nil,
+ :public_body_id => nil}}
+
+ def require_user_request_or_public_body
+ 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
+
+ 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
- text.gsub!(self.text, self.replacement)
+ to_replace = regexp? ? self.make_regexp() : self.text
+ text.gsub!(to_replace, self.replacement)
end
+
def apply_to_binary!(binary)
if binary.nil?
return nil
end
- binary.gsub!(self.text, self.binary_replacement)
+ to_replace = regexp? ? self.make_regexp() : self.text
+ binary.gsub!(to_replace){ |match| match.gsub(/./, 'x') }
end
-
- def validate
- 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; ")
+ 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
diff --git a/app/models/incoming_message.rb b/app/models/incoming_message.rb
index 593590fb8..13fc316cd 100644
--- a/app/models/incoming_message.rb
+++ b/app/models/incoming_message.rb
@@ -375,25 +375,10 @@ class IncomingMessage < ActiveRecord::Base
# http://www.whatdotheyknow.com/request/common_purpose_training_graduate#incoming-774
text.gsub!(/(Mobile|Mob)([\s\/]*(Fax|Tel))*\s*:?[\s\d]*\d/, "[mobile number]")
- # Specific removals # XXX remove these and turn them into censor rules in database
- # http://www.whatdotheyknow.com/request/total_number_of_objects_in_the_n_6
- text.gsub!(/\*\*\*+\nPolly Tucker.*/ms, "")
- # http://www.whatdotheyknow.com/request/cctv_data_retention_and_use
- text.gsub!(/Andy 079.*/, "Andy [mobile number]")
- # http://www.whatdotheyknow.com/request/how_do_the_pct_deal_with_retirin_113
- text.gsub!(/(Complaints and Corporate Affairs Officer)\s+Westminster Primary Care Trust.+/ms, "\\1")
-
# Remove WhatDoTheyKnow signup links
domain = MySociety::Config.get('DOMAIN')
text.gsub!(/http:\/\/#{domain}\/c\/[^\s]+/, "[WDTK login link]")
- # Remove Home Office survey links
- # e.g. http://www.whatdotheyknow.com/request/serious_crime_act_2007_section_7#incoming-12650
- if self.info_request.public_body.url_name == 'home_office'
- text.gsub!(/Your password:-\s+[^\s]+/, '[password]')
- text.gsub!(/Password=[^\s]+/, '[password]')
- end
-
# Remove things from censor rules
self.info_request.apply_censor_rules_to_text!(text)
end
@@ -599,7 +584,6 @@ class IncomingMessage < ActiveRecord::Base
# Remove existing quoted sections
folded_quoted_text = self.remove_lotus_quoting(text, 'FOLDED_QUOTED_SECTION')
folded_quoted_text = IncomingMessage.remove_quoted_sections(text, "FOLDED_QUOTED_SECTION")
-
self.cached_main_body_text_unfolded = text
self.cached_main_body_text_folded = folded_quoted_text
self.save!
diff --git a/app/models/info_request.rb b/app/models/info_request.rb
index 7e69a5cda..a7d09401b 100644
--- a/app/models/info_request.rb
+++ b/app/models/info_request.rb
@@ -136,7 +136,7 @@ class InfoRequest < ActiveRecord::Base
else
fake_slug = external_user_name.parameterize
end
- public_body.url_name + "_"+fake_slug
+ (public_body.url_name || "") + "_" + fake_slug
else
user.url_name
end
@@ -995,27 +995,28 @@ public
return ret.reverse
end
+ # Get the list of censor rules that apply to this request
+ def applicable_censor_rules
+ applicable_rules = [self.censor_rules, self.public_body.censor_rules, CensorRule.global.all]
+ if self.user && !self.user.censor_rules.empty?
+ applicable_rules << self.user.censor_rules
+ end
+ return applicable_rules.flatten
+ end
+
# Call groups of censor rules
def apply_censor_rules_to_text!(text)
- for censor_rule in self.censor_rules
+ self.applicable_censor_rules.each do |censor_rule|
censor_rule.apply_to_text!(text)
end
- if self.user # requests during construction have no user
- for censor_rule in self.user.censor_rules
- censor_rule.apply_to_text!(text)
- end
- end
+ return text
end
def apply_censor_rules_to_binary!(binary)
- for censor_rule in self.censor_rules
+ self.applicable_censor_rules.each do |censor_rule|
censor_rule.apply_to_binary!(binary)
end
- if self.user # requests during construction have no user
- for censor_rule in self.user.censor_rules
- censor_rule.apply_to_binary!(binary)
- end
- end
+ return binary
end
def is_owning_user?(user)
diff --git a/app/models/public_body.rb b/app/models/public_body.rb
index 9efeadf55..60ecb2781 100644
--- a/app/models/public_body.rb
+++ b/app/models/public_body.rb
@@ -1,3 +1,4 @@
+# -*- coding: utf-8 -*-
# == Schema Information
#
# Table name: public_bodies
@@ -41,6 +42,7 @@ class PublicBody < ActiveRecord::Base
has_many :info_requests, :order => 'created_at desc'
has_many :track_things, :order => 'created_at desc'
+ has_many :censor_rules, :order => 'created_at desc'
has_tag_string
@@ -91,8 +93,9 @@ class PublicBody < ActiveRecord::Base
# Make sure publication_scheme gets the correct default value.
# (This would work automatically, were publication_scheme not a translated attribute)
self.publication_scheme = "" if self.publication_scheme.nil?
-
- # Set an API key if there isn’t one
+ end
+
+ def before_save
self.api_key = SecureRandom.base64(33) if self.api_key.nil?
end