I’ve just released an early-alpha version of secure_rails (http://github.com/alexbartlow/secure_rails), a way of collecting the cross-cutting concern of security into a single cohesive set of policy files.
It looks a lot like this:
Secure(User) do |u|
u.policy(:default) do
attr_accessible end
u.policy(:self) do
attr_accessible :name, :email end
u.policy(:admin, :include => :self) do
attr_accessible :status
validates_exclusion_of :status, :in => [:banned_forever],
:unless => :skip_status_validation
def skip_status_validation ; false ; end
end
u.policy(:manager) do
end
u.policy(:super_admin, :include => [:admin, :manager]) do
def skip_status_validation ; true ; end
end
end
That’s for models. It also lets you specify controller-grained access control:
SecureController(UsersController) do |u|
u.policy(:default) do
if [:create, :update].include?(params[:action])
raise SecurityTransgression.new
end
end
end
Pretty awesome, dry, an unlikely to have you forget a before_filter :require_admin in an internal page. Default everything to closed, and you can open up all of your controllers and models in one place.
Check it out over on github (http://github.com/alexbartlow/secure_rails) – play around with it in your rails 3 apps, and let me know if you have any problems!