Creating user session only when uploading a screenshot

This makes responses usually cacheable.
This commit is contained in:
Christoph Haas 2016-07-13 14:22:03 +02:00
parent 773548afc3
commit df94be866e
2 changed files with 16 additions and 5 deletions

View file

@ -3,15 +3,15 @@ class ApplicationController < ActionController::Base
# For APIs, you may want to use :null_session instead. # For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception protect_from_forgery with: :exception
before_filter :create_user_token # before_filter :create_user_token
# Do not mention passwords in the log file # Do not mention passwords in the log file
# filter_parameter_logging :password # filter_parameter_logging :password
# Assign the visitor a unique hash so that we can track their uploads # Assign the visitor a unique hash so that we can track their uploads
# and show them even before moderation. # and show them even before moderation.
def create_user_token # def create_user_token
session[:token] ||= SecureRandom.hex # session[:token] ||= SecureRandom.hex
session[:ip] ||= request.remote_ip # session[:ip] ||= request.remote_ip
end # end
end end

View file

@ -22,6 +22,9 @@ class PackagesController < ApplicationController
# POST target of the screenshots upload form. # POST target of the screenshots upload form.
# Checks upload images and creates a new Screenshot record for it. # Checks upload images and creates a new Screenshot record for it.
def upload_image def upload_image
# Remember the user by the cookie session
create_user_token
@package = Package.find_by(name: params[:name]) @package = Package.find_by(name: params[:name])
successful_upload_count = 0 successful_upload_count = 0
@ -220,4 +223,12 @@ class PackagesController < ApplicationController
# - Is the user an admin (=logged in)? # - Is the user an admin (=logged in)?
@screenshot.uploaderhash == session[:token] or user_signed_in? @screenshot.uploaderhash == session[:token] or user_signed_in?
end end
# Store a random identifier and the client's IP address in the session
# for later identification.
def create_user_token
session[:token] ||= SecureRandom.hex
session[:ip] ||= request.remote_ip
end
end end