【问题标题】:Manage the edit users in rails 4在 rails 4 中管理编辑用户
【发布时间】:2015-07-31 17:31:01
【问题描述】:

我有一个用户数据库,我希望只有当前用户能够修改他自己的页面。

我的路线很简单:

resources :users

在我的 users_controller 中,我有一个编辑功能。我现在的问题是,如果你是用户1,你可以访问users/1/edit,这很好,但你也可以访问users/2/edit,然后可以修改另一个用户。

避免这个问题的最佳方法是什么?

目前,我尝试将此代码放在users_controller中,但是这样,即使当前用户也无法修改自己的页面:(

before_action :require_permission, only: :edit

def require_permission
 if current_user.id != params[:id]
  flash['alert alert-dismissible alert-danger'] = "Vous n'avez pas accès à cette page"
  redirect_to users_path
 end
end

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    您可以使用可用的授权 gems,例如 punditcancancan 等。下面是使用 pundit 的示例。

    示例代码

    #app/policies/user_policy.rb
    class UserPolicy
      attr_reader :current_user
    
      def initialize(current_user)
        @current_user = current_user
      end
    
      def edit?
        @current_user
      end
    end
    
    #users_controller
    def edit
      @user = current_user
      authorize @user
    end
    

    【讨论】:

    • 使用cancancan 代替cancan。旧的cancan gem 与 Rails 4 不兼容
    • 不错,但第一次接触 Rails 编程时很难理解。 +1
    • 非常感谢您的回答!
    【解决方案2】:

    这很简单,只有当current_user id 等于来自users/:id/editid 时,才使users/:id/edit 的链接可用。类似的,这应该在视图中:

    <% @users.each do |user| %>
        <% if current_user && (user.id == current_user.id) %>
          <%= link_to 'edit', edit_user_path(user) %>
        <% else %>
          <%= link_to 'edit', '#' %>
        <% end %>
      <% end %>
    

    还要检查控制器操作中编辑用户的权限。

    def edit
      if current_user && (current_user.id == params[:id])
        # do the stuff
        # ....
      else
        redirect_to :some_action
    

    要重用此代码,您可以使用before_action 来检查您需要的每个操作中的用户权限。

    application_controller.rb写一个方法:

    class ApplicationController < ActionController::Base
      # Prevent CSRF attacks by raising an exception.
      # For APIs, you may want to use :null_session instead.
      protect_from_forgery with: :exception
    
      # some code here
    
      def check_priveleges
        unless current_user && (current_user.id == params[:id])
          redirect_to root_path
        end
      end
    
      # some code here
    end
    

    users_controller.rb 写一个before_action

    class UsersController < ApplicationController
      # some code here
      before_action :check_priveleges, only: [:edit]
      # this mean pass to the `edit` action only after :check_priveleges filter
    
      def edit
        # already pass through :check_privilege action in `ApplicationController`
      end
    end
    

    【讨论】:

    • 感谢您的回答!但是,确切地说,我该如何写它仅在 current_user.id == :id 时才可用?抱歉,我真的是 Rails 新手
    • 感谢您的更新。我的问题是我仍然可以在浏览器中更改 url 并访问该页面。如何在控制器中设置检查权限?
    • 非常感谢,是否可以干燥此代码? (如果我需要在同一个控制器中多次检查当前用户是否是 url 中的用户?)
    • @saraf 是的,检查更新,但要小心它可能包含非常毛茸茸的错误。
    • 非常感谢!!!!!!!我注意到它只有在我测试 current_user.id.to_i != params[:id].to_i 时才有效
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2010-12-12
    • 1970-01-01
    相关资源
    最近更新 更多