Project

General

Profile

« Previous | Next » 

Revision 12612809

Added by Dominic Cleal almost 8 years ago

fixes #3917 - replace protected_attrs with strong parameters

Filtering of attributes has moved from the protected_attributes gem to
strong parameters in controller concerns, to be in line with current
Rails recommendations.

Concerns are shared between UI and both API controllers and list the
attributes using Foreman::ParameterFilter, which provides additional
features:

1. Registration of additional attributes from plugins, through the
plugin API or `attr_accessible` for short term compatibility.
2. Re-use of permitted attribute lists for nested models (e.g. host
and interface).
3. Combining of lists of attributes from all sources in a single
permit call.
4. A small DSL for changing accepted parameters based on controller,
action and UI/API type.

Plugins should either temporarily depend on protected_attributes to
continue to protect their models or call permit/use ParameterFilter
similarly: http://projects.theforeman.org/projects/foreman/wiki/Strong_parameters

Some UI changes were required to make nested model hash keys all
integers instead of "new_123456" etc, else strong parameters would
filter the entries out.

The Role model's builtin default has been moved from initialize to the
database as the removal of protected_attrs changed the initialisation
order.

View differences:

app/models/subnet.rb
include Parameterizable::ByIdName
include Exportable
attr_accessible :name, :type, :network, :mask, :gateway, :dns_primary, :dns_secondary, :ipam, :from,
:to, :vlanid, :boot_mode, :dhcp_id, :dhcp, :tftp_id, :tftp, :dns_id, :dns, :domain_ids, :domain_names,
:subnet_parameters_attributes, :cidr, :network_type
attr_exportable :name, :network, :mask, :gateway, :dns_primary, :dns_secondary, :from, :to, :boot_mode,
:ipam, :vlanid, :network_type
# This casts Subnet to Subnet::Ipv4 if no type is set
def self.new(*attributes, &block)
type = attributes.first.with_indifferent_access.delete(:type) if attributes.first.is_a?(Hash)
return Subnet::Ipv4.new_without_cast(*attributes, &block) if self == Subnet && type.nil?
super
end
# This sets the rails model name of all child classes to the
# model name of the parent class, i.e. Subnet.
# This is necessary for all STI classes to share the same
......
super
end
audited :allow_mass_assignment => true
audited
validates_lengths_from_database :except => [:gateway]
before_destroy EnsureNotUsedBy.new(:hosts, :hostgroups, :interfaces, :domains)
......
Subnet.all.detect {|s| s.family == ip.family && s.contains?(ip)}
end
# This casts Subnet to Subnet::Ipv4 if no type is set
def new_with_default_type(*attributes, &block)
type = attributes.first.with_indifferent_access.delete(:type) if attributes.first.is_a?(Hash)
return Subnet::Ipv4.new_without_cast(*attributes, &block) if self == Subnet && type.nil?
new_without_default_type(*attributes, &block)
end
alias_method_chain :new, :default_type
# allows to create a specific subnet class based on the network_type.
# network_type is more user friendly than the class names
def new_network_type(args)

Also available in: Unified diff