Safe Mass Assignment
Allows (careful) mass assignment of protected attributes, separate from values provided via users of your application.
When creating models via ActiveRecord, it is good practice to use attr_protected or (even better) attr_accessible to prevent malicious (or clueless) users from assigning values to attributes that you did not intend to be assignable via the web interface. Unfortunately, this results in clumsy situations where you need your program to be able to assign to those attributes:
user.attributes = params[:user] user.admin = true user.save
This plugin adds a second parameter to create, update_attributes, and related, which will always mass-assign, regardless of attr_protected or attr_accessible:
user.update_attributes(params[:user], :admin => true)
Naturally, clueless programmers will still be able to make a muck of this, but as long as you ensure that the second parameter is never using values provided by the user, you'll be just as secure as if you were using the first (more verbose) version.
Account.create(params[:account], :comped => true) Account.create!(params[:account], :comped => true) account.update_attributes(params[:account], :comped => true) account.update_attributes!(params[:account], :comped => true) account.users.create(params[:user], :admin => true) account.users.create!(params[:user], :admin => true)
(The MIT License)
Copyright © 2009 Jamis Buck <firstname.lastname@example.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.