Devise, Rolify 및 Cancan을 사용하는 앱이 있습니다.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
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, :join_table => :users_roles
belongs_to :resource, :polymorphic => true
end