Attempt to assign anonymous screenshots to logged-in user
This commit is contained in:
parent
228b7375e7
commit
0dd63f6bf6
7 changed files with 65 additions and 25 deletions
|
|
@ -33,8 +33,7 @@ class PackagesController < ApplicationController
|
||||||
# Create a pseudo user account for the user.
|
# Create a pseudo user account for the user.
|
||||||
# The user won't know that an account is created.
|
# The user won't know that an account is created.
|
||||||
# But this makes it easier to track who screenshots belong to.
|
# But this makes it easier to track who screenshots belong to.
|
||||||
create_pseudo_user unless user_signed_in?
|
# User.create_pseudo_user unless user_signed_in?
|
||||||
|
|
||||||
@package = Package.find_by!(name: params[:name])
|
@package = Package.find_by!(name: params[:name])
|
||||||
@valid_images = []
|
@valid_images = []
|
||||||
@invalid_images = []
|
@invalid_images = []
|
||||||
|
|
@ -57,16 +56,20 @@ class PackagesController < ApplicationController
|
||||||
new_screenshot.user = current_user
|
new_screenshot.user = current_user
|
||||||
|
|
||||||
# Can the upload get approved automatically?
|
# Can the upload get approved automatically?
|
||||||
new_screenshot.approve! if current_user and current_user.auto_approve?
|
if current_user
|
||||||
|
new_screenshot.approve! if current_user.auto_approve?
|
||||||
|
|
||||||
|
# For anonymous uploads remember which screenshots have been
|
||||||
|
# uploaded. If the user logs in later the screenshots will
|
||||||
|
# be linked to their account.
|
||||||
|
else
|
||||||
|
(session[:uploaded_screenshots] ||= []) << new_screenshot.id
|
||||||
|
Log.log "Anonymously uploaded screenshots #{new_screenshot.id} remembered in cookie session to assign it to a user later."
|
||||||
|
end
|
||||||
|
|
||||||
new_screenshot.save
|
new_screenshot.save
|
||||||
Log.log "Screenshot #{new_screenshot.id} uploaded successfully from #{session[:ip]}. User has token #{session[:token]} or is #{current_user}"
|
Log.log "Screenshot #{new_screenshot.id} uploaded successfully from #{session[:ip]}. User has token #{session[:token]} or is #{current_user}"
|
||||||
|
|
||||||
if current_user.is_admin?
|
|
||||||
Log.log "Admin upload is automatically approved."
|
|
||||||
new_screenshot.approve_screenshot!
|
|
||||||
end
|
|
||||||
|
|
||||||
@valid_images.push new_screenshot
|
@valid_images.push new_screenshot
|
||||||
else
|
else
|
||||||
Log.log "Screenshot #{new_screenshot.image_file_name} invalid (#{new_screenshot.errors[:image]})."
|
Log.log "Screenshot #{new_screenshot.image_file_name} invalid (#{new_screenshot.errors[:image]})."
|
||||||
|
|
@ -248,17 +251,6 @@ class PackagesController < ApplicationController
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
# Seamlessly create a user account for the current client.
|
|
||||||
# It helps track uploads.
|
|
||||||
def create_pseudo_user
|
|
||||||
generated_password = Devise.friendly_token.first(8)
|
|
||||||
new_user = User.create(
|
|
||||||
name: 'Anonymous',
|
|
||||||
password: generated_password)
|
|
||||||
Log.log "New pseudo user for anonymous upload created: #{new_user}"
|
|
||||||
sign_in(new_user)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Send a dummy thumbnail reading "No screenshot available. Sorry."
|
# Send a dummy thumbnail reading "No screenshot available. Sorry."
|
||||||
def thumbnail404
|
def thumbnail404
|
||||||
send_file Rails.root.join('public/images/dummy/thumbnail404.png'),
|
send_file Rails.root.join('public/images/dummy/thumbnail404.png'),
|
||||||
|
|
@ -298,10 +290,10 @@ class PackagesController < ApplicationController
|
||||||
|
|
||||||
# Store a random identifier and the client's IP address in the session
|
# Store a random identifier and the client's IP address in the session
|
||||||
# for later identification.
|
# for later identification.
|
||||||
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
|
||||||
|
|
||||||
# Get reviews of this package from the Ubuntu API
|
# Get reviews of this package from the Ubuntu API
|
||||||
def get_ubuntu_reviews(packagename)
|
def get_ubuntu_reviews(packagename)
|
||||||
|
|
|
||||||
23
app/controllers/users/my_sessions_controller.rb
Normal file
23
app/controllers/users/my_sessions_controller.rb
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
class Users::MySessionsController < Devise::SessionsController
|
||||||
|
# before_filter :before_login, :only => :create
|
||||||
|
after_filter :after_login, :only => :create
|
||||||
|
|
||||||
|
# def before_login
|
||||||
|
# end
|
||||||
|
|
||||||
|
# Hook after successful login
|
||||||
|
def after_login
|
||||||
|
Log.log "User #{current_user} logged in."
|
||||||
|
old_screenshots = session[:uploaded_screenshots]
|
||||||
|
raise
|
||||||
|
if count = old_screenshots.to_a.length > 0
|
||||||
|
old_screenshots.each do |id|
|
||||||
|
ss = Screenshot.find(id)
|
||||||
|
ss.user = @user
|
||||||
|
ss.save!
|
||||||
|
end
|
||||||
|
flash[:info] = "#{count} uploads have been added to your account"
|
||||||
|
Log.log "Anonymously uploaded screenshots #{old_screenshots} added to account."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -33,6 +33,8 @@ class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
|
||||||
# You need to implement the method below in your model (e.g. app/models/user.rb)
|
# You need to implement the method below in your model (e.g. app/models/user.rb)
|
||||||
@user = User.from_omniauth(request.env["omniauth.auth"])
|
@user = User.from_omniauth(request.env["omniauth.auth"])
|
||||||
|
|
||||||
|
# Has the user uploaded screenshots while not being logged in?
|
||||||
|
# Move the screenshots to the real account and tell the user.
|
||||||
if @user.persisted?
|
if @user.persisted?
|
||||||
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => provider_name
|
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => provider_name
|
||||||
sign_in_and_redirect @user, :event => :authentication
|
sign_in_and_redirect @user, :event => :authentication
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ class Screenshot < ApplicationRecord
|
||||||
belongs_to :package, inverse_of: :screenshots
|
belongs_to :package, inverse_of: :screenshots
|
||||||
belongs_to :user, inverse_of: :screenshots
|
belongs_to :user, inverse_of: :screenshots
|
||||||
|
|
||||||
|
# Use paperclip gem to handle image files related to screenshots
|
||||||
has_attached_file :image,
|
has_attached_file :image,
|
||||||
styles: { :large => '800x600>', :thumb => '160x120>' },
|
styles: { :large => '800x600>', :thumb => '160x120>' },
|
||||||
default_url: '/images/dummy/no-screenshots-available.svg',
|
default_url: '/images/dummy/no-screenshots-available.svg',
|
||||||
|
|
|
||||||
|
|
@ -52,6 +52,9 @@ class User < ApplicationRecord
|
||||||
|
|
||||||
# Do uploads from this user get approved automatically?
|
# Do uploads from this user get approved automatically?
|
||||||
def auto_approve?
|
def auto_approve?
|
||||||
|
# Anonymous users need to go through moderation
|
||||||
|
return false unless user_signed_in?
|
||||||
|
|
||||||
# Admins do not need moderation
|
# Admins do not need moderation
|
||||||
return true if current_user.is_admin?
|
return true if current_user.is_admin?
|
||||||
|
|
||||||
|
|
@ -75,4 +78,21 @@ class User < ApplicationRecord
|
||||||
user.password = Devise.friendly_token[0,20]
|
user.password = Devise.friendly_token[0,20]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Seamlessly create a user account for the current client.
|
||||||
|
# It helps track uploads because uploaded screenshots get assigned
|
||||||
|
# to this user record. The user can later decide to use a real
|
||||||
|
# account and get their screenshots transferred to that.
|
||||||
|
# def self.create_pseudo_user
|
||||||
|
# generated_password = Devise.friendly_token.first(8)
|
||||||
|
# new_user = User.create(
|
||||||
|
# name: 'Anonymous',
|
||||||
|
# password: generated_password)
|
||||||
|
# Log.log "New pseudo user for anonymous upload created: #{new_user}"
|
||||||
|
# sign_in new_user
|
||||||
|
# end
|
||||||
|
#
|
||||||
|
# Currently the approach is different: do not create an account
|
||||||
|
# for a user. Instead store the IDs of the uploaded screenshots
|
||||||
|
# and transfer them if the user decides to do a real login using SSO.
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
= session[:uploaded_screenshots].inspect
|
||||||
nav.top-bar
|
nav.top-bar
|
||||||
.grid-container style="flex-grow:1;"
|
.grid-container style="flex-grow:1;"
|
||||||
// https://foundation.zurb.com/forum/posts/53446-top-bar-and-xy-grid
|
// https://foundation.zurb.com/forum/posts/53446-top-bar-and-xy-grid
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,8 @@ Rails.application.routes.draw do
|
||||||
devise_for :users, controllers: {
|
devise_for :users, controllers: {
|
||||||
# registrations: "users/registrations",
|
# registrations: "users/registrations",
|
||||||
# passwords: "users/passwords",
|
# passwords: "users/passwords",
|
||||||
omniauth_callbacks: "users/omniauth_callbacks"
|
omniauth_callbacks: "users/omniauth_callbacks",
|
||||||
|
sessions: "users/my_sessions"
|
||||||
}
|
}
|
||||||
|
|
||||||
get 'packages' => 'packages#grid', as: :packages_grid
|
get 'packages' => 'packages#grid', as: :packages_grid
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue