Ruby on rails 如何使cancan授权gem限制用户使用他们自己的数据?

Ruby on rails 如何使cancan授权gem限制用户使用他们自己的数据?,ruby-on-rails,authorization,cancan,Ruby On Rails,Authorization,Cancan,您好,我正在使用Ryan的Bates cancan gem授权,使我的应用程序的用户只管理他们创建的数据。我正在使用来处理身份验证 型号/能力.rb class Ability include CanCan::Ability def initialize(user) user ||= User.new # guest user (not logged in) if user can :manage, Profile, :user_id =&g

您好,我正在使用Ryan的Bates cancan gem授权,使我的应用程序的用户只管理他们创建的数据。我正在使用来处理身份验证

型号/能力.rb

class Ability
  include CanCan::Ability

  def initialize(user)
       user ||= User.new # guest user (not logged in)
       if user
        can :manage, Profile, :user_id => user.id
        can :manage, Album, :user_id => user.id
       end

  end
end
控制器/相册\u控制器.rb

# -*- encoding : utf-8 -*-
class AlbumsController < ApplicationController

    # Authentification before accessing Albums
    before_filter :require_login, :except => [:not_authenticated]
    load_and_authorize_resource


  def index
    @albums = Album.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @albums }
    end
  end


  def show
    @album = Client.find(params[:id])
    authorize! :show, @album

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @album }
    end
  end


  def new
    @album = Album.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @album }
    end
  end

  def edit
    @album = Album.find(params[:id])
     authorize! :edit, @album
  end


  def create
    @album = Album.new(params[:album])

    respond_to do |format|
      if @album.save
        format.html { redirect_to @album, notice: 'album was successfully created.' }
        format.json { render json: @album, status: :created, location: @album }
      else
        format.html { render action: "new" }
        format.json { render json: @album.errors, status: :unprocessable_entity }
      end
    end
  end


  def update
    @album = Album.find(params[:id])
     authorize! :update, @album

    respond_to do |format|
      if @album.update_attributes(params[:album])
        format.html { redirect_to @album, notice: 'Album was successfully updated.' }
        format.json { head :ok }
      else
        format.html { render action: "edit" }
        format.json { render json: @album.errors, status: :unprocessable_entity }
      end
    end
  end


  def destroy
    @album = Album.find(params[:id])
    @album.destroy

    respond_to do |format|
      format.html { redirect_to albums_url }
      format.json { head :ok }
    end
  end
end
#-*-编码:utf-8-*-
类AlbumsController<应用程序控制器
#访问相册前的身份验证
在\u筛选器之前:要求\u登录,:except=>[:未\u验证]
加载和授权资源
def索引
@albums=Album.all
回应待办事项|格式|
format.html#index.html.erb
format.json{render json:@albums}
终止
终止
def秀
@album=Client.find(参数[:id])
授权!:显示,@相册
回应待办事项|格式|
format.html#show.html.erb
format.json{render json:@album}
终止
终止
def新
@album=album.new
回应待办事项|格式|
format.html#new.html.erb
format.json{render json:@album}
终止
终止
定义编辑
@album=album.find(参数[:id])
授权!:编辑,@相册
终止
def创建
@唱片集=唱片集。新建(参数[:唱片集])
回应待办事项|格式|
如果@album.save
format.html{将_重定向到@album,注意:“album已成功创建”。}
format.json{render json:@album,status::created,location:@album}
其他的
format.html{呈现操作:“新建”}
format.json{render json:@album.errors,status::unprocessable_entity}
终止
终止
终止
def更新
@album=album.find(参数[:id])
授权!:更新,@album
回应待办事项|格式|
如果@album.update_属性(参数[:album])
format.html{将_重定向到@album,注意:'album已成功更新。}
format.json{head:ok}
其他的
format.html{呈现操作:“编辑”}
format.json{render json:@album.errors,status::unprocessable_entity}
终止
终止
终止
def销毁
@album=album.find(参数[:id])
@毁灭
回应待办事项|格式|
format.html{重定向到相册\u url}
format.json{head:ok}
终止
终止
终止

但是在此之后,用户仍然可以对其他用户的数据进行操作。我缺少什么可以做的。

根据,我相信您必须添加一个
load\u和\u authorize\u资源
或类似于您想要授权的类,或者添加一个
authorize
在控制器操作中手动处理授权的方法。

加载和授权资源
自动为您提供@albums(因此无需在
索引
中再次设置)。因此,在以下情况下:

def index
  @albums = Album.all
   .....# some code
end
@albums
将再次加载所有相册,这就是它显示所有相册的原因。您可以将其替换为:

def index
  @albums = Album.accessible_by(current_ability)
   .....# some code
end
但即使是这样也不需要,因为
load\u和\u authorize\u资源
为当前用户填充了正确的相册。因此以下内容就足够了:

def index
   .....# some code
end

将给你同样的结果。这是cancan的力量。此外,你可以使用
授权资源
加载资源
来代替
加载和授权资源
。更多详细信息

这是能力的定义-你如何检查用户可以或不能对数据进行操作?通常你会某种类型的
authorize
调用您的控制器。(看起来您也进行了一些实验-如果您正在执行
user | |=user.new
,则用户不可能为零)你说得对,我已经更改了。感谢你指出这个问题。我已经添加了相册的控制器。它在profils_控制器中的配置相同。rbI已经尝试在每个控制器中加载_和_授权_资源,但用户仍然可以看到另一个数据