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])
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])
269 @title = t "users.logout.title"
271 if params[:session] == session.id
273 token = UserToken.find_by(:token => session[:token])
275 session.delete(:token)
277 session.delete(:user)
278 session_expires_automatically
280 redirect_to params[:referer]
282 redirect_to :controller => "site", :action => "index"
289 token = UserToken.find_by(:token => params[:confirm_string])
290 if token&.user&.active?
291 flash[:error] = t("users.confirm.already active")
292 redirect_to :action => "login"
293 elsif !token || token.expired?
294 flash[:error] = t("users.confirm.unknown token")
295 redirect_to :action => "confirm"
298 user.status = "active"
299 user.email_valid = true
300 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
302 referer = token.referer
306 token = UserToken.find_by(:token => session[:token])
307 session.delete(:token)
312 if token.nil? || token.user != user
313 flash[:notice] = t("users.confirm.success")
314 redirect_to :action => :login, :referer => referer
318 session[:user] = user.id
320 redirect_to referer || welcome_path
324 user = User.find_by(:display_name => params[:display_name])
326 redirect_to root_path if user.nil? || user.active?
331 user = User.find_by(:display_name => params[:display_name])
332 token = UserToken.find_by(:token => session[:token])
334 if user.nil? || token.nil? || token.user != user
335 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
337 Notifier.signup_confirm(user, user.tokens.create).deliver_later
338 flash[:notice] = t("users.confirm_resend.success", :email => user.email, :sender => SUPPORT_EMAIL).html_safe
341 redirect_to :action => "login"
346 token = UserToken.find_by(:token => params[:confirm_string])
347 if 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)
354 flash[:notice] = if gravatar_enabled
355 t("users.confirm_email.success") + " " + gravatar_status_message(current_user)
357 t("users.confirm_email.success")
360 flash[:errors] = current_user.errors
363 session[:user] = current_user.id
364 redirect_to :action => "account", :display_name => current_user.display_name
366 flash[:error] = t "users.confirm_email.failure"
367 redirect_to :action => "account", :display_name => token.user.display_name
369 flash[:error] = t "users.confirm_email.unknown_token"
375 @user = User.find_by(:display_name => params[:display_name])
378 (@user.visible? || (current_user&.administrator?))
379 @title = @user.display_name
381 render_unknown_user params[:display_name]
386 @new_friend = User.find_by(:display_name => params[:display_name])
391 friend.befriender = current_user
392 friend.befriendee = @new_friend
393 if current_user.is_friends_with?(@new_friend)
394 flash[:warning] = t "users.make_friend.already_a_friend", :name => @new_friend.display_name
396 flash[:notice] = t "users.make_friend.success", :name => @new_friend.display_name
397 Notifier.friend_notification(friend).deliver_later
399 friend.add_error(t("users.make_friend.failed", :name => @new_friend.display_name))
403 redirect_to params[:referer]
405 redirect_to :action => "show"
409 render_unknown_user params[:display_name]
414 @friend = User.find_by(:display_name => params[:display_name])
418 if current_user.is_friends_with?(@friend)
419 Friend.where(:user_id => current_user.id, :friend_user_id => @friend.id).delete_all
420 flash[:notice] = t "users.remove_friend.success", :name => @friend.display_name
422 flash[:error] = t "users.remove_friend.not_a_friend", :name => @friend.display_name
426 redirect_to params[:referer]
428 redirect_to :action => "show"
432 render_unknown_user params[:display_name]
437 # sets a user's status
439 @user.status = params[:status]
441 redirect_to user_path(:display_name => params[:display_name])
445 # delete a user, marking them as deleted and removing personal data
448 redirect_to user_path(:display_name => params[:display_name])
452 # display a list of users matching specified criteria
455 ids = params[:user].keys.collect(&:to_i)
457 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
458 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
460 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
462 @params = params.permit(:status, :ip)
465 conditions[:status] = @params[:status] if @params[:status]
466 conditions[:creation_ip] = @params[:ip] if @params[:ip]
468 @user_pages, @users = paginate(:users,
469 :conditions => conditions,
476 # omniauth success callback
478 auth_info = request.env["omniauth.auth"]
480 provider = auth_info[:provider]
481 uid = auth_info[:uid]
482 name = auth_info[:info][:name]
483 email = auth_info[:info][:email]
487 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
488 uid.match(%r{https://me.yahoo.com/(.*)})
489 when "google", "facebook"
490 email_verified = true
492 email_verified = false
495 if settings = session.delete(:new_user_settings)
496 current_user.auth_provider = provider
497 current_user.auth_uid = uid
499 update_user(current_user, settings)
501 session[:user_errors] = current_user.errors.as_json
503 redirect_to :action => "account", :display_name => current_user.display_name
504 elsif session[:new_user]
505 session[:new_user].auth_provider = provider
506 session[:new_user].auth_uid = uid
508 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
510 redirect_to :action => "terms"
512 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
514 if user.nil? && provider == "google"
515 openid_url = auth_info[:extra][:id_info]["openid_id"]
516 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
517 user&.update(:auth_provider => provider, :auth_uid => uid)
523 unconfirmed_login(user)
524 when "active", "confirmed" then
525 successful_login(user, request.env["omniauth.params"]["referer"])
526 when "suspended" then
527 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe
529 failed_login t("users.login.auth failure")
532 redirect_to :action => "new", :nickname => name, :email => email,
533 :auth_provider => provider, :auth_uid => uid
539 # omniauth failure callback
541 flash[:error] = t("users.auth_failure." + params[:message])
542 redirect_to params[:origin] || login_url
548 # handle password authentication
549 def password_authentication(username, password)
550 if user = User.authenticate(:username => username, :password => password)
551 successful_login(user)
552 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
553 unconfirmed_login(user)
554 elsif User.authenticate(:username => username, :password => password, :suspended => true)
555 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{SUPPORT_EMAIL}").html_safe, username
557 failed_login t("users.login.auth failure"), username
562 # return the URL to use for authentication
563 def auth_url(provider, uid, referer = nil)
564 params = { :provider => provider }
566 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
569 params[:origin] = request.path
571 params[:origin] = request.path + "?referer=" + CGI.escape(referer)
572 params[:referer] = referer
579 # special case some common OpenID providers by applying heuristics to
580 # try and come up with the correct URL based on what the user entered
581 def openid_expand_url(openid_url)
584 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
585 # Special case gmail.com as it is potentially a popular OpenID
586 # provider and, unlike yahoo.com, where it works automatically, Google
587 # have hidden their OpenID endpoint somewhere obscure this making it
588 # somewhat less user friendly.
589 "https://www.google.com/accounts/o8/id"
596 # process a successful login
597 def successful_login(user, referer = nil)
598 session[:user] = user.id
599 session_expires_after 28.days if session[:remember_me]
601 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
603 # The user is logged in, so decide where to send them:
605 # - If they haven't seen the contributor terms, send them there.
606 # - If they have a block on them, show them that.
607 # - If they were referred to the login, send them back there.
608 # - Otherwise, send them to the home page.
610 redirect_to :action => :terms, :referer => target
611 elsif user.blocked_on_view
612 redirect_to user.blocked_on_view, :referer => target
617 session.delete(:remember_me)
618 session.delete(:referer)
622 # process a failed login
623 def failed_login(message, username = nil)
624 flash[:error] = message
626 redirect_to :action => "login", :referer => session[:referer],
627 :username => username, :remember_me => session[:remember_me]
629 session.delete(:remember_me)
630 session.delete(:referer)
635 def unconfirmed_login(user)
636 session[:token] = user.tokens.create.token
638 redirect_to :action => "confirm", :display_name => user.display_name
640 session.delete(:remember_me)
641 session.delete(:referer)
645 # update a user's details
646 def update_user(user, params)
647 user.display_name = params[:user][:display_name]
648 user.new_email = params[:user][:new_email]
650 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
651 user.pass_crypt = params[:user][:pass_crypt]
652 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
655 if params[:user][:description] != user.description
656 user.description = params[:user][:description]
657 user.description_format = "markdown"
660 user.languages = params[:user][:languages].split(",")
662 case params[:image_action]
664 user.image = params[:user][:image]
665 user.image_use_gravatar = false
668 user.image_use_gravatar = false
671 user.image_use_gravatar = true
674 user.home_lat = params[:user][:home_lat]
675 user.home_lon = params[:user][:home_lon]
677 user.preferred_editor = if params[:user][:preferred_editor] == "default"
680 params[:user][:preferred_editor]
683 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
684 user.auth_provider = nil
691 if user.new_email.blank? || user.new_email == user.email
692 flash.now[:notice] = t "users.account.flash update success"
694 user.email = user.new_email
697 flash.now[:notice] = t "users.account.flash update success confirm needed"
700 Notifier.email_confirm(user, user.tokens.create).deliver_later
702 # Ignore errors sending email
705 current_user.errors.add(:new_email, current_user.errors[:email])
706 current_user.errors.add(:email, [])
715 # require that the user in the URL is the logged in user
717 head :forbidden if params[:display_name] != current_user.display_name
721 # ensure that there is a "user" instance variable
722 def lookup_user_by_name
723 @user = User.find_by(:display_name => params[:display_name])
724 rescue ActiveRecord::RecordNotFound
725 redirect_to :action => "view", :display_name => params[:display_name] unless @user
730 def disable_terms_redirect
731 # this is necessary otherwise going to the user terms page, when
732 # having not agreed already would cause an infinite redirect loop.
733 # it's .now so that this doesn't propagate to other pages.
734 flash.now[:skip_terms] = true
738 # return permitted user parameters
740 params.require(:user).permit(:email, :email_confirmation, :display_name,
741 :auth_provider, :auth_uid,
742 :pass_crypt, :pass_crypt_confirmation)
747 def check_signup_allowed(email = nil)
748 domain = if email.nil?
751 email.split("@").last
754 if blocked = Acl.no_account_creation(request.remote_ip, domain)
755 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
757 render :action => "blocked"
764 # check if this user has a gravatar and set the user pref is true
765 def gravatar_enable(user)
766 # code from example https://en.gravatar.com/site/implement/images/ruby/
767 return false if user.image.present?
769 hash = Digest::MD5.hexdigest(user.email.downcase)
770 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
771 response = OSM.http_client.get(URI.parse(url))
772 oldsetting = user.image_use_gravatar
773 user.image_use_gravatar = response.success?
774 oldsetting != user.image_use_gravatar
778 # display a message about th current status of the gravatar setting
779 def gravatar_status_message(user)
780 if user.image_use_gravatar
781 t "users.account.gravatar.enabled"
783 t "users.account.gravatar.disabled"