0

Rails アプリで CanCan をサポートする Rails Admin を使用しています。しかし、私は1つの問題で混乱しています。CanCan はどのユーザーがサインインしているかをどのように認識しますか? たとえば、私のユーザーはさまざまな役割を持つことができ、CanCan を介して各テーブルへの特定のアクセスに役割を割り当てます。にアクセスするとlocalhost:3000/admin、エラーが表示されます

 CanCan::AccessDenied in RailsAdmin::MainController#dashboard 

私のAbility.rbファイル

  def initialize(user)
    if user and user.role.eql? :super_admin
      can :manage, :all                # allow superadmins to do anything
    elsif user and user.role.eql? :admin
      can :manage, [Location, School]  # allow admins to manage only Locations and Schools
    end
  end

では、ユーザーが Rails Admin にサインインできるようにするにはどうすればよいでしょうか? 手動で作成する必要がありますか?

4

2 に答える 2

2

By default, CanCan will use whatever is returned by current_user. If you are using Devise within a namespace though (admin for example) then Devise actually will use current_admin_user instead. You can either create a current_user method in your ApplicationController (or some other base controller) that returns current_admin_user or overwrite the current_ability method to instantiate the Ability with current_admin_user instead.

(this is all assuming your Devise is using a namespace. By default Devise will use current_user)

于 2013-04-02T22:26:44.700 に答える
1

コントローラーでメソッドを使用できるようにする必要がありcurrent_userます。あなたがそれを持っていると仮定すると、署名されていない場合は現在のユーザーにアクセスできないため、アビリティ ファイルにユーザーが存在しない場合は割り当てる必要があります。ユーザーがまだ存在しない場合は、メソッドinitializeに追加して割り当てを行います。user ||= User.new

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

  if user and user.role.eql? :super_admin
    can :manage, :all                # allow superadmins to do anything
  elsif user and user.role.eql? :admin
    can :manage, [Location, School]  # allow admins to manage only Locations and Schools
  end
end
于 2013-04-02T22:24:32.573 に答える