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"
283 elsif !token.user.visible?
284 render_unknown_user token.user.display_name
287 user.status = "active"
288 user.email_valid = true
289 flash[:notice] = gravatar_status_message(user) if gravatar_enable(user)
291 referer = safe_referer(token.referer) if token.referer
295 token = UserToken.find_by(:token => session[:token])
296 session.delete(:token)
301 if token.nil? || token.user != user
302 flash[:notice] = t("users.confirm.success")
303 redirect_to login_path(:referer => referer)
307 session[:user] = user.id
308 session[:fingerprint] = user.fingerprint
310 redirect_to referer || welcome_path
314 user = User.visible.find_by(:display_name => params[:display_name])
316 redirect_to root_path if user.nil? || user.active?
321 user = User.visible.find_by(:display_name => params[:display_name])
322 token = UserToken.find_by(:token => session[:token])
324 if user.nil? || token.nil? || token.user != user
325 flash[:error] = t "users.confirm_resend.failure", :name => params[:display_name]
327 UserMailer.signup_confirm(user, user.tokens.create).deliver_later
328 flash[:notice] = t "users.confirm_resend.success_html", :email => user.email, :sender => Settings.support_email
331 redirect_to login_path
336 token = UserToken.find_by(:token => params[:confirm_string])
337 if token&.user&.new_email?
338 self.current_user = token.user
339 current_user.email = current_user.new_email
340 current_user.new_email = nil
341 current_user.email_valid = true
342 gravatar_enabled = gravatar_enable(current_user)
344 flash[:notice] = if gravatar_enabled
345 "#{t('users.confirm_email.success')} #{gravatar_status_message(current_user)}"
347 t("users.confirm_email.success")
350 flash[:errors] = current_user.errors
352 current_user.tokens.delete_all
353 session[:user] = current_user.id
354 session[:fingerprint] = current_user.fingerprint
355 redirect_to :action => "account", :display_name => current_user.display_name
357 flash[:error] = t "users.confirm_email.failure"
358 redirect_to :action => "account", :display_name => token.user.display_name
360 flash[:error] = t "users.confirm_email.unknown_token"
366 @user = User.find_by(:display_name => params[:display_name])
369 (@user.visible? || current_user&.administrator?)
370 @title = @user.display_name
372 render_unknown_user params[:display_name]
377 # sets a user's status
379 @user.status = params[:status]
381 redirect_to user_path(:display_name => params[:display_name])
385 # delete a user, marking them as deleted and removing personal data
388 redirect_to user_path(:display_name => params[:display_name])
392 # display a list of users matching specified criteria
395 ids = params[:user].keys.collect(&:to_i)
397 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
398 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
400 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
402 @params = params.permit(:status, :ip)
405 conditions[:status] = @params[:status] if @params[:status]
406 conditions[:creation_ip] = @params[:ip] if @params[:ip]
408 @user_pages, @users = paginate(:users,
409 :conditions => conditions,
416 # omniauth success callback
418 auth_info = request.env["omniauth.auth"]
420 provider = auth_info[:provider]
421 uid = auth_info[:uid]
422 name = auth_info[:info][:name]
423 email = auth_info[:info][:email]
427 email_verified = uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
428 uid.match(%r{https://me.yahoo.com/(.*)})
429 when "google", "facebook"
430 email_verified = true
432 email_verified = false
435 if settings = session.delete(:new_user_settings)
436 current_user.auth_provider = provider
437 current_user.auth_uid = uid
439 update_user(current_user, settings)
443 session[:user_errors] = current_user.errors.as_json
445 redirect_to :action => "account", :display_name => current_user.display_name
446 elsif session[:new_user]
447 session[:new_user].auth_provider = provider
448 session[:new_user].auth_uid = uid
450 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
452 redirect_to :action => "terms"
454 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
456 if user.nil? && provider == "google"
457 openid_url = auth_info[:extra][:id_info]["openid_id"]
458 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
459 user&.update(:auth_provider => provider, :auth_uid => uid)
465 unconfirmed_login(user)
466 when "active", "confirmed"
467 successful_login(user, request.env["omniauth.params"]["referer"])
469 failed_login t("users.login.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
471 failed_login t("users.login.auth failure")
474 redirect_to :action => "new", :nickname => name, :email => email,
475 :auth_provider => provider, :auth_uid => uid
481 # omniauth failure callback
483 flash[:error] = t("users.auth_failure.#{params[:message]}")
484 redirect_to params[:origin] || login_url
491 def unconfirmed_login(user)
492 session[:token] = user.tokens.create.token
494 redirect_to :action => "confirm", :display_name => user.display_name
496 session.delete(:remember_me)
497 session.delete(:referer)
501 # update a user's details
502 def update_user(user, params)
503 user.display_name = params[:user][:display_name]
504 user.new_email = params[:user][:new_email]
506 unless params[:user][:pass_crypt].empty? && params[:user][:pass_crypt_confirmation].empty?
507 user.pass_crypt = params[:user][:pass_crypt]
508 user.pass_crypt_confirmation = params[:user][:pass_crypt_confirmation]
511 if params[:user][:description] != user.description
512 user.description = params[:user][:description]
513 user.description_format = "markdown"
516 user.languages = params[:user][:languages].split(",")
518 case params[:avatar_action]
520 user.avatar.attach(params[:user][:avatar])
521 user.image_use_gravatar = false
523 user.avatar.purge_later
524 user.image_use_gravatar = false
526 user.avatar.purge_later
527 user.image_use_gravatar = true
530 user.home_lat = params[:user][:home_lat]
531 user.home_lon = params[:user][:home_lon]
533 user.preferred_editor = if params[:user][:preferred_editor] == "default"
536 params[:user][:preferred_editor]
539 if params[:user][:auth_provider].nil? || params[:user][:auth_provider].blank?
540 user.auth_provider = nil
545 session[:fingerprint] = user.fingerprint
547 set_locale(:reset => true)
549 if user.new_email.blank? || user.new_email == user.email
550 flash[:notice] = t "users.account.flash update success"
552 user.email = user.new_email
555 flash[:notice] = t "users.account.flash update success confirm needed"
558 UserMailer.email_confirm(user, user.tokens.create).deliver_later
560 # Ignore errors sending email
563 current_user.errors.add(:new_email, current_user.errors[:email])
564 current_user.errors.add(:email, [])
573 # require that the user in the URL is the logged in user
575 head :forbidden if params[:display_name] != current_user.display_name
579 # ensure that there is a "user" instance variable
580 def lookup_user_by_name
581 @user = User.find_by(:display_name => params[:display_name])
582 rescue ActiveRecord::RecordNotFound
583 redirect_to :action => "view", :display_name => params[:display_name] unless @user
587 # return permitted user parameters
589 params.require(:user).permit(:email, :email_confirmation, :display_name,
590 :auth_provider, :auth_uid,
591 :pass_crypt, :pass_crypt_confirmation)
596 def check_signup_allowed(email = nil)
597 domain = if email.nil?
600 email.split("@").last
603 mx_servers = if domain.nil?
606 domain_mx_servers(domain)
609 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
610 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
612 render :action => "blocked"
619 # get list of MX servers for a domains
620 def domain_mx_servers(domain)
621 Resolv::DNS.open do |dns|
622 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)
627 # check if this user has a gravatar and set the user pref is true
628 def gravatar_enable(user)
629 # code from example https://en.gravatar.com/site/implement/images/ruby/
630 return false if user.avatar.attached?
633 hash = Digest::MD5.hexdigest(user.email.downcase)
634 url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
635 response = OSM.http_client.get(URI.parse(url))
636 available = response.success?
641 oldsetting = user.image_use_gravatar
642 user.image_use_gravatar = available
643 oldsetting != user.image_use_gravatar
647 # display a message about th current status of the gravatar setting
648 def gravatar_status_message(user)
649 if user.image_use_gravatar
650 t "users.account.gravatar.enabled"
652 t "users.account.gravatar.disabled"