簡體   English   中英

cancan :: access被devise和rolify拒絕

[英]cancan::access denied using devise and rolify

我正在嘗試使用cancancan進行授權。 當我是授權的“ group_creator”用戶時,我仍然被拒絕訪問組/新用戶。

角色具有功能並且具有has_role嗎? 破壞作品。 因此,我認為這個問題來自於cancancan或設計。

Ability.rb

def initialize(user)

  user ||= User.new # guest user (not logged in)
  alias_action :create, :read, :update, :destroy, to: :crud

  if user.has_role? :group_creator
    can :create, Group
  elsif user.has_role?(:creator, Group)
    can :manage, Group, owner_id: user.id
  elsif user.has_role?(:admin, Group)
    can :crud, Group, :id => Group.with_role(:admin, user).pluck(:id)
  else
    can :read, Group
  end
end

相關的GroupsController.rb

class GroupsController < ApplicationController
  before_action :authenticate_user!
  load_and_authorize_resource

  def index
    @groups = Group.all # Can be deleted due to cancancan?
  end

  def new
    @group = Group.new # Can also be deleted due to cancancan...
  end
end

我嘗試使用以下命令在控制台中進行調試:

user=User.last
user.has_role? :group_creator # returns true
group=Group.last
ability=Ability.new(user)
ability.can?(:create, Group) # returns false
ability.can?(:create, group) # returns false

這里有兩件事。

  1. 用戶需要:read AND:創建訪問新動作的能力。 這為我解決了這個問題。

  2. 在今天早上擺弄新主意之后,為其他像我這樣的人提供了一些建議。...確保在更改能力.rb之后重啟服務器(我不確定昨天是否是問題的一部分,但是我想我應該提一下)。 Cancancan Wiki對如何利用單獨的權限表進行能力的動態修改有一些解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM