Devise、Rolify、Cancanを使用したアプリがあります。
現在、ユーザーインデックスへのアクセスを防ぐために管理者とユーザーを区別する唯一の設定、およびサイトの管理者部分。
私の問題は、ユーザーが他のユーザープロファイルにアクセスできないということですが、そうではないはずです。たとえば、.... user / 2にログインしている場合は、URLを変更するだけでuser/1を表示できます。これをブロックするにはどうすればよいですか?
Ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user.has_role? :admin
can :manage, :all
can :access, :rails_admin
can :dashboard
else
can :manage, Profile, :user_id => user.id
end
end
end
Application_controller
class ApplicationController < ActionController::Base
protect_from_forgery
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_path, :alert => exception.message
end
role.rb
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
end