From df94be866ec61c4a458a4a222266aef5d738aff3 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Wed, 13 Jul 2016 14:22:03 +0200 Subject: [PATCH] Creating user session only when uploading a screenshot This makes responses usually cacheable. --- app/controllers/application_controller.rb | 10 +++++----- app/controllers/packages_controller.rb | 11 +++++++++++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 9e45186..07bbe48 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,15 +3,15 @@ class ApplicationController < ActionController::Base # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception - before_filter :create_user_token + # before_filter :create_user_token # Do not mention passwords in the log file # filter_parameter_logging :password # Assign the visitor a unique hash so that we can track their uploads # and show them even before moderation. - def create_user_token - session[:token] ||= SecureRandom.hex - session[:ip] ||= request.remote_ip - end + # def create_user_token + # session[:token] ||= SecureRandom.hex + # session[:ip] ||= request.remote_ip + # end end diff --git a/app/controllers/packages_controller.rb b/app/controllers/packages_controller.rb index 410fc8a..540b212 100644 --- a/app/controllers/packages_controller.rb +++ b/app/controllers/packages_controller.rb @@ -22,6 +22,9 @@ class PackagesController < ApplicationController # POST target of the screenshots upload form. # Checks upload images and creates a new Screenshot record for it. def upload_image + # Remember the user by the cookie session + create_user_token + @package = Package.find_by(name: params[:name]) successful_upload_count = 0 @@ -220,4 +223,12 @@ class PackagesController < ApplicationController # - Is the user an admin (=logged in)? @screenshot.uploaderhash == session[:token] or user_signed_in? 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