使用Cancancan限制活动管理员的访问



我想禁止管理员查看用户的数据(该用户手动添加)。管理员应该只看到他从管理面板添加的数据。我正在使用康康星宝石和活动管理员宝石。

这是 ability.rb 文件,其中使用了 cancancan:

def initialize(user)
    user ||= User.new # guest user (not logged in)
    if user.admin?
      can :manage, :all
    else
      can :manage, WebCred, user_passes: {user_id: user.id}
    end
    index do
    selectable_column
    id_column
    column :email
    column :current_sign_in_at
    column :sign_in_count
    column :created_at
    actions
  end
  filter :email
  filter :current_sign_in_at
  filter :sign_in_count
  filter :created_at
  form do |f|
    f.inputs do
    f.input :email
    f.input :password
    f.input :password_confirmation
  end
  f.actions
end

这是我的活动管理员的文件。我想禁止管理员查看用户手动添加的网络信誉的通道[原文如此],同时允许管理员查看他添加的密码。

您可以通过传递用于评估条件的块来添加新功能(当管理员不应看到密码时,该块必须评估为true)。

def initialize(user)
    user ||= User.new # guest user (not logged in)
    if user.admin?
        can :manage, :all
        cannot :see_password, WebCred do |web_cred|
            web_cred.password_created_by_user_himself? ###Your condition here
        end
    else
        can :manage, WebCred, user_passes: {user_id: user.id}
    end
end

然后在您的视图或控制器中,您可以使用 can? :see_password, WebCred.find(:id) .

相关内容

最新更新