4

非常に基本的なユーザー モデルです。管理者ユーザーに :manage all をお願いします

そうでなければ、インデックス、ユーザー、およびその他のオプションを使用できませんが、管理者以外のユーザーがユーザーインデックスを表示できないようにブロックしようとすると、管理者ユーザーもアクセスできません。

これが私の能力です.rb

class Ability
  include CanCan::Ability

  def initialize(user)
    user ||= User.new #guest user

    can :manage, :all if user.role == "admin" #if user.admin? can :manage, :all
    can :assign_role, User 

    else
      can :read, :all
      can :create, User


      cannot :assign_role, User
      cannot :index, User

      can [:show, :edit, :update], User do |current_user|
              user.id == current_user.id || user.role == "admin"
            end



  end
end

すべてのユーザーがユーザー インデックスからブロックされないようにするにはどうすればよいですか?

よろしく

ダン

4

1 に答える 1

3

コードの if-else に問題があります。

if user.role == "admin"
  can :manage, :all
  can :assign_role, User 

else
  can :read, :all
  can :create, User


  cannot :assign_role, User
  cannot :index, User
  can [:show, :edit, :update], User do |current_user|
    user.id == current_user.id || user.role == "admin"
  end

end

また、管理者以外のユーザーがロールを割り当てることを明らかに拒否する必要はありません (:assign_role, User はできません)。

于 2010-03-03T07:06:35.637 に答える