Plugins - AttrLocked
Add to favoritesAttrLocked
AttrLocked is a little Rails plugin that provides extra security for your ActiveRecord models. It lets you specify, at the model level, that certain model attributes should never be allowed to change. Perfect for usernames or financial record-keeping.
In the model
Let’s take an example: your application has users, who each have a username. Once created, that username should never change. AttrLocked lets you specify this with a single line:
class User < ActiveRecord::Base
attr_locked :username
end
Now, when you create a new User, you can of course assign it a username. But once the record has been saved (specifically, when new_record? returns false) you cannot reassign it. This is best illustrated with a little code:
foo = User.new(:username => 'Bob')
foo.save
bob = User.find_by_username('Bob')
bob.username
#=> "Bob"
bob.username = 'Mike'
#=> "Mike"
bob.username
#=> "Bob"
bob.update_attribute(:username, 'Freddie')
#=> false
bob.username
#=> "Bob"
So, you are unable to assign using username= (or indeed using attributes=). save, update_attributes and attributes= will silently ignore the locked attributes, just as they do for attr_protected. update_attribute will return false if you use it with a locked attribute.
In the view
To make sure people don’t waste their time filling out forms, any form input that’s linked to a locked attribute will be disabled automatically, unless you’re creating a new record. So, you don’t need to go around putting :disabled => !object.new_record? everywhere. Helpful, no?
Locking whole tables
You might have some database tables that are read-only. For example, there might be a table that stores a set of third-party geographic data that’s used by your application. You can safeguard such tables against changes by using:
class Location < ActiveRecord::Base
table_locked
end
With that in place, you cannot modify the properties of any Location instances, even new ones not yet saved to the database. You cannot save, create, update or destroy (or delete_all) any records from the table.
Associations
You needn’t worry that locking records or tables means they cannot be associated with anything. Using our geographic data example, we could have:
# This table is for reference only
class Town < ActiveRecord::Base
table_locked
has_many :users
end
class User < ActiveRecord::Base
attr_locked :username
belongs_to :town
end
With this setup, we can still modify associations as usual:
town = Town.find(:first) user = User.find(:first) # Saves immediately town.users = [user] # As does this town.users << user # This works too user.town = town user.save
License
Copyright © 2007 James Coglan
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.
http://blog.jcoglan.com/attrlocked/
http://svn.jcoglan.com/attrlocked/trunk/attr_locked
Rails' (MIT)
Security
