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