Devise before_filter authenticate_admin?

前端 未结 2 1304
天命终不由人
天命终不由人 2021-02-05 18:02

I added an admin role to Devise by adding a admin attribute.

Could you tell me if this is the right way to create a before_filter that requires an admin user to be signe

2条回答
  •  遇见更好的自我
    2021-02-05 18:22

    Go with this approach

      before_filter :authenticate_user!
      before_filter do 
        redirect_to new_user_session_path unless current_user && current_user.admin?
      end
    

    This also ensures any guests are forced to sign in as well. You don't need to modify the default method to force authentication just to access the instance method admin?

    def admin?
      self.admin == true
    end
    

    My approach is to create a role attribute and check its string value against a set of intended roles - it's far more flexible this way rather than having to create many boolean attributes.

提交回复
热议问题