0

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 

답변

0

당신은 C로해야합니다 role.rb 제어기의 기능을 파악하고 CanCan 문서에 따라 컨트롤러 맨 위에 넣으면됩니다. load_and_authorize_resource

이렇게하면 사용자가 이미 정의한 것처럼 보이기 때문에 사용자가 다른 프로파일을 보지 못하게해야합니다. 능력을보기 만 자신의 프로필로 제한 할 수 있습니다.

은 자세한 내용은 캉캉 위키를 참조하십시오 : https://github.com/ryanb/cancan/wiki

행운을 빕니다!