1 class ConfirmationsController < ApplicationController
7 before_action :authorize_web
8 before_action :set_locale
9 before_action :check_database_readable
11 authorize_resource :class => false
13 before_action :check_database_writable, :only => [:confirm, :confirm_email]
14 before_action :require_cookies, :only => [:confirm]
18 token = UserToken.find_by(:token => params[:confirm_string])
19 if token&.user&.active?
20 flash[:error] = t(".already active")
21 redirect_to login_path
22 elsif !token || token.expired?
23 flash[:error] = t(".unknown token")
24 redirect_to :action => "confirm"
25 elsif !token.user.visible?
26 render_unknown_user token.user.display_name
30 user.email_valid = true
31 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
33 referer = safe_referer(token.referer) if token.referer
37 token = UserToken.find_by(:token => session[:token])
38 session.delete(:token)
43 if token.nil? || token.user != user
44 flash[:notice] = t(".success")
45 redirect_to login_path(:referer => referer)
49 session[:user] = user.id
50 session[:fingerprint] = user.fingerprint
52 redirect_to referer || welcome_path
56 user = User.visible.find_by(:display_name => params[:display_name])
58 redirect_to root_path if user.nil? || user.active?
63 user = User.visible.find_by(:display_name => params[:display_name])
64 token = UserToken.find_by(:token => session[:token])
66 if user.nil? || token.nil? || token.user != user
67 flash[:error] = t ".failure", :name => params[:display_name]
69 UserMailer.signup_confirm(user, user.tokens.create).deliver_later
70 flash[:notice] = { :partial => "confirmations/resend_success_flash", :locals => { :email => user.email, :sender => Settings.email_from } }
73 redirect_to login_path
78 token = UserToken.find_by(:token => params[:confirm_string])
79 if token&.user&.new_email?
80 self.current_user = token.user
81 current_user.email = current_user.new_email
82 current_user.new_email = nil
83 current_user.email_valid = true
84 gravatar_enabled = gravatar_enable(current_user)
86 flash[:notice] = if gravatar_enabled
87 "#{t('.success')} #{gravatar_status_message(current_user)}"
92 flash[:errors] = current_user.errors
94 current_user.tokens.delete_all
95 session[:user] = current_user.id
96 session[:fingerprint] = current_user.fingerprint
98 flash[:error] = t ".failure"
100 flash[:error] = t ".unknown_token"
103 redirect_to edit_account_path
110 # check if this user has a gravatar and set the user pref is true
111 def gravatar_enable(user)
112 # code from example https://en.gravatar.com/site/implement/images/ruby/
113 return false if user.avatar.attached?
116 hash = Digest::MD5.hexdigest(user.email.downcase)
117 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
118 response = OSM.http_client.get(URI.parse(url))
119 available = response.success?
124 oldsetting = user.image_use_gravatar
125 user.image_use_gravatar = available
126 oldsetting != user.image_use_gravatar
130 # display a message about th current status of the gravatar setting
131 def gravatar_status_message(user)
132 if user.image_use_gravatar
133 t "profiles.edit.gravatar.enabled"
135 t "profiles.edit.gravatar.disabled"