Avoid CSP issues with OpenID login
[rails.git] / app / controllers / users_controller.rb
1 class UsersController < ApplicationController
2   layout "site", :except => [:api_details]
3
4   skip_before_action :verify_authenticity_token, :only => [:api_read, :api_users, :api_details, :api_gpx_files, :auth_success]
5   before_action :disable_terms_redirect, :only => [:terms, :save, :logout, :api_details]
6   before_action :authorize, :only => [:api_details, :api_gpx_files]
7   before_action :authorize_web, :except => [:api_read, :api_users, :api_details, :api_gpx_files]
8   before_action :set_locale, :except => [:api_read, :api_users, :api_details, :api_gpx_files]
9   before_action :api_deny_access_handler, :only => [:api_read, :api_users, :api_details, :api_gpx_files]
10
11   authorize_resource
12
13   before_action :require_self, :only => [:account]
14   before_action :check_database_readable, :except => [:login, :api_read, :api_users, :api_details, :api_gpx_files]
15   before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend]
16   before_action :check_api_readable, :only => [:api_read, :api_users, :api_details, :api_gpx_files]
17   before_action :require_cookies, :only => [:new, :login, :confirm]
18   around_action :api_call_handle_error, :only => [:api_read, :api_users, :api_details, :api_gpx_files]
19   before_action :lookup_user_by_id, :only => [:api_read]
20   before_action :lookup_user_by_name, :only => [:set_status, :delete]
21   before_action :allow_thirdparty_images, :only => [:show, :account]
22
23   def terms
24     @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || DEFAULT_LEGALE
25     @text = OSM.legal_text_for_country(@legale)
26
27     if request.xhr?
28       render :partial => "terms"
29     else
30       @title = t "users.terms.title"
31
32       if current_user&.terms_agreed?
33         # Already agreed to terms, so just show settings
34         redirect_to :action => :account, :display_name => current_user.display_name
35       elsif current_user.nil? && session[:new_user].nil?
36         redirect_to :action => :login, :referer => request.fullpath
37       end
38     end
39   end
40
41   def save
42     @title = t "users.new.title"
43
44     if params[:decline]
45       if current_user
46         current_user.terms_seen = true
47
48         flash[:notice] = t("users.new.terms declined", :url => t("users.new.terms declined url")).html_safe if current_user.save
49
50         if params[:referer]
51           redirect_to params[:referer]
52         else
53           redirect_to :action => :account, :display_name => current_user.display_name
54         end
55       else
56         redirect_to t("users.terms.declined")
57       end
58     elsif current_user
59       unless current_user.terms_agreed?
60         current_user.consider_pd = params[:user][:consider_pd]
61         current_user.terms_agreed = Time.now.getutc
62         current_user.terms_seen = true
63
64         flash[:notice] = t "users.new.terms accepted" if current_user.save
65       end
66
67       if params[:referer]
68         redirect_to params[:referer]
69       else
70         redirect_to :action => :account, :display_name => current_user.display_name
71       end
72     else
73       self.current_user = session.delete(:new_user)
74
75       if check_signup_allowed(current_user.email)
76         current_user.data_public = true
77         current_user.description = "" if current_user.description.nil?
78         current_user.creation_ip = request.remote_ip
79         current_user.languages = http_accept_language.user_preferred_languages
80         current_user.terms_agreed = Time.now.getutc
81         current_user.terms_seen = true
82
83         if current_user.auth_uid.blank?
84           current_user.auth_provider = nil
85           current_user.auth_uid = nil
86         end
87
88         if current_user.save
89           flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
90
91           referer = welcome_path
92
93           begin
94             uri = URI(session[:referer])
95             %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
96               editor = Rack::Utils.parse_query(uri.query).slice("editor")
97               referer = welcome_path({ "zoom" => m[1],
98                                        "lat" => m[2],
99                                        "lon" => m[3] }.merge(editor))
100             end
101           rescue StandardError
102             # Use default
103           end
104
105           if current_user.status == "active"
106             session[:referer] = referer
107             successful_login(current_user)
108           else
109             session[:token] = current_user.tokens.create.token
110             Notifier.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_later
111             redirect_to :action => "confirm", :display_name => current_user.display_name
112           end
113         else
114           render :action => "new", :referer => params[:referer]
115         end
116       end
117     end
118   end
119
120   def account
121     @tokens = current_user.oauth_tokens.authorized
122
123     if params[:user] && params[:user][:display_name] && params[:user][:description]
124       if params[:user][:auth_provider].blank? ||
125          (params[:user][:auth_provider] == current_user.auth_provider &&
126           params[:user][:auth_uid] == current_user.auth_uid)
127         update_user(current_user, params)
128       else
129         session[:new_user_settings] = params
130         redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
131       end
132     elsif errors = session.delete(:user_errors)
133       errors.each do |attribute, error|
134         current_user.errors.add(attribute, error)
135       end
136     end
137     @title = t "users.account.title"
138   end
139
140   def go_public
141     current_user.data_public = true
142     current_user.save
143     flash[:notice] = t "users.go_public.flash success"
144     redirect_to :action => "account", :display_name => current_user.display_name
145   end
146
147   def lost_password
148     @title = t "users.lost_password.title"
149
150     if params[:user] && params[:user][:email]
151       user = User.visible.find_by(:email => params[:user][:email])
152
153       if user.nil?
154         users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
155
156         user = users.first if users.count == 1
157       end
158
159       if user
160         token = user.tokens.create
161         Notifier.lost_password(user, token).deliver_later
162         flash[:notice] = t "users.lost_password.notice email on way"
163         redirect_to :action => "login"
164       else
165         flash.now[:error] = t "users.lost_password.notice email cannot find"
166       end
167     end
168   end
169
170   def reset_password
171     @title = t "users.reset_password.title"
172
173     if params[:token]
174       token = UserToken.find_by(:token => params[:token])
175
176       if token
177         self.current_user = token.user
178
179         if params[:user]
180           current_user.pass_crypt = params[:user][:pass_crypt]
181           current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
182           current_user.status = "active" if current_user.status == "pending"
183           current_user.email_valid = true
184
185           if current_user.save
186             token.destroy
187             flash[:notice] = t "users.reset_password.flash changed"
188             successful_login(current_user)
189           end
190         end
191       else
192         flash[:error] = t "users.reset_password.flash token bad"
193         redirect_to :action => "lost_password"
194       end
195     else
196       head :bad_request
197     end
198   end
199
200   def new
201     @title = t "users.new.title"
202     @referer = params[:referer] || session[:referer]
203
204     append_content_security_policy_directives(
205       :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
206     )
207
208     if current_user
209       # The user is logged in already, so don't show them the signup
210       # page, instead send them to the home page
211       if @referer
212         redirect_to @referer
213       else
214         redirect_to :controller => "site", :action => "index"
215       end
216     elsif params.key?(:auth_provider) && params.key?(:auth_uid)
217       self.current_user = User.new(:email => params[:email],
218                                    :email_confirmation => params[:email],
219                                    :display_name => params[:nickname],
220                                    :auth_provider => params[:auth_provider],
221                                    :auth_uid => params[:auth_uid])
222
223       flash.now[:notice] = render_to_string :partial => "auth_association"
224     else
225       check_signup_allowed
226
227       self.current_user = User.new
228     end
229   end
230
231   def create
232     self.current_user = User.new(user_params)
233
234     if check_signup_allowed(current_user.email)
235       session[:referer] = params[:referer]
236
237       current_user.status = "pending"
238
239       if current_user.auth_provider.present? && current_user.pass_crypt.empty?
240         # We are creating an account with external authentication and
241         # no password was specified so create a random one
242         current_user.pass_crypt = SecureRandom.base64(16)
243         current_user.pass_crypt_confirmation = current_user.pass_crypt
244       end
245
246       if current_user.invalid?
247         # Something is wrong with a new user, so rerender the form
248         render :action => "new"
249       elsif current_user.auth_provider.present?
250         # Verify external authenticator before moving on
251         session[:new_user] = current_user
252         redirect_to auth_url(current_user.auth_provider, current_user.auth_uid)
253       else
254         # Save the user record
255         session[:new_user] = current_user
256         redirect_to :action => :terms
257       end
258     end
259   end
260
261   def login
262     session[:referer] = params[:referer] if params[:referer]
263
264     if params[:username].present? && params[:password].present?
265       session[:remember_me] ||= params[:remember_me]
266       password_authentication(params[:username], params[:password])
267     end
268   end
269
270   def logout
271     @title = t "users.logout.title"
272
273     if params[:session] == session.id
274       if session[:token]
275         token = UserToken.find_by(:token => session[:token])
276         token&.destroy
277         session.delete(:token)
278       end
279       session.delete(:user)
280       session_expires_automatically
281       if params[:referer]
282         redirect_to params[:referer]
283       else
284         redirect_to :controller => "site", :action => "index"
285       end
286     end
287   end
288
289   def confirm
290     if request.post?
291       token = UserToken.find_by(:token => params[:confirm_string])
292       if token&.user&.active?
293         flash[:error] = t("users.confirm.already active")
294         redirect_to :action => "login"
295       elsif !token || token.expired?
296         flash[:error] = t("users.confirm.unknown token")
297         redirect_to :action => "confirm"
298       else
299         user = token.user
300         user.status = "active"
301         user.email_valid = true
302         flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
303         user.save!
304         referer = token.referer
305         token.destroy
306
307         if session[:token]
308           token = UserToken.find_by(:token => session[:token])
309           session.delete(:token)
310         else
311           token = nil
312         end
313
314         if token.nil? || token.user != user
315           flash[:notice] = t("users.confirm.success")
316           redirect_to :action => :login, :referer => referer
317         else
318           token.destroy
319
320           session[:user] = user.id
321
322           redirect_to referer || welcome_path
323         end
324       end
325     else
326       user = User.find_by(:display_name => params[:display_name])
327
328       redirect_to root_path if user.nil? || user.active?
329     end
330   end
331
332   def confirm_resend
333     user = User.find_by(:display_name => params[:display_name])
334     token = UserToken.find_by(:token => session[:token])
335
336     if user.nil? || token.nil? || token.user != user
337       flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
338     else
339       Notifier.signup_confirm(user, user.tokens.create).deliver_later
340       flash[:notice] = t("users.confirm_resend.success", :email => user.email, :sender => SUPPORT_EMAIL).html_safe
341     end
342
343     redirect_to :action => "login"
344   end
345
346   def confirm_email
347     if request.post?
348       token = UserToken.find_by(:token => params[:confirm_string])
349       if token&.user&.new_email?
350         self.current_user = token.user
351         current_user.email = current_user.new_email
352         current_user.new_email = nil
353         current_user.email_valid = true
354         gravatar_enabled = gravatar_enable(current_user)
355         if current_user.save
356           flash[:notice] = if gravatar_enabled
357                              t("users.confirm_email.success") + " " + gravatar_status_message(current_user)
358                            else
359                              t("users.confirm_email.success")
360                            end
361         else
362           flash[:errors] = current_user.errors
363         end
364         token.destroy
365         session[:user] = current_user.id
366         redirect_to :action => "account", :display_name => current_user.display_name
367       elsif token
368         flash[:error] = t "users.confirm_email.failure"
369         redirect_to :action => "account", :display_name => token.user.display_name
370       else
371         flash[:error] = t "users.confirm_email.unknown_token"
372       end
373     end
374   end
375
376   def api_read
377     if @user.visible?
378       render :action => :api_read, :content_type => "text/xml"
379     else
380       head :gone
381     end
382   end
383
384   def api_details
385     @user = current_user
386     render :action => :api_read, :content_type => "text/xml"
387   end
388
389   def api_users
390     raise OSM::APIBadUserInput, "The parameter users is required, and must be of the form users=id[,id[,id...]]" unless params["users"]
391
392     ids = params["users"].split(",").collect(&:to_i)
393
394     raise OSM::APIBadUserInput, "No users were given to search for" if ids.empty?
395
396     @users = User.visible.find(ids)
397
398     render :action => :api_users, :content_type => "text/xml"
399   end
400
401   def api_gpx_files
402     doc = OSM::API.new.get_xml_doc
403     current_user.traces.reload.each do |trace|
404       doc.root << trace.to_xml_node
405     end
406     render :xml => doc.to_s
407   end
408
409   def show
410     @user = User.find_by(:display_name => params[:display_name])
411
412     if @user &&
413        (@user.visible? || (current_user&.administrator?))
414       @title = @user.display_name
415     else
416       render_unknown_user params[:display_name]
417     end
418   end
419
420   def make_friend
421     @new_friend = User.find_by(:display_name => params[:display_name])
422
423     if @new_friend
424       if request.post?
425         friend = Friend.new
426         friend.befriender = current_user
427         friend.befriendee = @new_friend
428         if current_user.is_friends_with?(@new_friend)
429           flash[:warning] = t "users.make_friend.already_a_friend", :name => @new_friend.display_name
430         elsif friend.save
431           flash[:notice] = t "users.make_friend.success", :name => @new_friend.display_name
432           Notifier.friend_notification(friend).deliver_later
433         else
434           friend.add_error(t("users.make_friend.failed", :name => @new_friend.display_name))
435         end
436
437         if params[:referer]
438           redirect_to params[:referer]
439         else
440           redirect_to :action => "show"
441         end
442       end
443     else
444       render_unknown_user params[:display_name]
445     end
446   end
447
448   def remove_friend
449     @friend = User.find_by(:display_name => params[:display_name])
450
451     if @friend
452       if request.post?
453         if current_user.is_friends_with?(@friend)
454           Friend.where(:user_id => current_user.id, :friend_user_id => @friend.id).delete_all
455           flash[:notice] = t "users.remove_friend.success", :name => @friend.display_name
456         else
457           flash[:error] = t "users.remove_friend.not_a_friend", :name => @friend.display_name
458         end
459
460         if params[:referer]
461           redirect_to params[:referer]
462         else
463           redirect_to :action => "show"
464         end
465       end
466     else
467       render_unknown_user params[:display_name]
468     end
469   end
470
471   ##
472   # sets a user's status
473   def set_status
474     @user.status = params[:status]
475     @user.save
476     redirect_to user_path(:display_name => params[:display_name])
477   end
478
479   ##
480   # delete a user, marking them as deleted and removing personal data
481   def delete
482     @user.delete
483     redirect_to user_path(:display_name => params[:display_name])
484   end
485
486   ##
487   # display a list of users matching specified criteria
488   def index
489     if request.post?
490       ids = params[:user].keys.collect(&:to_i)
491
492       User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
493       User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
494
495       redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
496     else
497       @params = params.permit(:status, :ip)
498
499       conditions = {}
500       conditions[:status] = @params[:status] if @params[:status]
501       conditions[:creation_ip] = @params[:ip] if @params[:ip]
502
503       @user_pages, @users = paginate(:users,
504                                      :conditions => conditions,
505                                      :order => :id,
506                                      :per_page => 50)
507     end
508   end
509
510   ##
511   # omniauth success callback
512   def auth_success
513     auth_info = request.env["omniauth.auth"]
514
515     provider = auth_info[:provider]
516     uid = auth_info[:uid]
517     name = auth_info[:info][:name]
518     email = auth_info[:info][:email]
519
520     case provider
521     when "openid"
522       email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
523                        uid.match(%r{https://me.yahoo.com/(.*)})
524     when "google", "facebook"
525       email_verified = true
526     else
527       email_verified = false
528     end
529
530     if settings = session.delete(:new_user_settings)
531       current_user.auth_provider = provider
532       current_user.auth_uid = uid
533
534       update_user(current_user, settings)
535
536       session[:user_errors] = current_user.errors.as_json
537
538       redirect_to :action => "account", :display_name => current_user.display_name
539     elsif session[:new_user]
540       session[:new_user].auth_provider = provider
541       session[:new_user].auth_uid = uid
542
543       session[:new_user].status = "active" if email_verified && email == session[:new_user].email
544
545       redirect_to :action => "terms"
546     else
547       user = User.find_by(:auth_provider => provider, :auth_uid => uid)
548
549       if user.nil? && provider == "google"
550         openid_url = auth_info[:extra][:id_info]["openid_id"]
551         user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
552         user&.update(:auth_provider => provider, :auth_uid => uid)
553       end
554
555       if user
556         case user.status
557         when "pending" then
558           unconfirmed_login(user)
559         when "active", "confirmed" then
560           successful_login(user, request.env["omniauth.params"]["referer"])
561         when "suspended" then
562           failed_login t("users.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe
563         else
564           failed_login t("users.login.auth failure")
565         end
566       else
567         redirect_to :action => "new", :nickname => name, :email => email,
568                     :auth_provider => provider, :auth_uid => uid
569       end
570     end
571   end
572
573   ##
574   # omniauth failure callback
575   def auth_failure
576     flash[:error] = t("users.auth_failure." + params[:message])
577     redirect_to params[:origin] || login_url
578   end
579
580   private
581
582   ##
583   # handle password authentication
584   def password_authentication(username, password)
585     if user = User.authenticate(:username => username, :password => password)
586       successful_login(user)
587     elsif user = User.authenticate(:username => username, :password => password, :pending => true)
588       unconfirmed_login(user)
589     elsif User.authenticate(:username => username, :password => password, :suspended => true)
590       failed_login t("users.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe, username
591     else
592       failed_login t("users.login.auth failure"), username
593     end
594   end
595
596   ##
597   # return the URL to use for authentication
598   def auth_url(provider, uid, referer = nil)
599     params = { :provider => provider }
600
601     params[:openid_url] = openid_expand_url(uid) if provider == "openid"
602
603     if referer.nil?
604       params[:origin] = request.path
605     else
606       params[:origin] = request.path + "?referer=" + CGI.escape(referer)
607       params[:referer] = referer
608     end
609
610     auth_path(params)
611   end
612
613   ##
614   # special case some common OpenID providers by applying heuristics to
615   # try and come up with the correct URL based on what the user entered
616   def openid_expand_url(openid_url)
617     if openid_url.nil?
618       nil
619     elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
620       # Special case gmail.com as it is potentially a popular OpenID
621       # provider and, unlike yahoo.com, where it works automatically, Google
622       # have hidden their OpenID endpoint somewhere obscure this making it
623       # somewhat less user friendly.
624       "https://www.google.com/accounts/o8/id"
625     else
626       openid_url
627     end
628   end
629
630   ##
631   # process a successful login
632   def successful_login(user, referer = nil)
633     session[:user] = user.id
634     session_expires_after 28.days if session[:remember_me]
635
636     target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
637
638     # The user is logged in, so decide where to send them:
639     #
640     # - If they haven't seen the contributor terms, send them there.
641     # - If they have a block on them, show them that.
642     # - If they were referred to the login, send them back there.
643     # - Otherwise, send them to the home page.
644     if !user.terms_seen
645       redirect_to :action => :terms, :referer => target
646     elsif user.blocked_on_view
647       redirect_to user.blocked_on_view, :referer => target
648     else
649       redirect_to target
650     end
651
652     session.delete(:remember_me)
653     session.delete(:referer)
654   end
655
656   ##
657   # process a failed login
658   def failed_login(message, username = nil)
659     flash[:error] = message
660
661     redirect_to :action => "login", :referer => session[:referer],
662                 :username => username, :remember_me => session[:remember_me]
663
664     session.delete(:remember_me)
665     session.delete(:referer)
666   end
667
668   ##
669   #
670   def unconfirmed_login(user)
671     session[:token] = user.tokens.create.token
672
673     redirect_to :action => "confirm", :display_name => user.display_name
674
675     session.delete(:remember_me)
676     session.delete(:referer)
677   end
678
679   ##
680   # update a user's details
681   def update_user(user, params)
682     user.display_name = params[:user][:display_name]
683     user.new_email = params[:user][:new_email]
684
685     unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
686       user.pass_crypt = params[:user][:pass_crypt]
687       user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
688     end
689
690     if params[:user][:description] != user.description
691       user.description = params[:user][:description]
692       user.description_format = "markdown"
693     end
694
695     user.languages = params[:user][:languages].split(",")
696
697     case params[:image_action]
698     when "new" then
699       user.image = params[:user][:image]
700       user.image_use_gravatar = false
701     when "delete" then
702       user.image = nil
703       user.image_use_gravatar = false
704     when "gravatar" then
705       user.image = nil
706       user.image_use_gravatar = true
707     end
708
709     user.home_lat = params[:user][:home_lat]
710     user.home_lon = params[:user][:home_lon]
711
712     user.preferred_editor = if params[:user][:preferred_editor] == "default"
713                               nil
714                             else
715                               params[:user][:preferred_editor]
716                             end
717
718     if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
719       user.auth_provider = nil
720       user.auth_uid = nil
721     end
722
723     if user.save
724       set_locale(true)
725
726       if user.new_email.blank? || user.new_email == user.email
727         flash.now[:notice] = t "users.account.flash update success"
728       else
729         user.email = user.new_email
730
731         if user.valid?
732           flash.now[:notice] = t "users.account.flash update success confirm needed"
733
734           begin
735             Notifier.email_confirm(user, user.tokens.create).deliver_later
736           rescue StandardError
737             # Ignore errors sending email
738           end
739         else
740           current_user.errors.add(:new_email, current_user.errors[:email])
741           current_user.errors.add(:email, [])
742         end
743
744         user.restore_email!
745       end
746     end
747   end
748
749   ##
750   # require that the user in the URL is the logged in user
751   def require_self
752     head :forbidden if params[:display_name] != current_user.display_name
753   end
754
755   ##
756   # ensure that there is a "user" instance variable
757   def lookup_user_by_id
758     @user = User.find(params[:id])
759   end
760
761   ##
762   # ensure that there is a "user" instance variable
763   def lookup_user_by_name
764     @user = User.find_by(:display_name => params[:display_name])
765   rescue ActiveRecord::RecordNotFound
766     redirect_to :action => "view", :display_name => params[:display_name] unless @user
767   end
768
769   ##
770   #
771   def disable_terms_redirect
772     # this is necessary otherwise going to the user terms page, when
773     # having not agreed already would cause an infinite redirect loop.
774     # it's .now so that this doesn't propagate to other pages.
775     flash.now[:skip_terms] = true
776   end
777
778   ##
779   # return permitted user parameters
780   def user_params
781     params.require(:user).permit(:email, :email_confirmation, :display_name,
782                                  :auth_provider, :auth_uid,
783                                  :pass_crypt, :pass_crypt_confirmation)
784   end
785
786   ##
787   # check signup acls
788   def check_signup_allowed(email = nil)
789     domain = if email.nil?
790                nil
791              else
792                email.split("@").last
793              end
794
795     if blocked = Acl.no_account_creation(request.remote_ip, domain)
796       logger.info "Blocked signup from #{request.remote_ip} for #{email}"
797
798       render :action => "blocked"
799     end
800
801     !blocked
802   end
803
804   ##
805   # check if this user has a gravatar and set the user pref is true
806   def gravatar_enable(user)
807     # code from example https://en.gravatar.com/site/implement/images/ruby/
808     return false if user.image.present?
809
810     hash = Digest::MD5.hexdigest(user.email.downcase)
811     url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
812     response = OSM.http_client.get(URI.parse(url))
813     oldsetting = user.image_use_gravatar
814     user.image_use_gravatar = response.success?
815     oldsetting != user.image_use_gravatar
816   end
817
818   ##
819   # display a message about th current status of the gravatar setting
820   def gravatar_status_message(user)
821     if user.image_use_gravatar
822       t "users.account.gravatar.enabled"
823     else
824       t "users.account.gravatar.disabled"
825     end
826   end
827 end