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