Smorgasbord - Politics, Lisp, Rails, Fencing, etc.

My musings on assortment of things ranging from politics, computer technology and programming to sports.

Thursday, March 09, 2006


On this day:

Rails User Engine - Adding permissions

A while back, there was discussion on the engines mailing list on how to add permissions for different roles in the database for the user engine.

My preferred way of doing this is using migrations. I will illustrate it here using an example, say we have a controller about with actions ["terms", "privacy", "faq", "about"] and we want to give permissions to both Guest and User roles to have access to this.

So, to write a migration for this, fire up a shell and type:


ruby script\generate migration AddPermissionsData



This will create a new migration file in the `db\migrate' folder:



class AddPermissionsData < ActiveRecord::Migration
def self.up
end

def self.down
end
end



Now, to add permissions modify the above file, and add the following code:



class AddPermissionsData < ActiveRecord::Migration
def self.up
guest_role = Role.find_by_name("Guest")
user_role = Role.find_by_name("User")
controller_actions = {:about => ["terms", "privacy",
"faq", "about"] }
controller_actions.each do |controller,actions|
for action in actions
permission = Permission.find_by_controller_and_action(
controller.to_s,action)
if permission
permission.roles << guest_role
permission.roles << user_role
end
end
end
end

def self.down
guest_role = Role.find_by_name("Guest")
user_role = Role.find_by_name("User")
controller_actions = {:about => ["terms", "privacy",
"faq", "about"]}
controller_actions.each do |controller,actions|
for action in actions
permission = Permission.find_by_controller_and_action(
controller.to_s,action)
if permission
permission.roles.delete guest_role
permission.roles.delete user_role
end
end
end
end
end




The code in self.down should remove whatever was added in self.up.

Important: Before running the above migration, don't forget to run rake sync_permissions.

If you know of a better approach of doing the above then please post it as a comment.

8 Comments:

At Wed May 17, 03:06:00 AM 2006, Blogger Surendra Singhi said...

Instead of running 'rake sync' put 'Permission.sync' in the `self.up' method before adding permissions.

 
At Wed May 17, 03:41:00 AM 2006, Blogger Surendra Singhi said...

Also, one can use:

ActiveRecord::Base.connection.transaction do

..all the code adding permissions..

end


so that migrations are rolled back if something fails.

 
At Sun Sep 13, 09:07:00 PM 2009, Anonymous Anonymous said...

酒店打工

酒店兼職

台北酒店

打工兼差

酒店工作

酒店經紀

禮服店

酒店兼差

酒店上班

酒店PT

酒店

酒店喝酒

酒店消費

喝花酒

粉味

 
At Fri Jul 08, 05:20:00 PM 2016, Blogger ninest123 said...

air max, sac longchamp, ray ban sunglasses, longchamp pas cher, ugg boots, ray ban sunglasses, nike free, oakley sunglasses, nike free, chanel handbags, cheap oakley sunglasses, nike outlet, louboutin, longchamp, nike roshe run, air jordan pas cher, tiffany jewelry, longchamp outlet, longchamp outlet, michael kors, prada outlet, louis vuitton outlet, louboutin outlet, louis vuitton, uggs on sale, ugg boots, louis vuitton, polo ralph lauren outlet, oakley sunglasses, louis vuitton, kate spade outlet, louboutin pas cher, polo ralph lauren outlet, tory burch outlet, tiffany and co, oakley sunglasses, ray ban sunglasses, ralph lauren pas cher, christian louboutin outlet, nike air max, nike air max, louboutin shoes, replica watches, jordan shoes, oakley sunglasses, gucci outlet, prada handbags, burberry, replica watches, louis vuitton outlet

 
At Fri Jul 08, 05:24:00 PM 2016, Blogger ninest123 said...

ray ban uk, vanessa bruno, sac guess, air force, tn pas cher, coach outlet, michael kors, burberry outlet online, nike blazer, lululemon, vans pas cher, mulberry, michael kors, burberry, nike free run uk, nike roshe, ray ban pas cher, michael kors outlet, hollister pas cher, hollister, north face, hermes, new balance pas cher, coach purses, true religion outlet, nike air max, replica handbags, ugg boots, michael kors outlet, hogan, nike air max, true religion jeans, abercrombie and fitch, kate spade handbags, lacoste pas cher, true religion jeans, michael kors outlet, converse pas cher, coach outlet, timberland, nike air max, michael kors, ugg boots, michael kors outlet, true religion jeans, north face, michael kors, ralph lauren uk, oakley pas cher, michael kors outlet

 
At Fri Jul 08, 05:35:00 PM 2016, Blogger ninest123 said...

ray ban uk, vanessa bruno, sac guess, air force, tn pas cher, coach outlet, michael kors, burberry outlet online, nike blazer, lululemon, vans pas cher, mulberry, michael kors, burberry, nike free run uk, nike roshe, ray ban pas cher, michael kors outlet, hollister pas cher, hollister, north face, hermes, new balance pas cher, coach purses, true religion outlet, nike air max, replica handbags, ugg boots, michael kors outlet, hogan, nike air max, true religion jeans, abercrombie and fitch, kate spade handbags, lacoste pas cher, true religion jeans, michael kors outlet, converse pas cher, coach outlet, timberland, nike air max, michael kors, ugg boots, michael kors outlet, true religion jeans, north face, michael kors, ralph lauren uk, oakley pas cher, michael kors outlet

 
At Fri Jul 08, 05:38:00 PM 2016, Blogger ninest123 said...

wedding dresses, giuseppe zanotti, instyler, mont blanc, converse outlet, longchamp, babyliss, north face outlet, north face outlet, insanity workout, gucci, vans, hollister, asics running shoes, chi flat iron, ralph lauren, baseball bats, bottega veneta, timberland boots, mac cosmetics, valentino shoes, abercrombie and fitch, mcm handbags, hollister, lululemon, nike air max, hollister, celine handbags, herve leger, ghd, louboutin, soccer jerseys, p90x workout, beats by dre, vans shoes, ray ban, nike air max, nike trainers, new balance, ferragamo shoes, nike huarache, reebok shoes, lancel, nfl jerseys, nike roshe, oakley, iphone cases, jimmy choo shoes, birkin bag, soccer shoes

 
At Fri Jul 08, 05:51:00 PM 2016, Blogger ninest123 said...

bottes ugg, ugg pas cher, louis vuitton, pandora jewelry, moncler, moncler, doudoune canada goose, replica watches, louis vuitton, moncler, juicy couture outlet, moncler, juicy couture outlet, ugg,uggs,uggs canada, canada goose, ugg,ugg australia,ugg italia, pandora charms, canada goose, links of london, canada goose outlet, pandora charms, canada goose uk, canada goose, marc jacobs, moncler, wedding dresses, louis vuitton, canada goose outlet, karen millen, louis vuitton, moncler outlet, sac louis vuitton pas cher, coach outlet, moncler, swarovski, ugg boots uk, toms shoes, moncler, pandora jewelry, canada goose, montre pas cher, thomas sabo, supra shoes, hollister, swarovski crystal

 

Post a Comment

Links to this post:

Create a Link

<< Home