Skip to content

AM::MassAssingmentSecurity: improve performance #5431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions activemodel/lib/active_model/mass_assignment_security/sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ module MassAssignmentSecurity
class Sanitizer
# Returns all attributes not denied by the authorizer.
def sanitize(attributes, authorizer)
sanitized_attributes = attributes.reject { |key, value| authorizer.deny?(key) }
debug_protected_attribute_removal(attributes, sanitized_attributes)
sanitized_attributes
attributes.reject do |attr, value|
if authorizer.deny?(attr)
process_removed_attribute(attr)
true
end
end
end

protected

def debug_protected_attribute_removal(attributes, sanitized_attributes)
removed_keys = attributes.keys - sanitized_attributes.keys
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"
def process_removed_attribute(attr)
raise NotImplementedError, "#process_removed_attribute(attr) suppose to be overwritten"
end
end

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

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

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

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

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

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

class CustomSanitizer < ActiveModel::MassAssignmentSecurity::Sanitizer

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

Expand Down