[英]how to use cancancan with displaying certain aspect of a page to certain users
[英]CanCanCan is not blocking access for users index page
我想阻止未登錄的用戶使用CanCanCan訪問URL
我的能力模型是
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.admin?
can :manage, :all
elsif user.roles.size > 0
can :manage, User, :id => user.id
else
can :read, :all
cannot :read, :User
end
end
end
我的用戶控制器是
class UsersController < ApplicationController
load_and_authorize_resource
def index
@users = User.paginate(page: params[:page],:per_page => 5)
end
def new
# @user = User.new
end
...
end
當我以訪客用戶身份訪問頁面時。 我在應用程序控制器中看到用戶索引頁面,而不是通過此代碼重定向到登錄頁面
rescue_from CanCan::AccessDenied do |exception|
if user_signed_in?
flash[:error] = "Access denied!"
redirect_to root_url
else
flash[:error] = "Please Sign in"
redirect_to new_user_session_path
end
end
CanCanCan可以工作並且停止訪問控制器中的其他操作,只是不用於索引。
在我的用戶控制器中,我不見了
before_filter :authenticate_user!, :except => [:new, :create]
這允許來賓用戶訪問頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.