我的路线文件中有这个:
devise_for :users, :controllers => { :registrations => "users/registrations",
:sessions => "users/sessions",
:omniauth_callbacks => "users/omniauth_callbacks" }
devise_scope :user do
get 'sign_in', :to => 'users/sessions#new', :as => :new_user_session
get 'sign_out', :to => 'devise/sessions#destroy', :as => :destroy_user_session
end
现在我补充说:
resources :users
因为我希望管理员用户能够看到所有用户。
在 CanCan 我有这个:
class Ability
include CanCan::Ability
def initialize(user)
if user
if user.admin?
can :manage, :all
end
can [:read, :edit, :update], User, :id => user.id
end
can [:create], User
end
end
这足够安全吗?我是否还应该在 UserController 中添加一个 before_filter 来阻止对新创建的路由的调用?像这样的东西?问题在于用户无法再更改自己的个人资料......
before_filter :check_rights
private
def check_rights
unless current_user.admin
redirect_to root_path
end
end