我有2個模型,用戶和common_app。是必要的action之前的correct_user嗎?
用戶has_one common_app。
在common_app控制器中,我使用current_user助手定義了幾乎所有的東西。這基本上使得編輯表單忽略了用戶通過Web瀏覽器POST的ID。
它看起來像這樣 - >
class CommonAppsController < ApplicationController
before_action :signed_in_user
def new
if current_user.common_app.present?
redirect_to current_user
else
@common_app = current_user.build_common_app
end
end
def create
@common_app = current_user.build_common_app(common_app_params)
if @common_app.save
flash[:success] = "Common App Created!"
redirect_to root_url
else
redirect_to 'common_apps/new'
end
end
def update
if current_user.common_app.update_attributes(common_app_params)
flash[:success] = "Common App Updated"
redirect_to root_url
else
render 'common_apps/edit'
end
end
def show
@common_app = current_user.common_app
end
def edit
@common_app = current_user.common_app
end
private
def common_app_params
params.require(:common_app).permit(:current_city,:grad_year,:read_type,
:listen_speak,:time_in_china,
:cover_letter,:resume) ####fill in the correct ones here
end
# is correct_user necessary?
end
是什麼讓我的警惕是,雖然我沒有使用動作之前correct_user。如果我不使用它,這裏會有安全漏洞嗎?我可以通過一個shell或別的東西發佈某人?
如果是的話,你將如何改變控制器,包括之前的過濾器? PS:我對@變量的正確使用也有點困惑。如果我過度使用它們,或者做一些古怪的他們,請告訴我,幫助我成爲一個更好的小白:)
PPS:這是我的SessionsHelper模塊,爲signed_in_user過濾器工作前 - >
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user=(user)
@current_user = user
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
def current_user?(user)
user == current_user
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
def redirect_back_or(default) # this creates friendly forwarding for the app
redirect_to(session[:return_to] || default)
session.delete(:return_to)
end
def store_location
session[:return_to] = request.url if request.get?
end
def signed_in_user
unless signed_in?
store_location
redirect_to signin_url, notice: "Please sign in."
end
end
end
你的':signed_in_user'過濾器是在別的地方定義的嗎?在'ApplicationController'中? – janfoeh
嗨Janfoeh,signed_in_user在SessionsHelper下定義,讓我更新 - > –
'correct_user'過濾器會做什麼?你懷疑在這裏可能會錯過什麼? – janfoeh