/
censor_rule.rb
125 lines (108 loc) · 3.2 KB
/
censor_rule.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# == Schema Information
# Schema version: 20210114161442
#
# Table name: censor_rules
#
# id :integer not null, primary key
# info_request_id :integer
# user_id :integer
# public_body_id :integer
# text :text not null
# replacement :text not null
# last_edit_editor :string not null
# last_edit_comment :text not null
# created_at :datetime not null
# updated_at :datetime not null
# regexp :boolean
#
# models/censor_rule.rb:
# Stores alterations to remove specific data from requests.
#
# Copyright (c) 2008 UK Citizens Online Democracy. All rights reserved.
# Email: hello@mysociety.org; WWW: http://www.mysociety.org/
class CensorRule < ApplicationRecord
include AdminColumn
belongs_to :info_request,
:inverse_of => :censor_rules
belongs_to :user,
:inverse_of => :censor_rules
belongs_to :public_body,
:inverse_of => :censor_rules
validate :require_valid_regexp, :if => proc { |rule| rule.regexp? == true }
validates_presence_of :text,
:replacement,
:last_edit_comment,
:last_edit_editor
scope :global, -> {
where(info_request_id: nil,
user_id: nil,
public_body_id: nil)
}
def apply_to_text(text_to_censor)
return nil if text_to_censor.nil?
text_to_censor.gsub(to_replace('UTF-8'), replacement)
end
def apply_to_binary(binary_to_censor)
return nil if binary_to_censor.nil?
binary_to_censor.gsub(to_replace(binary_to_censor.encoding)) do |match|
match.gsub(single_char_regexp) { |m| 'x' * m.bytesize }
end
end
def is_global?
info_request_id.nil? && user_id.nil? && public_body_id.nil?
end
def expire_requests
if info_request
info_request.expire
elsif user
user.expire_requests
elsif public_body
public_body.expire_requests
else # global rule
InfoRequest.find_in_batches do |group|
group.each { |request| request.expire }
end
end
end
def censorable_requests
if info_request
# Prefer a chainable query instead of wrapping in Array for similar API
# between CensorRule types
InfoRequest.where(id: info_request_id)
elsif user
user.info_requests
elsif public_body
public_body.info_requests
else
InfoRequest.unscoped
end
end
private
def single_char_regexp
if String.method_defined?(:encode)
Regexp.new('.'.force_encoding('ASCII-8BIT'))
else
Regexp.new('.', nil, 'N')
end
end
def require_valid_regexp
begin
make_regexp('UTF-8')
rescue RegexpError => e
errors.add(:text, e.message)
end
end
def to_replace(encoding)
regexp? ? make_regexp(encoding) : encoded_text(encoding)
end
def encoded_text(encoding)
String.method_defined?(:encode) ? text.dup.force_encoding(encoding) : text
end
def make_regexp(encoding)
::Warning.with_raised_warnings do
Regexp.new(encoded_text(encoding), Regexp::MULTILINE)
end
rescue RaisedWarning => e
raise RegexpError, e.message.split('warning: ').last.chomp
end
end