@@ -3,18 +3,20 @@ module MassAssignmentSecurity
3
3
class Sanitizer
4
4
# Returns all attributes not denied by the authorizer.
5
5
def sanitize ( attributes , authorizer )
6
- attributes . reject do |attr , value |
7
- if authorizer . deny? ( attr )
8
- process_removed_attribute ( attr )
9
- true
10
- end
11
- end
6
+ sanitized_attributes = attributes . reject { |key , value | authorizer . deny? ( key ) }
7
+ debug_protected_attribute_removal ( attributes , sanitized_attributes )
8
+ sanitized_attributes
12
9
end
13
10
14
11
protected
15
12
16
- def process_removed_attribute ( attr )
17
- raise NotImplementedError , "#process_removed_attribute(attr) suppose to be overwritten"
13
+ def debug_protected_attribute_removal ( attributes , sanitized_attributes )
14
+ removed_keys = attributes . keys - sanitized_attributes . keys
15
+ process_removed_attributes ( removed_keys ) if removed_keys . any?
16
+ end
17
+
18
+ def process_removed_attributes ( attrs )
19
+ raise NotImplementedError , "#process_removed_attributes(attrs) suppose to be overwritten"
18
20
end
19
21
end
20
22
@@ -32,8 +34,8 @@ def logger?
32
34
@target . respond_to? ( :logger ) && @target . logger
33
35
end
34
36
35
- def process_removed_attribute ( attr )
36
- logger . warn "Can't mass-assign protected attribute : #{ attr } " if logger?
37
+ def process_removed_attributes ( attrs )
38
+ logger . warn "Can't mass-assign protected attributes : #{ attrs . join ( ', ' ) } " if logger?
37
39
end
38
40
end
39
41
@@ -42,19 +44,19 @@ def initialize(target = nil)
42
44
super ( )
43
45
end
44
46
45
- def process_removed_attribute ( attr )
46
- return if insensitive_attributes . include? ( attr )
47
- raise ActiveModel ::MassAssignmentSecurity ::Error . new ( attr )
47
+ def process_removed_attributes ( attrs )
48
+ return if ( attrs - insensitive_attributes ) . empty?
49
+ raise ActiveModel ::MassAssignmentSecurity ::Error . new ( attrs )
48
50
end
49
51
50
52
def insensitive_attributes
51
- @insensitive_attributes ||= [ 'id' ]
53
+ [ 'id' ]
52
54
end
53
55
end
54
56
55
57
class Error < StandardError
56
- def initialize ( attr )
57
- super ( "Can't mass-assign protected attribute : #{ attr } " )
58
+ def initialize ( attrs )
59
+ super ( "Can't mass-assign protected attributes : #{ attrs . join ( ', ' ) } " )
58
60
end
59
61
end
60
62
end
0 commit comments