1 class UsersController < ApplicationController
6 skip_before_action :verify_authenticity_token, :only => [:auth_success]
7 before_action :disable_terms_redirect, :only => [:terms, :save]
8 before_action :authorize_web
9 before_action :set_locale
10 before_action :check_database_readable
14 before_action :require_self, :only => [:account]
15 before_action :check_database_writable, :only => [:new, :account, :confirm, :confirm_email, :lost_password, :reset_password, :go_public]
16 before_action :require_cookies, :only => [:new, :confirm]
17 before_action :lookup_user_by_name, :only => [:set_status, :destroy]
18 before_action :allow_thirdparty_images, :only => [:show, :account]
21 @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || Settings.default_legale
22 @text = OSM.legal_text_for_country(@legale)
25 render :partial => "terms"
27 @title = t "users.terms.title"
29 if current_user&.terms_agreed?
30 # Already agreed to terms, so just show settings
31 redirect_to :action => :account, :display_name => current_user.display_name
32 elsif current_user.nil? && session[:new_user].nil?
33 redirect_to :action => :login, :referer => request.fullpath
39 @title = t "users.new.title"
41 if params[:decline] || !(params[:read_tou] && params[:read_ct])
43 current_user.terms_seen = true
45 flash[:notice] = t("users.new.terms declined", :url => t("users.new.terms declined url")).html_safe if current_user.save
48 redirect_to safe_referer(params[:referer])
50 redirect_to :action => :account, :display_name => current_user.display_name
52 elsif params[:decline]
53 redirect_to t("users.terms.declined")
55 redirect_to :action => :terms
58 unless current_user.terms_agreed?
59 current_user.consider_pd = params[:user][:consider_pd]
60 current_user.tou_agreed = Time.now.getutc
61 current_user.terms_agreed = Time.now.getutc
62 current_user.terms_seen = true
64 flash[:notice] = t "users.new.terms accepted" if current_user.save
68 redirect_to safe_referer(params[:referer])
70 redirect_to :action => :account, :display_name => current_user.display_name
73 self.current_user = session.delete(:new_user)
75 if check_signup_allowed(current_user.email)
76 current_user.data_public = true
77 current_user.description = "" if current_user.description.nil?
78 current_user.creation_ip = request.remote_ip
79 current_user.languages = http_accept_language.user_preferred_languages
80 current_user.terms_agreed = Time.now.getutc
81 current_user.tou_agreed = Time.now.getutc
82 current_user.terms_seen = true
84 if current_user.auth_uid.blank?
85 current_user.auth_provider = nil
86 current_user.auth_uid = nil
90 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
92 referer = welcome_path
95 uri = URI(session[:referer])
96 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
97 editor = Rack::Utils.parse_query(uri.query).slice("editor")
98 referer = welcome_path({ "zoom" => m[1],
100 "lon" => m[3] }.merge(editor))
106 if current_user.status == "active"
107 session[:referer] = referer
108 successful_login(current_user)
110 session[:token] = current_user.tokens.create.token
111 UserMailer.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_later
112 redirect_to :action => "confirm", :display_name => current_user.display_name
115 render :action => "new", :referer => params[:referer]
122 @tokens = current_user.oauth_tokens.authorized
124 append_content_security_policy_directives(
125 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
129 if params[:user][:auth_provider].blank? ||
130 (params[:user][:auth_provider] == current_user.auth_provider &&
131 params[:user][:auth_uid] == current_user.auth_uid)
132 update_user(current_user, params)
133 redirect_to user_account_url(current_user) if current_user.errors.count.zero?
135 session[:new_user_settings] = params
136 redirect_to auth_url(params[:user][:auth_provider], params[:user][:auth_uid]), :status => :temporary_redirect
138 elsif errors = session.delete(:user_errors)
139 errors.each do |attribute, error|
140 current_user.errors.add(attribute, error)
143 @title = t "users.account.title"
147 current_user.data_public = true
149 flash[:notice] = t "users.go_public.flash success"
150 redirect_to :action => "account", :display_name => current_user.display_name
154 @title = t "users.lost_password.title"
157 user = User.visible.find_by(:email => params[:email])
160 users = User.visible.where("LOWER(email) = LOWER(?)", params[:email])
162 user = users.first if users.count == 1
166 token = user.tokens.create
167 UserMailer.lost_password(user, token).deliver_later
168 flash[:notice] = t "users.lost_password.notice email on way"
169 redirect_to login_path
171 flash.now[:error] = t "users.lost_password.notice email cannot find"
177 @title = t "users.reset_password.title"
180 token = UserToken.find_by(:token => params[:token])
183 self.current_user = token.user
186 current_user.pass_crypt = params[:user][:pass_crypt]
187 current_user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
188 current_user.status = "active" if current_user.status == "pending"
189 current_user.email_valid = true
193 session[:fingerprint] = current_user.fingerprint
194 flash[:notice] = t "users.reset_password.flash changed"
195 successful_login(current_user)
199 flash[:error] = t "users.reset_password.flash token bad"
200 redirect_to :action => "lost_password"
208 @title = t "users.new.title"
209 @referer = if params[:referer]
210 safe_referer(params[:referer])
215 append_content_security_policy_directives(
216 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
220 # The user is logged in already, so don't show them the signup
221 # page, instead send them to the home page
225 redirect_to :controller => "site", :action => "index"
227 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
228 self.current_user = User.new(:email => params[:email],
229 :email_confirmation => params[:email],
230 :display_name => params[:nickname],
231 :auth_provider => params[:auth_provider],
232 :auth_uid => params[:auth_uid])
234 flash.now[:notice] = render_to_string :partial => "auth_association"
238 self.current_user = User.new
243 self.current_user = User.new(user_params)
245 if check_signup_allowed(current_user.email)
246 session[:referer] = safe_referer(params[:referer]) if params[:referer]
248 Rails.logger.info "create: #{session[:referer]}"
250 current_user.status = "pending"
252 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
253 # We are creating an account with external authentication and
254 # no password was specified so create a random one
255 current_user.pass_crypt = SecureRandom.base64(16)
256 current_user.pass_crypt_confirmation = current_user.pass_crypt
259 if current_user.invalid?
260 # Something is wrong with a new user, so rerender the form
261 render :action => "new"
262 elsif current_user.auth_provider.present?
263 # Verify external authenticator before moving on
264 session[:new_user] = current_user
265 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid), :status => :temporary_redirect
267 # Save the user record
268 session[:new_user] = current_user
269 redirect_to :action => :terms
276 token = UserToken.find_by(:token => params[:confirm_string])
277 if token&.user&.active?
278 flash[:error] = t("users.confirm.already active")
279 redirect_to login_path
280 elsif !token || token.expired?
281 flash[:error] = t("users.confirm.unknown token")
282 redirect_to :action => "confirm"
285 user.status = "active"
286 user.email_valid = true
287 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
289 referer = safe_referer(token.referer) if token.referer
293 token = UserToken.find_by(:token => session[:token])
294 session.delete(:token)
299 if token.nil? || token.user != user
300 flash[:notice] = t("users.confirm.success")
301 redirect_to login_path(:referer => referer)
305 session[:user] = user.id
306 session[:fingerprint] = user.fingerprint
308 redirect_to referer || welcome_path
312 user = User.find_by(:display_name => params[:display_name])
314 redirect_to root_path if user.nil? || user.active?
319 user = User.find_by(:display_name => params[:display_name])
320 token = UserToken.find_by(:token => session[:token])
322 if user.nil? || token.nil? || token.user != user
323 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
325 UserMailer.signup_confirm(user, user.tokens.create).deliver_later
326 flash[:notice] = t "users.confirm_resend.success_html", :email => user.email, :sender => Settings.support_email
329 redirect_to login_path
334 token = UserToken.find_by(:token => params[:confirm_string])
335 if token&.user&.new_email?
336 self.current_user = token.user
337 current_user.email = current_user.new_email
338 current_user.new_email = nil
339 current_user.email_valid = true
340 gravatar_enabled = gravatar_enable(current_user)
342 flash[:notice] = if gravatar_enabled
343 "#{t('users.confirm_email.success')} #{gravatar_status_message(current_user)}"
345 t("users.confirm_email.success")
348 flash[:errors] = current_user.errors
350 current_user.tokens.delete_all
351 session[:user] = current_user.id
352 session[:fingerprint] = current_user.fingerprint
353 redirect_to :action => "account", :display_name => current_user.display_name
355 flash[:error] = t "users.confirm_email.failure"
356 redirect_to :action => "account", :display_name => token.user.display_name
358 flash[:error] = t "users.confirm_email.unknown_token"
364 @user = User.find_by(:display_name => params[:display_name])
367 (@user.visible? || current_user&.administrator?)
368 @title = @user.display_name
370 render_unknown_user params[:display_name]
375 # sets a user's status
377 @user.status = params[:status]
379 redirect_to user_path(:display_name => params[:display_name])
383 # delete a user, marking them as deleted and removing personal data
386 redirect_to user_path(:display_name => params[:display_name])
390 # display a list of users matching specified criteria
393 ids = params[:user].keys.collect(&:to_i)
395 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
396 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
398 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
400 @params = params.permit(:status, :ip)
403 conditions[:status] = @params[:status] if @params[:status]
404 conditions[:creation_ip] = @params[:ip] if @params[:ip]
406 @user_pages, @users = paginate(:users,
407 :conditions => conditions,
414 # omniauth success callback
416 auth_info = request.env["omniauth.auth"]
418 provider = auth_info[:provider]
419 uid = auth_info[:uid]
420 name = auth_info[:info][:name]
421 email = auth_info[:info][:email]
425 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
426 uid.match(%r{https://me.yahoo.com/(.*)})
427 when "google", "facebook"
428 email_verified = true
430 email_verified = false
433 if settings = session.delete(:new_user_settings)
434 current_user.auth_provider = provider
435 current_user.auth_uid = uid
437 update_user(current_user, settings)
441 session[:user_errors] = current_user.errors.as_json
443 redirect_to :action => "account", :display_name => current_user.display_name
444 elsif session[:new_user]
445 session[:new_user].auth_provider = provider
446 session[:new_user].auth_uid = uid
448 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
450 redirect_to :action => "terms"
452 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
454 if user.nil? && provider == "google"
455 openid_url = auth_info[:extra][:id_info]["openid_id"]
456 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
457 user&.update(:auth_provider => provider, :auth_uid => uid)
463 unconfirmed_login(user)
464 when "active", "confirmed"
465 successful_login(user, request.env["omniauth.params"]["referer"])
467 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
469 failed_login t("users.login.auth failure")
472 redirect_to :action => "new", :nickname => name, :email => email,
473 :auth_provider => provider, :auth_uid => uid
479 # omniauth failure callback
481 flash[:error] = t("users.auth_failure.#{params[:message]}")
482 redirect_to params[:origin] || login_url
489 def unconfirmed_login(user)
490 session[:token] = user.tokens.create.token
492 redirect_to :action => "confirm", :display_name => user.display_name
494 session.delete(:remember_me)
495 session.delete(:referer)
499 # update a user's details
500 def update_user(user, params)
501 user.display_name = params[:user][:display_name]
502 user.new_email = params[:user][:new_email]
504 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
505 user.pass_crypt = params[:user][:pass_crypt]
506 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
509 if params[:user][:description] != user.description
510 user.description = params[:user][:description]
511 user.description_format = "markdown"
514 user.languages = params[:user][:languages].split(",")
516 case params[:avatar_action]
518 user.avatar.attach(params[:user][:avatar])
519 user.image_use_gravatar = false
521 user.avatar.purge_later
522 user.image_use_gravatar = false
524 user.avatar.purge_later
525 user.image_use_gravatar = true
528 user.home_lat = params[:user][:home_lat]
529 user.home_lon = params[:user][:home_lon]
531 user.preferred_editor = if params[:user][:preferred_editor] == "default"
534 params[:user][:preferred_editor]
537 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
538 user.auth_provider = nil
543 session[:fingerprint] = user.fingerprint
545 set_locale(:reset => true)
547 if user.new_email.blank? || user.new_email == user.email
548 flash[:notice] = t "users.account.flash update success"
550 user.email = user.new_email
553 flash[:notice] = t "users.account.flash update success confirm needed"
556 UserMailer.email_confirm(user, user.tokens.create).deliver_later
558 # Ignore errors sending email
561 current_user.errors.add(:new_email, current_user.errors[:email])
562 current_user.errors.add(:email, [])
571 # require that the user in the URL is the logged in user
573 head :forbidden if params[:display_name] != current_user.display_name
577 # ensure that there is a "user" instance variable
578 def lookup_user_by_name
579 @user = User.find_by(:display_name => params[:display_name])
580 rescue ActiveRecord::RecordNotFound
581 redirect_to :action => "view", :display_name => params[:display_name] unless @user
585 # return permitted user parameters
587 params.require(:user).permit(:email, :email_confirmation, :display_name,
588 :auth_provider, :auth_uid,
589 :pass_crypt, :pass_crypt_confirmation)
594 def check_signup_allowed(email = nil)
595 domain = if email.nil?
598 email.split("@").last
601 mx_servers = if domain.nil?
604 domain_mx_servers(domain)
607 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
608 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
610 render :action => "blocked"
617 # get list of MX servers for a domains
618 def domain_mx_servers(domain)
619 Resolv::DNS.open do |dns|
620 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)
625 # check if this user has a gravatar and set the user pref is true
626 def gravatar_enable(user)
627 # code from example https://en.gravatar.com/site/implement/images/ruby/
628 return false if user.avatar.attached?
631 hash = Digest::MD5.hexdigest(user.email.downcase)
632 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
633 response = OSM.http_client.get(URI.parse(url))
634 available = response.success?
639 oldsetting = user.image_use_gravatar
640 user.image_use_gravatar = available
641 oldsetting != user.image_use_gravatar
645 # display a message about th current status of the gravatar setting
646 def gravatar_status_message(user)
647 if user.image_use_gravatar
648 t "users.account.gravatar.enabled"
650 t "users.account.gravatar.disabled"