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