1 class UsersController < ApplicationController
7 skip_before_action :verify_authenticity_token, :only => [:auth_success]
8 before_action :disable_terms_redirect, :only => [:terms, :save]
9 before_action :authorize_web
10 before_action :set_locale
11 before_action :check_database_readable
15 before_action :check_database_writable, :only => [:new, :go_public]
16 before_action :require_cookies, :only => [:new]
17 before_action :lookup_user_by_name, :only => [:set_status, :destroy]
18 before_action :allow_thirdparty_images, :only => [:show]
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 edit_account_path
32 elsif current_user.nil? && session[:new_user].nil?
33 redirect_to login_path(: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] = { :partial => "users/terms_declined_flash" } if current_user.save
47 referer = safe_referer(params[:referer]) if params[:referer]
49 redirect_to referer || edit_account_path
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.utc
59 current_user.terms_agreed = Time.now.utc
60 current_user.terms_seen = true
62 flash[:notice] = t "users.new.terms accepted" if current_user.save
65 referer = safe_referer(params[:referer]) if params[:referer]
67 redirect_to referer || edit_account_path
69 self.current_user = session.delete(:new_user)
71 if check_signup_allowed(current_user.email)
72 current_user.data_public = true
73 current_user.description = "" if current_user.description.nil?
74 current_user.creation_ip = request.remote_ip
75 current_user.languages = http_accept_language.user_preferred_languages
76 current_user.terms_agreed = Time.now.utc
77 current_user.tou_agreed = Time.now.utc
78 current_user.terms_seen = true
80 if current_user.auth_uid.blank?
81 current_user.auth_provider = nil
82 current_user.auth_uid = nil
86 flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
88 referer = welcome_path
91 uri = URI(session[:referer])
92 %r{map=(.*)/(.*)/(.*)}.match(uri.fragment) do |m|
93 editor = Rack::Utils.parse_query(uri.query).slice("editor")
94 referer = welcome_path({ "zoom" => m[1],
96 "lon" => m[3] }.merge(editor))
102 if current_user.status == "active"
103 session[:referer] = referer
104 successful_login(current_user)
106 session[:token] = current_user.tokens.create.token
107 UserMailer.signup_confirm(current_user, current_user.tokens.create(:referer => referer)).deliver_later
108 redirect_to :controller => :confirmations, :action => :confirm, :display_name => current_user.display_name
111 render :action => "new", :referer => params[:referer]
118 current_user.data_public = true
120 flash[:notice] = t "users.go_public.flash success"
121 redirect_to edit_account_path
125 @title = t "users.new.title"
126 @referer = if params[:referer]
127 safe_referer(params[:referer])
132 append_content_security_policy_directives(
133 :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
137 # The user is logged in already, so don't show them the signup
138 # page, instead send them to the home page
142 redirect_to :controller => "site", :action => "index"
144 elsif params.key?(:auth_provider) && params.key?(:auth_uid)
145 self.current_user = User.new(:email => params[:email],
146 :email_confirmation => params[:email],
147 :display_name => params[:nickname],
148 :auth_provider => params[:auth_provider],
149 :auth_uid => params[:auth_uid])
151 flash.now[:notice] = render_to_string :partial => "auth_association"
155 self.current_user = User.new
160 self.current_user = User.new(user_params)
162 if check_signup_allowed(current_user.email)
163 session[:referer] = safe_referer(params[:referer]) if params[:referer]
165 Rails.logger.info "create: #{session[:referer]}"
167 if current_user.auth_provider.present? && current_user.pass_crypt.empty?
168 # We are creating an account with external authentication and
169 # no password was specified so create a random one
170 current_user.pass_crypt = SecureRandom.base64(16)
171 current_user.pass_crypt_confirmation = current_user.pass_crypt
174 if current_user.invalid?
175 # Something is wrong with a new user, so rerender the form
176 render :action => "new"
177 elsif current_user.auth_provider.present?
178 # Verify external authenticator before moving on
179 session[:new_user] = current_user
180 redirect_to auth_url(current_user.auth_provider, current_user.auth_uid), :status => :temporary_redirect
182 # Save the user record
183 session[:new_user] = current_user
184 redirect_to :action => :terms
190 @user = User.find_by(:display_name => params[:display_name])
193 (@user.visible? || current_user&.administrator?)
194 @title = @user.display_name
196 render_unknown_user params[:display_name]
201 # sets a user's status
203 @user.activate! if params[:event] == "activate"
204 @user.confirm! if params[:event] == "confirm"
205 @user.unconfirm! if params[:event] == "unconfirm"
206 @user.hide! if params[:event] == "hide"
207 @user.unhide! if params[:event] == "unhide"
208 @user.unsuspend! if params[:event] == "unsuspend"
209 redirect_to user_path(:display_name => params[:display_name])
213 # destroy a user, marking them as deleted and removing personal data
216 redirect_to user_path(:display_name => params[:display_name])
220 # display a list of users matching specified criteria
223 ids = params[:user].keys.collect(&:to_i)
225 User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
226 User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
228 redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
230 @params = params.permit(:status, :ip)
233 conditions[:status] = @params[:status] if @params[:status]
234 conditions[:creation_ip] = @params[:ip] if @params[:ip]
236 @user_pages, @users = paginate(:users,
237 :conditions => conditions,
244 # omniauth success callback
246 auth_info = request.env["omniauth.auth"]
248 provider = auth_info[:provider]
249 uid = auth_info[:uid]
250 name = auth_info[:info][:name]
251 email = auth_info[:info][:email]
253 email_verified = case provider
255 uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
256 uid.match(%r{https://me.yahoo.com/(.*)})
257 when "google", "facebook"
263 if settings = session.delete(:new_user_settings)
264 current_user.auth_provider = provider
265 current_user.auth_uid = uid
267 update_user(current_user, settings)
271 session[:user_errors] = current_user.errors.as_json
273 redirect_to edit_account_path
274 elsif session[:new_user]
275 session[:new_user].auth_provider = provider
276 session[:new_user].auth_uid = uid
278 session[:new_user].status = "active" if email_verified && email == session[:new_user].email
280 redirect_to :action => "terms"
282 user = User.find_by(:auth_provider => provider, :auth_uid => uid)
284 if user.nil? && provider == "google"
285 openid_url = auth_info[:extra][:id_info]["openid_id"]
286 user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
287 user&.update(:auth_provider => provider, :auth_uid => uid)
293 unconfirmed_login(user)
294 when "active", "confirmed"
295 successful_login(user, request.env["omniauth.params"]["referer"])
297 failed_login t("sessions.new.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
299 failed_login t("sessions.new.auth failure")
302 redirect_to :action => "new", :nickname => name, :email => email,
303 :auth_provider => provider, :auth_uid => uid
309 # omniauth failure callback
311 flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t("users.auth_failure.unknown_error"))
313 origin = safe_referer(params[:origin]) if params[:origin]
315 redirect_to origin || login_url
321 # ensure that there is a "user" instance variable
322 def lookup_user_by_name
323 @user = User.find_by(:display_name => params[:display_name])
324 rescue ActiveRecord::RecordNotFound
325 redirect_to :action => "view", :display_name => params[:display_name] unless @user
329 # return permitted user parameters
331 params.require(:user).permit(:email, :email_confirmation, :display_name,
332 :auth_provider, :auth_uid,
333 :pass_crypt, :pass_crypt_confirmation)
338 def check_signup_allowed(email = nil)
339 domain = if email.nil?
342 email.split("@").last
345 mx_servers = if domain.nil?
348 domain_mx_servers(domain)
351 if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
352 logger.info "Blocked signup from #{request.remote_ip} for #{email}"
354 render :action => "blocked"
361 # get list of MX servers for a domains
362 def domain_mx_servers(domain)
363 Resolv::DNS.open do |dns|
364 dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)