Hacking together an admin function for controller before_filters
I'm using Authologic and some written methods for managing users and what they can do in my app. I don't really need the ability to have multiple roles, I just need the ability for me to destroy things. I am wondering if I can hack this together.
In my "QuestionsController" I am using the following filters:
before_filter :require_user, :only => [:edit, :update, :destroy] # all actions require user to be logged in
before_filter :init_data # create a member variable called @post, initialized based on the action
before_filter :require_owner, :only => [:edit, :update, :dest开发者_运维百科roy] #edit, update, and destroy actions require ownership
I am trying to figure out if I can wrap those filters with a condition that specifies if the current_user's username is "bgadoci" then don't do the filters. Is this even possible and if so what syntax should I use (kind of new to ruby and rails).
Here is what I have now which is giving a syntax error in the first line (obviously).
if :current_user :username => "bgadoci"
before_filter :require_user, :only => [:edit, :update, :destroy] # all actions require user to be logged in
before_filter :init_data # create a member variable called @post, initialized based on the action
before_filter :require_owner, :only => [:edit, :update, :destroy] #edit, update, and destroy actions require ownership
end
Maybe you can do a before filter that wraps those other filters?
before_filter :check_for_me
def check_for_me
unless current_user.username == 'me'
before_filter :require_user
..
end
end
精彩评论