]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_controller.rb
Convert @user to current_user
[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 current_user && current_user.terms_agreed?
32         # Already agreed to terms, so just show settings
33         redirect_to :action => :account, :display_name => current_user.display_name
34       elsif current_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 current_user
45         current_user.terms_seen = true
46
47         if current_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 => current_user.display_name
55         end
56       else
57         redirect_to t("user.terms.declined")
58       end
59     elsif current_user
60       unless current_user.terms_agreed?
61         current_user.consider_pd = params[:user][:consider_pd]
62         current_user.terms_agreed = Time.now.getutc
63         current_user.terms_seen = true
64
65         flash[:notice] = t "user.new.terms accepted" if current_user.save
66       end
67
68       if params[:referer]
69         redirect_to params[:referer]
70       else
71         redirect_to :action => :account, :display_name => current_user.display_name
72       end
73     else
74       self.current_user = session.delete(:new_user)
75
76       if check_signup_allowed(current_user.email)
77         current_user.data_public = true
78         current_user.description = "" if current_user.description.nil?
79         current_user.creation_ip = request.remote_ip
80         current_user.languages = http_accept_language.user_preferred_languages
81         current_user.terms_agreed = Time.now.getutc
82         current_user.terms_seen = true
83
84         if current_user.auth_uid.blank?
85           current_user.auth_provider = nil
86           current_user.auth_uid = nil
87         end
88
89         if current_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 current_user.status == "active"
107             session[:referer] = referer
108             successful_login(current_user)
109           else
110             session[:token] = current_user.tokens.create.token
111             Notifier.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_now
112             redirect_to :action => "confirm", :display_name => current_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 = current_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] == current_user.auth_provider &&
128           params[:user][:auth_uid] == current_user.auth_uid)
129         update_user(current_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         current_user.errors.add(attribute, error)
137       end
138     end
139   end
140
141   def go_public
142     current_user.data_public = true
143     current_user.save
144     flash[:notice] = t "user.go_public.flash success"
145     redirect_to :action => "account", :display_name => current_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         self.current_user = token.user
179
180         if params[:user]
181           current_user.pass_crypt = params[:user][:pass_crypt]
182           current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
183           current_user.status = "active" if current_user.status == "pending"
184           current_user.email_valid = true
185
186           if current_user.save
187             token.destroy
188             flash[:notice] = t "user.reset_password.flash changed"
189             successful_login(current_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       head :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 current_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       self.current_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     self.current_user = User.new(user_params)
228
229     if check_signup_allowed(current_user.email)
230       session[:referer] = params[:referer]
231
232       current_user.status = "pending"
233
234       if current_user.auth_provider.present? && current_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         current_user.pass_crypt = SecureRandom.base64(16)
238         current_user.pass_crypt_confirmation = current_user.pass_crypt
239       end
240
241       if current_user.invalid?
242         # Something is wrong with a new user, so rerender the form
243         render :action => "new"
244       elsif current_user.auth_provider.present?
245         # Verify external authenticator before moving on
246         session[:new_user] = current_user
247         redirect_to auth_url(current_user.auth_provider, current_user.auth_uid)
248       else
249         # Save the user record
250         session[:new_user] = current_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] == session.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         flash[:notice] = gravatar_status_message(user) if 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         self.current_user = token.user
349         current_user.email = current_user.new_email
350         current_user.new_email = nil
351         current_user.email_valid = true
352         gravatar_enabled = gravatar_enable(current_user)
353         if current_user.save
354           flash[:notice] = if gravatar_enabled
355                              t("user.confirm_email.success") + " " + gravatar_status_message(current_user)
356                            else
357                              t("user.confirm_email.success")
358                            end
359         else
360           flash[:errors] = current_user.errors
361         end
362         token.destroy
363         session[:user] = current_user.id
364         redirect_to :action => "account", :display_name => current_user.display_name
365       elsif token
366         flash[:error] = t "user.confirm_email.failure"
367         redirect_to :action => "account", :display_name => token.user.display_name
368       else
369         flash[:error] = t "user.confirm_email.unknown_token"
370       end
371     end
372   end
373
374   def api_read
375     if @this_user.visible?
376       render :action => :api_read, :content_type => "text/xml"
377     else
378       head :gone
379     end
380   end
381
382   def api_details
383     @this_user = current_user
384     render :action => :api_read, :content_type => "text/xml"
385   end
386
387   def api_gpx_files
388     doc = OSM::API.new.get_xml_doc
389     current_user.traces.reload.each do |trace|
390       doc.root << trace.to_xml_node
391     end
392     render :xml => doc.to_s
393   end
394
395   def view
396     @this_user = User.find_by(:display_name => params[:display_name])
397
398     if @this_user &&
399        (@this_user.visible? || (current_user && current_user.administrator?))
400       @title = @this_user.display_name
401     else
402       render_unknown_user params[:display_name]
403     end
404   end
405
406   def make_friend
407     @new_friend = User.find_by(:display_name => params[:display_name])
408
409     if @new_friend
410       if request.post?
411         friend = Friend.new
412         friend.user_id = current_user.id
413         friend.friend_user_id = @new_friend.id
414         if current_user.is_friends_with?(@new_friend)
415           flash[:warning] = t "user.make_friend.already_a_friend", :name => @new_friend.display_name
416         elsif friend.save
417           flash[:notice] = t "user.make_friend.success", :name => @new_friend.display_name
418           Notifier.friend_notification(friend).deliver_now
419         else
420           friend.add_error(t("user.make_friend.failed", :name => @new_friend.display_name))
421         end
422
423         if params[:referer]
424           redirect_to params[:referer]
425         else
426           redirect_to :action => "view"
427         end
428       end
429     else
430       render_unknown_user params[:display_name]
431     end
432   end
433
434   def remove_friend
435     @friend = User.find_by(:display_name => params[:display_name])
436
437     if @friend
438       if request.post?
439         if current_user.is_friends_with?(@friend)
440           Friend.where(:user_id => current_user.id, :friend_user_id => @friend.id).delete_all
441           flash[:notice] = t "user.remove_friend.success", :name => @friend.display_name
442         else
443           flash[:error] = t "user.remove_friend.not_a_friend", :name => @friend.display_name
444         end
445
446         if params[:referer]
447           redirect_to params[:referer]
448         else
449           redirect_to :action => "view"
450         end
451       end
452     else
453       render_unknown_user params[:display_name]
454     end
455   end
456
457   ##
458   # sets a user's status
459   def set_status
460     @this_user.status = params[:status]
461     @this_user.save
462     redirect_to :action => "view", :display_name => params[:display_name]
463   end
464
465   ##
466   # delete a user, marking them as deleted and removing personal data
467   def delete
468     @this_user.delete
469     redirect_to :action => "view", :display_name => params[:display_name]
470   end
471
472   ##
473   # display a list of users matching specified criteria
474   def list
475     if request.post?
476       ids = params[:user].keys.collect(&:to_i)
477
478       User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
479       User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
480
481       redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
482     else
483       @params = params.permit(:status, :ip)
484
485       conditions = {}
486       conditions[:status] = @params[:status] if @params[:status]
487       conditions[:creation_ip] = @params[:ip] if @params[:ip]
488
489       @user_pages, @users = paginate(:users,
490                                      :conditions => conditions,
491                                      :order => :id,
492                                      :per_page => 50)
493     end
494   end
495
496   ##
497   # omniauth success callback
498   def auth_success
499     auth_info = request.env["omniauth.auth"]
500
501     provider = auth_info[:provider]
502     uid = auth_info[:uid]
503     name = auth_info[:info][:name]
504     email = auth_info[:info][:email]
505
506     case provider
507     when "openid"
508       email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
509                        uid.match(%r{https://me.yahoo.com/(.*)})
510     when "google", "facebook"
511       email_verified = true
512     else
513       email_verified = false
514     end
515
516     if settings = session.delete(:new_user_settings)
517       current_user.auth_provider = provider
518       current_user.auth_uid = uid
519
520       update_user(current_user, settings)
521
522       session[:user_errors] = current_user.errors.as_json
523
524       redirect_to :action => "account", :display_name => current_user.display_name
525     elsif session[:new_user]
526       session[:new_user].auth_provider = provider
527       session[:new_user].auth_uid = uid
528
529       if email_verified && email == session[:new_user].email
530         session[:new_user].status = "active"
531       end
532
533       redirect_to :action => "terms"
534     else
535       user = User.find_by(:auth_provider => provider, :auth_uid => uid)
536
537       if user.nil? && provider == "google"
538         openid_url = auth_info[:extra][:id_info]["openid_id"]
539         user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
540         user.update(:auth_provider => provider, :auth_uid => uid) if user
541       end
542
543       if user
544         case user.status
545         when "pending" then
546           unconfirmed_login(user)
547         when "active", "confirmed" then
548           successful_login(user, request.env["omniauth.params"]["referer"])
549         when "suspended" then
550           failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}")
551         else
552           failed_login t("user.login.auth failure")
553         end
554       else
555         redirect_to :action => "new", :nickname => name, :email => email,
556                     :auth_provider => provider, :auth_uid => uid
557       end
558     end
559   end
560
561   ##
562   # omniauth failure callback
563   def auth_failure
564     flash[:error] = t("user.auth_failure." + params[:message])
565     redirect_to params[:origin] || login_url
566   end
567
568   private
569
570   ##
571   # handle password authentication
572   def password_authentication(username, password)
573     if user = User.authenticate(:username => username, :password => password)
574       successful_login(user)
575     elsif user = User.authenticate(:username => username, :password => password, :pending => true)
576       unconfirmed_login(user)
577     elsif User.authenticate(:username => username, :password => password, :suspended => true)
578       failed_login t("user.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}"), username
579     else
580       failed_login t("user.login.auth failure"), username
581     end
582   end
583
584   ##
585   # return the URL to use for authentication
586   def auth_url(provider, uid, referer = nil)
587     params = { :provider => provider }
588
589     params[:openid_url] = openid_expand_url(uid) if provider == "openid"
590
591     if referer.nil?
592       params[:origin] = request.path
593     else
594       params[:origin] = request.path + "?referer=" + CGI.escape(referer)
595       params[:referer] = referer
596     end
597
598     auth_path(params)
599   end
600
601   ##
602   # special case some common OpenID providers by applying heuristics to
603   # try and come up with the correct URL based on what the user entered
604   def openid_expand_url(openid_url)
605     if openid_url.nil?
606       nil
607     elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
608       # Special case gmail.com as it is potentially a popular OpenID
609       # provider and, unlike yahoo.com, where it works automatically, Google
610       # have hidden their OpenID endpoint somewhere obscure this making it
611       # somewhat less user friendly.
612       "https://www.google.com/accounts/o8/id"
613     else
614       openid_url
615     end
616   end
617
618   ##
619   # process a successful login
620   def successful_login(user, referer = nil)
621     session[:user] = user.id
622     session_expires_after 28.days if session[:remember_me]
623
624     target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
625
626     # The user is logged in, so decide where to send them:
627     #
628     # - If they haven't seen the contributor terms, send them there.
629     # - If they have a block on them, show them that.
630     # - If they were referred to the login, send them back there.
631     # - Otherwise, send them to the home page.
632     if REQUIRE_TERMS_SEEN && !user.terms_seen
633       redirect_to :action => :terms, :referer => target
634     elsif user.blocked_on_view
635       redirect_to user.blocked_on_view, :referer => target
636     else
637       redirect_to target
638     end
639
640     session.delete(:remember_me)
641     session.delete(:referer)
642   end
643
644   ##
645   # process a failed login
646   def failed_login(message, username = nil)
647     flash[:error] = message
648
649     redirect_to :action => "login", :referer => session[:referer],
650                 :username => username, :remember_me => session[:remember_me]
651
652     session.delete(:remember_me)
653     session.delete(:referer)
654   end
655
656   ##
657   #
658   def unconfirmed_login(user)
659     session[:token] = user.tokens.create.token
660
661     redirect_to :action => "confirm", :display_name => user.display_name
662
663     session.delete(:remember_me)
664     session.delete(:referer)
665   end
666
667   ##
668   # update a user's details
669   def update_user(user, params)
670     user.display_name = params[:user][:display_name]
671     user.new_email = params[:user][:new_email]
672
673     unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
674       user.pass_crypt = params[:user][:pass_crypt]
675       user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
676     end
677
678     if params[:user][:description] != user.description
679       user.description = params[:user][:description]
680       user.description_format = "markdown"
681     end
682
683     user.languages = params[:user][:languages].split(",")
684
685     case params[:image_action]
686     when "new" then
687       user.image = params[:user][:image]
688       user.image_use_gravatar = false
689     when "delete" then
690       user.image = nil
691       user.image_use_gravatar = false
692     when "gravatar" then
693       user.image = nil
694       user.image_use_gravatar = true
695     end
696
697     user.home_lat = params[:user][:home_lat]
698     user.home_lon = params[:user][:home_lon]
699
700     user.preferred_editor = if params[:user][:preferred_editor] == "default"
701                               nil
702                             else
703                               params[:user][:preferred_editor]
704                             end
705
706     if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
707       user.auth_provider = nil
708       user.auth_uid = nil
709     end
710
711     if user.save
712       set_locale
713
714       if user.new_email.blank? || user.new_email == user.email
715         flash.now[:notice] = t "user.account.flash update success"
716       else
717         user.email = user.new_email
718
719         if user.valid?
720           flash.now[:notice] = t "user.account.flash update success confirm needed"
721
722           begin
723             Notifier.email_confirm(user, user.tokens.create).deliver_now
724           rescue
725             # Ignore errors sending email
726           end
727         else
728           current_user.errors.add(:new_email, current_user.errors[:email])
729           current_user.errors.add(:email, [])
730         end
731
732         user.restore_email!
733       end
734     end
735   end
736
737   ##
738   # require that the user is a administrator, or fill out a helpful error message
739   # and return them to the user page.
740   def require_administrator
741     if current_user && !current_user.administrator?
742       flash[:error] = t("user.filter.not_an_administrator")
743
744       if params[:display_name]
745         redirect_to :action => "view", :display_name => params[:display_name]
746       else
747         redirect_to :action => "login", :referer => request.fullpath
748       end
749     elsif !current_user
750       redirect_to :action => "login", :referer => request.fullpath
751     end
752   end
753
754   ##
755   # require that the user in the URL is the logged in user
756   def require_self
757     head :forbidden if params[:display_name] != current_user.display_name
758   end
759
760   ##
761   # ensure that there is a "this_user" instance variable
762   def lookup_user_by_id
763     @this_user = User.find(params[:id])
764   end
765
766   ##
767   # ensure that there is a "this_user" instance variable
768   def lookup_user_by_name
769     @this_user = User.find_by(:display_name => params[:display_name])
770   rescue ActiveRecord::RecordNotFound
771     redirect_to :action => "view", :display_name => params[:display_name] unless @this_user
772   end
773
774   ##
775   #
776   def disable_terms_redirect
777     # this is necessary otherwise going to the user terms page, when
778     # having not agreed already would cause an infinite redirect loop.
779     # it's .now so that this doesn't propagate to other pages.
780     flash.now[:skip_terms] = true
781   end
782
783   ##
784   # return permitted user parameters
785   def user_params
786     params.require(:user).permit(:email, :email_confirmation, :display_name,
787                                  :auth_provider, :auth_uid,
788                                  :pass_crypt, :pass_crypt_confirmation)
789   end
790
791   ##
792   # check signup acls
793   def check_signup_allowed(email = nil)
794     domain = if email.nil?
795                nil
796              else
797                email.split("@").last
798              end
799
800     if blocked = Acl.no_account_creation(request.remote_ip, domain)
801       logger.info "Blocked signup from #{request.remote_ip} for #{email}"
802
803       render :action => "blocked"
804     end
805
806     !blocked
807   end
808
809   ##
810   # check if this user has a gravatar and set the user pref is true
811   def gravatar_enable(user)
812     # code from example https://en.gravatar.com/site/implement/images/ruby/
813     return false if user.image.present?
814     hash = Digest::MD5.hexdigest(user.email.downcase)
815     url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
816     response = OSM.http_client.get(URI.parse(url))
817     oldsetting = user.image_use_gravatar
818     user.image_use_gravatar = response.success?
819     oldsetting != user.image_use_gravatar
820   end
821
822   ##
823   # display a message about th current status of the gravatar setting
824   def gravatar_status_message(user)
825     if user.image_use_gravatar
826       t "user.account.gravatar.enabled"
827     else
828       t "user.account.gravatar.disabled"
829     end
830   end
831 end