1 class UsersController < ApplicationController
4 skip_before_action :verify_authenticity_token, :only => [:auth_success]
5 before_action :disable_terms_redirect, :only => [:terms, :save, :logout]
6 before_action :authorize_web
7 before_action :set_locale
11 before_action :require_self, :only => [:account]
12 before_action :check_database_readable, :except => [:login]
13 before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public, :make_friend, :remove_friend]
14 before_action :require_cookies, :only => [:new, :login, :confirm]
15 before_action :lookup_user_by_name, :only => [:set_status, :delete]
16 before_action :allow_thirdparty_images, :only => [:show, :account]
19 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || DEFAULT_LEGALE
20 @text = OSM.legal_text_for_country(@legale)
23 render :partial => "terms"
25 @title = t "users.terms.title"
27 if current_user&.terms_agreed?
28 # Already agreed to terms, so just show settings
29 redirect_to :action => :account, :display_name => current_user.display_name
30 elsif current_user.nil? && session[:new_user].nil?
31 redirect_to :action => :login, :referer => request.fullpath
37 @title = t "users.new.title"
41 current_user.terms_seen = true
43 flash[:notice] = t("users.new.terms declined", :url => t("users.new.terms declined url")).html_safe if current_user.save
46 redirect_to params[:referer]
48 redirect_to :action => :account, :display_name => current_user.display_name
51 redirect_to t("users.terms.declined")
54 unless current_user.terms_agreed?
55 current_user.consider_pd = params[:user][:consider_pd]
56 current_user.terms_agreed = Time.now.getutc
57 current_user.terms_seen = true
59 flash[:notice] = t "users.new.terms accepted" if current_user.save
63 redirect_to params[:referer]
65 redirect_to :action => :account, :display_name => current_user.display_name
68 self.current_user = session.delete(:new_user)
70 if check_signup_allowed(current_user.email)
71 current_user.data_public = true
72 current_user.description = "" if current_user.description.nil?
73 current_user.creation_ip = request.remote_ip
74 current_user.languages = http_accept_language.user_preferred_languages
75 current_user.terms_agreed = Time.now.getutc
76 current_user.terms_seen = true
78 if current_user.auth_uid.blank?
79 current_user.auth_provider = nil
80 current_user.auth_uid = nil
84 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
86 referer = welcome_path
89 uri = URI(session[:referer])
90 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
91 editor = Rack::Utils.parse_query(uri.query).slice("editor")
92 referer = welcome_path({ "zoom" => m[1],
94 "lon" => m[3] }.merge(editor))
100 if current_user.status == "active"
101 session[:referer] = referer
102 successful_login(current_user)
104 session[:token] = current_user.tokens.create.token
105 Notifier.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_later
106 redirect_to :action => "confirm", :display_name => current_user.display_name
109 render :action => "new", :referer => params[:referer]
116 @tokens = current_user.oauth_tokens.authorized
118 if params[:user] && params[:user][:display_name] && params[:user][:description]
119 if params[:user][:auth_provider].blank? ||
120 (params[:user][:auth_provider] == current_user.auth_provider &&
121 params[:user][:auth_uid] == current_user.auth_uid)
122 update_user(current_user, params)
124 session[:new_user_settings] = params
125 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
127 elsif errors = session.delete(:user_errors)
128 errors.each do |attribute, error|
129 current_user.errors.add(attribute, error)
132 @title = t "users.account.title"
136 current_user.data_public = true
138 flash[:notice] = t "users.go_public.flash success"
139 redirect_to :action => "account", :display_name => current_user.display_name
143 @title = t "users.lost_password.title"
145 if params[:user] && params[:user][:email]
146 user = User.visible.find_by(:email => params[:user][:email])
149 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
151 user = users.first if users.count == 1
155 token = user.tokens.create
156 Notifier.lost_password(user, token).deliver_later
157 flash[:notice] = t "users.lost_password.notice email on way"
158 redirect_to :action => "login"
160 flash.now[:error] = t "users.lost_password.notice email cannot find"
166 @title = t "users.reset_password.title"
169 token = UserToken.find_by(:token => params[:token])
172 self.current_user = token.user
175 current_user.pass_crypt = params[:user][:pass_crypt]
176 current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
177 current_user.status = "active" if current_user.status == "pending"
178 current_user.email_valid = true
182 flash[:notice] = t "users.reset_password.flash changed"
183 successful_login(current_user)
187 flash[:error] = t "users.reset_password.flash token bad"
188 redirect_to :action => "lost_password"
196 @title = t "users.new.title"
197 @referer = params[:referer] || session[:referer]
199 append_content_security_policy_directives(
200 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
204 # The user is logged in already, so don't show them the signup
205 # page, instead send them to the home page
209 redirect_to :controller => "site", :action => "index"
211 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
212 self.current_user = User.new(:email => params[:email],
213 :email_confirmation => params[:email],
214 :display_name => params[:nickname],
215 :auth_provider => params[:auth_provider],
216 :auth_uid => params[:auth_uid])
218 flash.now[:notice] = render_to_string :partial => "auth_association"
222 self.current_user = User.new
227 self.current_user = User.new(user_params)
229 if check_signup_allowed(current_user.email)
230 session[:referer] = params[:referer]
232 current_user.status = "pending"
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
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)
249 # Save the user record
250 session[:new_user] = current_user
251 redirect_to :action => :terms
257 session[:referer] = params[:referer] if params[:referer]
259 if params[:username].present? && params[:password].present?
260 session[:remember_me] ||= params[:remember_me]
261 password_authentication(params[:username], params[:password])
266 @title = t "users.logout.title"
268 if params[:session] == session.id
270 token = UserToken.find_by(:token => session[:token])
272 session.delete(:token)
274 session.delete(:user)
275 session_expires_automatically
277 redirect_to params[:referer]
279 redirect_to :controller => "site", :action => "index"
286 token = UserToken.find_by(:token => params[:confirm_string])
287 if token&.user&.active?
288 flash[:error] = t("users.confirm.already active")
289 redirect_to :action => "login"
290 elsif !token || token.expired?
291 flash[:error] = t("users.confirm.unknown token")
292 redirect_to :action => "confirm"
295 user.status = "active"
296 user.email_valid = true
297 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
299 referer = token.referer
303 token = UserToken.find_by(:token => session[:token])
304 session.delete(:token)
309 if token.nil? || token.user != user
310 flash[:notice] = t("users.confirm.success")
311 redirect_to :action => :login, :referer => referer
315 session[:user] = user.id
317 redirect_to referer || welcome_path
321 user = User.find_by(:display_name => params[:display_name])
323 redirect_to root_path if user.nil? || user.active?
328 user = User.find_by(:display_name => params[:display_name])
329 token = UserToken.find_by(:token => session[:token])
331 if user.nil? || token.nil? || token.user != user
332 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
334 Notifier.signup_confirm(user, user.tokens.create).deliver_later
335 flash[:notice] = t("users.confirm_resend.success", :email => user.email, :sender => SUPPORT_EMAIL).html_safe
338 redirect_to :action => "login"
343 token = UserToken.find_by(:token => params[:confirm_string])
344 if token&.user&.new_email?
345 self.current_user = token.user
346 current_user.email = current_user.new_email
347 current_user.new_email = nil
348 current_user.email_valid = true
349 gravatar_enabled = gravatar_enable(current_user)
351 flash[:notice] = if gravatar_enabled
352 t("users.confirm_email.success") + " " + gravatar_status_message(current_user)
354 t("users.confirm_email.success")
357 flash[:errors] = current_user.errors
360 session[:user] = current_user.id
361 redirect_to :action => "account", :display_name => current_user.display_name
363 flash[:error] = t "users.confirm_email.failure"
364 redirect_to :action => "account", :display_name => token.user.display_name
366 flash[:error] = t "users.confirm_email.unknown_token"
372 @user = User.find_by(:display_name => params[:display_name])
375 (@user.visible? || (current_user&.administrator?))
376 @title = @user.display_name
378 render_unknown_user params[:display_name]
383 @new_friend = User.find_by(:display_name => params[:display_name])
388 friend.befriender = current_user
389 friend.befriendee = @new_friend
390 if current_user.is_friends_with?(@new_friend)
391 flash[:warning] = t "users.make_friend.already_a_friend", :name => @new_friend.display_name
393 flash[:notice] = t "users.make_friend.success", :name => @new_friend.display_name
394 Notifier.friend_notification(friend).deliver_later
396 friend.add_error(t("users.make_friend.failed", :name => @new_friend.display_name))
400 redirect_to params[:referer]
402 redirect_to :action => "show"
406 render_unknown_user params[:display_name]
411 @friend = User.find_by(:display_name => params[:display_name])
415 if current_user.is_friends_with?(@friend)
416 Friend.where(:user_id => current_user.id, :friend_user_id => @friend.id).delete_all
417 flash[:notice] = t "users.remove_friend.success", :name => @friend.display_name
419 flash[:error] = t "users.remove_friend.not_a_friend", :name => @friend.display_name
423 redirect_to params[:referer]
425 redirect_to :action => "show"
429 render_unknown_user params[:display_name]
434 # sets a user's status
436 @user.status = params[:status]
438 redirect_to user_path(:display_name => params[:display_name])
442 # delete a user, marking them as deleted and removing personal data
445 redirect_to user_path(:display_name => params[:display_name])
449 # display a list of users matching specified criteria
452 ids = params[:user].keys.collect(&:to_i)
454 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
455 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
457 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
459 @params = params.permit(:status, :ip)
462 conditions[:status] = @params[:status] if @params[:status]
463 conditions[:creation_ip] = @params[:ip] if @params[:ip]
465 @user_pages, @users = paginate(:users,
466 :conditions => conditions,
473 # omniauth success callback
475 auth_info = request.env["omniauth.auth"]
477 provider = auth_info[:provider]
478 uid = auth_info[:uid]
479 name = auth_info[:info][:name]
480 email = auth_info[:info][:email]
484 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
485 uid.match(%r{https://me.yahoo.com/(.*)})
486 when "google", "facebook"
487 email_verified = true
489 email_verified = false
492 if settings = session.delete(:new_user_settings)
493 current_user.auth_provider = provider
494 current_user.auth_uid = uid
496 update_user(current_user, settings)
498 session[:user_errors] = current_user.errors.as_json
500 redirect_to :action => "account", :display_name => current_user.display_name
501 elsif session[:new_user]
502 session[:new_user].auth_provider = provider
503 session[:new_user].auth_uid = uid
505 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
507 redirect_to :action => "terms"
509 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
511 if user.nil? && provider == "google"
512 openid_url = auth_info[:extra][:id_info]["openid_id"]
513 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
514 user&.update(:auth_provider => provider, :auth_uid => uid)
520 unconfirmed_login(user)
521 when "active", "confirmed" then
522 successful_login(user, request.env["omniauth.params"]["referer"])
523 when "suspended" then
524 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe
526 failed_login t("users.login.auth failure")
529 redirect_to :action => "new", :nickname => name, :email => email,
530 :auth_provider => provider, :auth_uid => uid
536 # omniauth failure callback
538 flash[:error] = t("users.auth_failure." + params[:message])
539 redirect_to params[:origin] || login_url
545 # handle password authentication
546 def password_authentication(username, password)
547 if user = User.authenticate(:username => username, :password => password)
548 successful_login(user)
549 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
550 unconfirmed_login(user)
551 elsif User.authenticate(:username => username, :password => password, :suspended => true)
552 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe, username
554 failed_login t("users.login.auth failure"), username
559 # return the URL to use for authentication
560 def auth_url(provider, uid, referer = nil)
561 params = { :provider => provider }
563 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
566 params[:origin] = request.path
568 params[:origin] = request.path + "?referer=" + CGI.escape(referer)
569 params[:referer] = referer
576 # special case some common OpenID providers by applying heuristics to
577 # try and come up with the correct URL based on what the user entered
578 def openid_expand_url(openid_url)
581 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
582 # Special case gmail.com as it is potentially a popular OpenID
583 # provider and, unlike yahoo.com, where it works automatically, Google
584 # have hidden their OpenID endpoint somewhere obscure this making it
585 # somewhat less user friendly.
586 "https://www.google.com/accounts/o8/id"
593 # process a successful login
594 def successful_login(user, referer = nil)
595 session[:user] = user.id
596 session_expires_after 28.days if session[:remember_me]
598 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
600 # The user is logged in, so decide where to send them:
602 # - If they haven't seen the contributor terms, send them there.
603 # - If they have a block on them, show them that.
604 # - If they were referred to the login, send them back there.
605 # - Otherwise, send them to the home page.
607 redirect_to :action => :terms, :referer => target
608 elsif user.blocked_on_view
609 redirect_to user.blocked_on_view, :referer => target
614 session.delete(:remember_me)
615 session.delete(:referer)
619 # process a failed login
620 def failed_login(message, username = nil)
621 flash[:error] = message
623 redirect_to :action => "login", :referer => session[:referer],
624 :username => username, :remember_me => session[:remember_me]
626 session.delete(:remember_me)
627 session.delete(:referer)
632 def unconfirmed_login(user)
633 session[:token] = user.tokens.create.token
635 redirect_to :action => "confirm", :display_name => user.display_name
637 session.delete(:remember_me)
638 session.delete(:referer)
642 # update a user's details
643 def update_user(user, params)
644 user.display_name = params[:user][:display_name]
645 user.new_email = params[:user][:new_email]
647 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
648 user.pass_crypt = params[:user][:pass_crypt]
649 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
652 if params[:user][:description] != user.description
653 user.description = params[:user][:description]
654 user.description_format = "markdown"
657 user.languages = params[:user][:languages].split(",")
659 case params[:image_action]
661 user.image = params[:user][:image]
662 user.image_use_gravatar = false
665 user.image_use_gravatar = false
668 user.image_use_gravatar = true
671 user.home_lat = params[:user][:home_lat]
672 user.home_lon = params[:user][:home_lon]
674 user.preferred_editor = if params[:user][:preferred_editor] == "default"
677 params[:user][:preferred_editor]
680 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
681 user.auth_provider = nil
688 if user.new_email.blank? || user.new_email == user.email
689 flash.now[:notice] = t "users.account.flash update success"
691 user.email = user.new_email
694 flash.now[:notice] = t "users.account.flash update success confirm needed"
697 Notifier.email_confirm(user, user.tokens.create).deliver_later
699 # Ignore errors sending email
702 current_user.errors.add(:new_email, current_user.errors[:email])
703 current_user.errors.add(:email, [])
712 # require that the user in the URL is the logged in user
714 head :forbidden if params[:display_name] != current_user.display_name
718 # ensure that there is a "user" instance variable
719 def lookup_user_by_name
720 @user = User.find_by(:display_name => params[:display_name])
721 rescue ActiveRecord::RecordNotFound
722 redirect_to :action => "view", :display_name => params[:display_name] unless @user
727 def disable_terms_redirect
728 # this is necessary otherwise going to the user terms page, when
729 # having not agreed already would cause an infinite redirect loop.
730 # it's .now so that this doesn't propagate to other pages.
731 flash.now[:skip_terms] = true
735 # return permitted user parameters
737 params.require(:user).permit(:email, :email_confirmation, :display_name,
738 :auth_provider, :auth_uid,
739 :pass_crypt, :pass_crypt_confirmation)
744 def check_signup_allowed(email = nil)
745 domain = if email.nil?
748 email.split("@").last
751 if blocked = Acl.no_account_creation(request.remote_ip, domain)
752 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
754 render :action => "blocked"
761 # check if this user has a gravatar and set the user pref is true
762 def gravatar_enable(user)
763 # code from example https://en.gravatar.com/site/implement/images/ruby/
764 return false if user.image.present?
766 hash = Digest::MD5.hexdigest(user.email.downcase)
767 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
768 response = OSM.http_client.get(URI.parse(url))
769 oldsetting = user.image_use_gravatar
770 user.image_use_gravatar = response.success?
771 oldsetting != user.image_use_gravatar
775 # display a message about th current status of the gravatar setting
776 def gravatar_status_message(user)
777 if user.image_use_gravatar
778 t "users.account.gravatar.enabled"
780 t "users.account.gravatar.disabled"