Skip to content

Commit

Permalink
AM::MassAssingmentSecurity: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdan committed Mar 14, 2012
1 parent 29094c0 commit 7d1379f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
34 changes: 16 additions & 18 deletions activemodel/lib/active_model/mass_assignment_security/sanitizer.rb
Expand Up @@ -3,20 +3,18 @@ module MassAssignmentSecurity
class Sanitizer class Sanitizer
# Returns all attributes not denied by the authorizer. # Returns all attributes not denied by the authorizer.
def sanitize(attributes, authorizer) def sanitize(attributes, authorizer)
sanitized_attributes = attributes.reject { |key, value| authorizer.deny?(key) } attributes.reject do |attr, value|
debug_protected_attribute_removal(attributes, sanitized_attributes) if authorizer.deny?(attr)
sanitized_attributes process_removed_attribute(attr)
true
end
end
end end


protected protected


def debug_protected_attribute_removal(attributes, sanitized_attributes) def process_removed_attribute(attr)
removed_keys = attributes.keys - sanitized_attributes.keys raise NotImplementedError, "#process_removed_attribute(attr) suppose to be overwritten"
process_removed_attributes(removed_keys) if removed_keys.any?
end

def process_removed_attributes(attrs)
raise NotImplementedError, "#process_removed_attributes(attrs) suppose to be overwritten"
end end
end end


Expand All @@ -34,8 +32,8 @@ def logger?
@target.respond_to?(:logger) && @target.logger @target.respond_to?(:logger) && @target.logger
end end


def process_removed_attributes(attrs) def process_removed_attribute(attr)
logger.warn "Can't mass-assign protected attributes: #{attrs.join(', ')}" if logger? logger.warn "Can't mass-assign protected attribute: #{attr}" if logger?
end end
end end


Expand All @@ -44,19 +42,19 @@ def initialize(target = nil)
super() super()
end end


def process_removed_attributes(attrs) def process_removed_attribute(attr)
return if (attrs - insensitive_attributes).empty? return if insensitive_attributes.include?(attr)
raise ActiveModel::MassAssignmentSecurity::Error.new(attrs) raise ActiveModel::MassAssignmentSecurity::Error.new(attr)
end end


def insensitive_attributes def insensitive_attributes
['id'] @insensitive_attributes ||= ['id']
end end
end end


class Error < StandardError class Error < StandardError
def initialize(attrs) def initialize(attr)
super("Can't mass-assign protected attributes: #{attrs.join(', ')}") super("Can't mass-assign protected attribute: #{attr}")
end end
end end
end end
Expand Down
2 changes: 1 addition & 1 deletion activemodel/test/cases/mass_assignment_security_test.rb
Expand Up @@ -4,7 +4,7 @@


class CustomSanitizer < ActiveModel::MassAssignmentSecurity::Sanitizer class CustomSanitizer < ActiveModel::MassAssignmentSecurity::Sanitizer


def process_removed_attributes(attrs) def process_removed_attribute(attr)
raise StandardError raise StandardError
end end


Expand Down

0 comments on commit 7d1379f

Please sign in to comment.