Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 具有默认身份验证的Rails 3授权_Ruby On Rails_Ruby On Rails 3_Authentication_Authorization - Fatal编程技术网

Ruby on rails 具有默认身份验证的Rails 3授权

Ruby on rails 具有默认身份验证的Rails 3授权,ruby-on-rails,ruby-on-rails-3,authentication,authorization,Ruby On Rails,Ruby On Rails 3,Authentication,Authorization,我正在使用具有用户授权的应用程序。它有一个列表和用户类。身份验证是由Ryan Bates创建的 我不确定授权流程。我读到了有关cancan的文章。但我不明白 我想做到这一点: 用户只能查看/编辑/删除自己的列表 用户只能查看/编辑/删除自己的配置文件(用户类) 我现在不实现用户级。没有猜测或管理员 如何在列表和用户控制器中与当前用户实例一起使用before\u filter方法?由于您在应用程序控制器中定义了当前用户,这很容易。您可以在用户控制器中这样使用before\u filter: cla

我正在使用具有用户授权的应用程序。它有一个
列表
用户
类。身份验证是由Ryan Bates创建的

我不确定授权流程。我读到了有关cancan的文章。但我不明白

我想做到这一点:

  • 用户只能查看/编辑/删除自己的列表
  • 用户只能查看/编辑/删除自己的配置文件(用户类)
  • 我现在不实现用户级。没有猜测或管理员


    如何在
    列表
    用户
    控制器中与
    当前用户
    实例一起使用before\u filter方法?

    由于您在应用程序控制器中定义了
    当前用户
    ,这很容易。您可以在用户控制器中这样使用
    before\u filter

    class ItemsController < ApplicationController
      before_filter :check_if_owner, :only => [:edit, :update, :show, :destroy]
    
      def check_if_owner
        unless current_user.admin? # check whether the user is admin, preferably by a method in the model
          unless # check whether the current user is the owner of the item (or whether it is his account) like 'current_user.id == params[:id].to_i'
            flash[:notice] = "You dont have permission to modify this item"
            redirect_to # some path
            return
          end
        end
      end
    
      ###
    
    end
    
    class ItemsController[:编辑,:更新,:显示,:销毁]
    def检查是否为所有者
    除非当前_user.admin?#检查用户是否是管理员,最好使用模型中的方法
    除非#检查当前用户是否是项目的所有者(或是否是他的帐户),如“current_user.id==params[:id]。to_i”
    flash[:notice]=“您没有修改此项目的权限”
    重定向到某个路径
    返回
    结束
    结束
    结束
    ###
    结束
    
    您应该向UsersController添加一个类似的方法来检查它是否是他的个人资料,他正在编辑


    另外,看看哪一个插件是推荐用于身份验证的插件

    为此,我不会使用Desive。对于这种简单的使用来说,这是一个非常有用的方法

    我会为公众视图制作一个单独的控制器,并始终引用当前用户 记住为PublicController中的操作创建路由

    class PublicController < ApplicationController
     before_filter :login_required?
     def list
      @list = current_user.list
     end
     def user
      @user = current_user
     end
     def user_delete
      @user = current_user
       # do your magic
     end
     def user_update
      @user = current_user
       # do your magic
     end
    
    # and so on...
    end
    
    class PublicController