【问题标题】:Is it safe to edit user based on devise current_user helper?基于设计 current_user 助手编辑用户是否安全?
【发布时间】:2023-04-08 20:32:01
【问题描述】:

我正在使用 Devise、Omniauth-twitter 和 Omniauth-facebook 进行我的 rails 应用程序身份验证,我必须制作自己的控制器来编辑用户参数,而无需为 facebook 和 twitter 等提供商的用户提供密码。 我没有通过用户 ID 将用户路由到他的个人资料,而是使用设计助手 current_user 来显示和编辑 current_user 参数 我的问题是..这样做安全吗? 我是初学者..所以当事情做的那么容易时,我担心安全漏洞。这是我的代码。

profile_controller.rb

class ProfileController < ApplicationController
   before_action :authenticate_user!
   def show
       @user = current_user
   end

   def edit
       @profile = current_user
   end
   def update 
       @profile = current_user
       if @profile.update(profile_params)
           redirect_to profile_path(@profile)
       else
           render 'edit'
       end
   end
   private
   def profile_params
      params.require(:user).permit(:username,:first_name,:last_name,:gender)
   end
end

routes.rb

get'profile' => 'profile#show'
get'profile/edit' => 'profile#edit'
patch'profile/edit' => 'profile#update'

edit.html.erb

<%= form_for @profile, url: {action: "edit"} do |f| %>

    <%= f.text_field :username, autofocus: true %>

    <%= f.text_field :first_name, autofocus: true %>

    <%= f.text_field :last_name, autofocus: true %>

    <%= f.text_field :gender, autofocus: true %>

    <%= f.submit "Sign up" %>

<% end %>

【问题讨论】:

    标签: ruby-on-rails devise


    【解决方案1】:

    如果您使用 Devise,您可以让用户使用他们现有的视图,而不是尝试自己实现它们。但是,我认为您当前的方法没有任何安全威胁,只是浪费时间。

    查看设计文档并检查配置视图部分,

    https://github.com/plataformatec/devise

    【讨论】:

      猜你喜欢
      • 2013-05-23
      • 2012-07-03
      • 1970-01-01
      • 2014-08-31
      • 1970-01-01
      • 2014-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多