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
8 before_action :check_database_readable
12 before_action :require_self, :only => [:account]
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) || Settings.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"
39 if params[:decline] || !(params[:read_tou] && params[:read_ct])
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
50 elsif params[:decline]
51 redirect_to t("users.terms.declined")
53 redirect_to :action => :terms
56 unless current_user.terms_agreed?
57 current_user.consider_pd = params[:user][:consider_pd]
58 current_user.tou_agreed = Time.now.getutc
59 current_user.terms_agreed = Time.now.getutc
60 current_user.terms_seen = true
62 flash[:notice] = t "users.new.terms accepted" if current_user.save
66 redirect_to params[:referer]
68 redirect_to :action => :account, :display_name => current_user.display_name
71 self.current_user = session.delete(:new_user)
73 if check_signup_allowed(current_user.email)
74 current_user.data_public = true
75 current_user.description = "" if current_user.description.nil?
76 current_user.creation_ip = request.remote_ip
77 current_user.languages = http_accept_language.user_preferred_languages
78 current_user.terms_agreed = Time.now.getutc
79 current_user.tou_agreed = Time.now.getutc
80 current_user.terms_seen = true
82 if current_user.auth_uid.blank?
83 current_user.auth_provider = nil
84 current_user.auth_uid = nil
88 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
90 referer = welcome_path
93 uri = URI(session[:referer])
94 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
95 editor = Rack::Utils.parse_query(uri.query).slice("editor")
96 referer = welcome_path({ "zoom" => m[1],
98 "lon" => m[3] }.merge(editor))
104 if current_user.status == "active"
105 session[:referer] = referer
106 successful_login(current_user)
108 session[:token] = current_user.tokens.create.token
109 Notifier.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_later
110 redirect_to :action => "confirm", :display_name => current_user.display_name
113 render :action => "new", :referer => params[:referer]
120 @tokens = current_user.oauth_tokens.authorized
122 if params[:user] && params[:user][:display_name] && params[:user][:description]
123 if params[:user][:auth_provider].blank? ||
124 (params[:user][:auth_provider] == current_user.auth_provider &&
125 params[:user][:auth_uid] == current_user.auth_uid)
126 update_user(current_user, params)
128 session[:new_user_settings] = params
129 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid])
131 elsif errors = session.delete(:user_errors)
132 errors.each do |attribute, error|
133 current_user.errors.add(attribute, error)
136 @title = t "users.account.title"
140 current_user.data_public = true
142 flash[:notice] = t "users.go_public.flash success"
143 redirect_to :action => "account", :display_name => current_user.display_name
147 @title = t "users.lost_password.title"
149 if params[:user] && params[:user][:email]
150 user = User.visible.find_by(:email => params[:user][:email])
153 users = User.visible.where("LOWER(email) = LOWER(?)", params[:user][:email])
155 user = users.first if users.count == 1
159 token = user.tokens.create
160 Notifier.lost_password(user, token).deliver_later
161 flash[:notice] = t "users.lost_password.notice email on way"
162 redirect_to :action => "login"
164 flash.now[:error] = t "users.lost_password.notice email cannot find"
170 @title = t "users.reset_password.title"
173 token = UserToken.find_by(:token => params[:token])
176 self.current_user = token.user
179 current_user.pass_crypt = params[:user][:pass_crypt]
180 current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
181 current_user.status = "active" if current_user.status == "pending"
182 current_user.email_valid = true
186 flash[:notice] = t "users.reset_password.flash changed"
187 successful_login(current_user)
191 flash[:error] = t "users.reset_password.flash token bad"
192 redirect_to :action => "lost_password"
200 @title = t "users.new.title"
201 @referer = params[:referer] || session[:referer]
203 append_content_security_policy_directives(
204 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
208 # The user is logged in already, so don't show them the signup
209 # page, instead send them to the home page
213 redirect_to :controller => "site", :action => "index"
215 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
216 self.current_user = User.new(:email => params[:email],
217 :email_confirmation => params[:email],
218 :display_name => params[:nickname],
219 :auth_provider => params[:auth_provider],
220 :auth_uid => params[:auth_uid])
222 flash.now[:notice] = render_to_string :partial => "auth_association"
226 self.current_user = User.new
231 self.current_user = User.new(user_params)
233 if check_signup_allowed(current_user.email)
234 session[:referer] = params[:referer]
236 current_user.status = "pending"
238 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
239 # We are creating an account with external authentication and
240 # no password was specified so create a random one
241 current_user.pass_crypt = SecureRandom.base64(16)
242 current_user.pass_crypt_confirmation = current_user.pass_crypt
245 if current_user.invalid?
246 # Something is wrong with a new user, so rerender the form
247 render :action => "new"
248 elsif current_user.auth_provider.present?
249 # Verify external authenticator before moving on
250 session[:new_user] = current_user
251 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid)
253 # Save the user record
254 session[:new_user] = current_user
255 redirect_to :action => :terms
261 session[:referer] = params[:referer] if params[:referer]
263 if params[:username].present? && params[:password].present?
264 session[:remember_me] ||= params[:remember_me]
265 password_authentication(params[:username], params[:password])
270 @title = t "users.logout.title"
272 if params[:session] == session.id
274 token = UserToken.find_by(:token => session[:token])
276 session.delete(:token)
278 session.delete(:user)
279 session_expires_automatically
281 redirect_to params[:referer]
283 redirect_to :controller => "site", :action => "index"
290 token = UserToken.find_by(:token => params[:confirm_string])
291 if token&.user&.active?
292 flash[:error] = t("users.confirm.already active")
293 redirect_to :action => "login"
294 elsif !token || token.expired?
295 flash[:error] = t("users.confirm.unknown token")
296 redirect_to :action => "confirm"
299 user.status = "active"
300 user.email_valid = true
301 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
303 referer = token.referer
307 token = UserToken.find_by(:token => session[:token])
308 session.delete(:token)
313 if token.nil? || token.user != user
314 flash[:notice] = t("users.confirm.success")
315 redirect_to :action => :login, :referer => referer
319 session[:user] = user.id
321 redirect_to referer || welcome_path
325 user = User.find_by(:display_name => params[:display_name])
327 redirect_to root_path if user.nil? || user.active?
332 user = User.find_by(:display_name => params[:display_name])
333 token = UserToken.find_by(:token => session[:token])
335 if user.nil? || token.nil? || token.user != user
336 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
338 Notifier.signup_confirm(user, user.tokens.create).deliver_later
339 flash[:notice] = t("users.confirm_resend.success", :email => user.email, :sender => Settings.support_email).html_safe
342 redirect_to :action => "login"
347 token = UserToken.find_by(:token => params[:confirm_string])
348 if token&.user&.new_email?
349 self.current_user = token.user
350 current_user.email = current_user.new_email
351 current_user.new_email = nil
352 current_user.email_valid = true
353 gravatar_enabled = gravatar_enable(current_user)
355 flash[:notice] = if gravatar_enabled
356 t("users.confirm_email.success") + " " + gravatar_status_message(current_user)
358 t("users.confirm_email.success")
361 flash[:errors] = current_user.errors
364 session[:user] = current_user.id
365 redirect_to :action => "account", :display_name => current_user.display_name
367 flash[:error] = t "users.confirm_email.failure"
368 redirect_to :action => "account", :display_name => token.user.display_name
370 flash[:error] = t "users.confirm_email.unknown_token"
376 @user = User.find_by(:display_name => params[:display_name])
379 (@user.visible? || (current_user&.administrator?))
380 @title = @user.display_name
382 render_unknown_user params[:display_name]
387 @new_friend = User.find_by(:display_name => params[:display_name])
391 friendship = Friendship.new
392 friendship.befriender = current_user
393 friendship.befriendee = @new_friend
394 if current_user.is_friends_with?(@new_friend)
395 flash[:warning] = t "users.make_friend.already_a_friend", :name => @new_friend.display_name
396 elsif friendship.save
397 flash[:notice] = t "users.make_friend.success", :name => @new_friend.display_name
398 Notifier.friend_notification(friendship).deliver_later
400 friendship.add_error(t("users.make_friend.failed", :name => @new_friend.display_name))
404 redirect_to params[:referer]
406 redirect_to :action => "show"
410 render_unknown_user params[:display_name]
415 @friend = User.find_by(:display_name => params[:display_name])
419 if current_user.is_friends_with?(@friend)
420 Friendship.where(:befriender => current_user, :befriendee => @friend).delete_all
421 flash[:notice] = t "users.remove_friend.success", :name => @friend.display_name
423 flash[:error] = t "users.remove_friend.not_a_friend", :name => @friend.display_name
427 redirect_to params[:referer]
429 redirect_to :action => "show"
433 render_unknown_user params[:display_name]
438 # sets a user's status
440 @user.status = params[:status]
442 redirect_to user_path(:display_name => params[:display_name])
446 # delete a user, marking them as deleted and removing personal data
449 redirect_to user_path(:display_name => params[:display_name])
453 # display a list of users matching specified criteria
456 ids = params[:user].keys.collect(&:to_i)
458 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
459 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
461 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
463 @params = params.permit(:status, :ip)
466 conditions[:status] = @params[:status] if @params[:status]
467 conditions[:creation_ip] = @params[:ip] if @params[:ip]
469 @user_pages, @users = paginate(:users,
470 :conditions => conditions,
477 # omniauth success callback
479 auth_info = request.env["omniauth.auth"]
481 provider = auth_info[:provider]
482 uid = auth_info[:uid]
483 name = auth_info[:info][:name]
484 email = auth_info[:info][:email]
488 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
489 uid.match(%r{https://me.yahoo.com/(.*)})
490 when "google", "facebook"
491 email_verified = true
493 email_verified = false
496 if settings = session.delete(:new_user_settings)
497 current_user.auth_provider = provider
498 current_user.auth_uid = uid
500 update_user(current_user, settings)
502 session[:user_errors] = current_user.errors.as_json
504 redirect_to :action => "account", :display_name => current_user.display_name
505 elsif session[:new_user]
506 session[:new_user].auth_provider = provider
507 session[:new_user].auth_uid = uid
509 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
511 redirect_to :action => "terms"
513 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
515 if user.nil? && provider == "google"
516 openid_url = auth_info[:extra][:id_info]["openid_id"]
517 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
518 user&.update(:auth_provider => provider, :auth_uid => uid)
524 unconfirmed_login(user)
525 when "active", "confirmed" then
526 successful_login(user, request.env["omniauth.params"]["referer"])
527 when "suspended" then
528 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
530 failed_login t("users.login.auth failure")
533 redirect_to :action => "new", :nickname => name, :email => email,
534 :auth_provider => provider, :auth_uid => uid
540 # omniauth failure callback
542 flash[:error] = t("users.auth_failure." + params[:message])
543 redirect_to params[:origin] || login_url
549 # handle password authentication
550 def password_authentication(username, password)
551 if user = User.authenticate(:username => username, :password => password)
552 successful_login(user)
553 elsif user = User.authenticate(:username => username, :password => password, :pending => true)
554 unconfirmed_login(user)
555 elsif User.authenticate(:username => username, :password => password, :suspended => true)
556 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe, username
558 failed_login t("users.login.auth failure"), username
563 # return the URL to use for authentication
564 def auth_url(provider, uid, referer = nil)
565 params = { :provider => provider }
567 params[:openid_url] = openid_expand_url(uid) if provider == "openid"
570 params[:origin] = request.path
572 params[:origin] = request.path + "?referer=" + CGI.escape(referer)
573 params[:referer] = referer
580 # special case some common OpenID providers by applying heuristics to
581 # try and come up with the correct URL based on what the user entered
582 def openid_expand_url(openid_url)
585 elsif openid_url.match(%r{(.*)gmail.com(/?)$}) || openid_url.match(%r{(.*)googlemail.com(/?)$})
586 # Special case gmail.com as it is potentially a popular OpenID
587 # provider and, unlike yahoo.com, where it works automatically, Google
588 # have hidden their OpenID endpoint somewhere obscure this making it
589 # somewhat less user friendly.
590 "https://www.google.com/accounts/o8/id"
597 # process a successful login
598 def successful_login(user, referer = nil)
599 session[:user] = user.id
600 session_expires_after 28.days if session[:remember_me]
602 target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
604 # The user is logged in, so decide where to send them:
606 # - If they haven't seen the contributor terms, send them there.
607 # - If they have a block on them, show them that.
608 # - If they were referred to the login, send them back there.
609 # - Otherwise, send them to the home page.
611 redirect_to :action => :terms, :referer => target
612 elsif user.blocked_on_view
613 redirect_to user.blocked_on_view, :referer => target
618 session.delete(:remember_me)
619 session.delete(:referer)
623 # process a failed login
624 def failed_login(message, username = nil)
625 flash[:error] = message
627 redirect_to :action => "login", :referer => session[:referer],
628 :username => username, :remember_me => session[:remember_me]
630 session.delete(:remember_me)
631 session.delete(:referer)
636 def unconfirmed_login(user)
637 session[:token] = user.tokens.create.token
639 redirect_to :action => "confirm", :display_name => user.display_name
641 session.delete(:remember_me)
642 session.delete(:referer)
646 # update a user's details
647 def update_user(user, params)
648 user.display_name = params[:user][:display_name]
649 user.new_email = params[:user][:new_email]
651 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
652 user.pass_crypt = params[:user][:pass_crypt]
653 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
656 if params[:user][:description] != user.description
657 user.description = params[:user][:description]
658 user.description_format = "markdown"
661 user.languages = params[:user][:languages].split(",")
663 case params[:image_action]
665 user.image = params[:user][:image]
666 user.image_use_gravatar = false
669 user.image_use_gravatar = false
672 user.image_use_gravatar = true
675 user.home_lat = params[:user][:home_lat]
676 user.home_lon = params[:user][:home_lon]
678 user.preferred_editor = if params[:user][:preferred_editor] == "default"
681 params[:user][:preferred_editor]
684 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
685 user.auth_provider = nil
692 if user.new_email.blank? || user.new_email == user.email
693 flash.now[:notice] = t "users.account.flash update success"
695 user.email = user.new_email
698 flash.now[:notice] = t "users.account.flash update success confirm needed"
701 Notifier.email_confirm(user, user.tokens.create).deliver_later
703 # Ignore errors sending email
706 current_user.errors.add(:new_email, current_user.errors[:email])
707 current_user.errors.add(:email, [])
716 # require that the user in the URL is the logged in user
718 head :forbidden if params[:display_name] != current_user.display_name
722 # ensure that there is a "user" instance variable
723 def lookup_user_by_name
724 @user = User.find_by(:display_name => params[:display_name])
725 rescue ActiveRecord::RecordNotFound
726 redirect_to :action => "view", :display_name => params[:display_name] unless @user
731 def disable_terms_redirect
732 # this is necessary otherwise going to the user terms page, when
733 # having not agreed already would cause an infinite redirect loop.
734 # it's .now so that this doesn't propagate to other pages.
735 flash.now[:skip_terms] = true
739 # return permitted user parameters
741 params.require(:user).permit(:email, :email_confirmation, :display_name,
742 :auth_provider, :auth_uid,
743 :pass_crypt, :pass_crypt_confirmation)
748 def check_signup_allowed(email = nil)
749 domain = if email.nil?
752 email.split("@").last
755 mx_servers = if domain.nil?
758 domain_mx_servers(domain)
761 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
762 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
764 render :action => "blocked"
771 # get list of MX servers for a domains
772 def domain_mx_servers(domain)
773 Resolv::DNS.open do |dns|
774 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)
779 # check if this user has a gravatar and set the user pref is true
780 def gravatar_enable(user)
781 # code from example https://en.gravatar.com/site/implement/images/ruby/
782 return false if user.image.present?
784 hash = Digest::MD5.hexdigest(user.email.downcase)
785 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
786 response = OSM.http_client.get(URI.parse(url))
787 oldsetting = user.image_use_gravatar
788 user.image_use_gravatar = response.success?
789 oldsetting != user.image_use_gravatar
793 # display a message about th current status of the gravatar setting
794 def gravatar_status_message(user)
795 if user.image_use_gravatar
796 t "users.account.gravatar.enabled"
798 t "users.account.gravatar.disabled"