That the, password protected, editing can be enabled for the users per
controller (well model actually). e.g.
User: mark
Areas: Book CD
So in this case the user mark is allowed to edit the models Book and CD
Ok, it's clear now. You want to define areas in you app in which you can
manage rights for your users.
Looks clear! I'll play with it. Still wondering how ik can restrict
this to a specific model. Use something like this maybe?
.... User.find(session[:user_id]).rights == 'modelname_admin'
Use the model name for the 'rights' value.
You could handle your rights like this. But what happens if your
modelname change tomorrows? By the way, mark is admin of the book shop AND
the CD shop. How will you handle it?
Hmm ... maybe just check for the value book (in the field area) with the
method "authorize" and then it's based on the available models.
I suggest you to create a table for your areas:
create_table "admin_areas", :force => true do |t|
t.column "name", :string
t.column "user_id", :integer
end
class User < ActiveRecord::Base
has_many :admin_areas
def is_admin_for?(area)
AdminArea.find_by_name_and_user_id(area,self.id)
end
end
The second method return true if it finds something or nil if it
doesn't.
Assuming you have @area='book' in your controller book_controller.rb,
you can define in your application.rb:
def authorize
if User.find(session[:user_id]).is_admin_for?(@area)
return true
else
flash[:warning]="Hey! You're not admin for area #{@area}!"
redirect_to :controller => "user", :action => "show"
return false
end
end
It definitely does.
Cool