]> git.openstreetmap.org Git - rails.git/blob - app/controllers/users_controller.rb
Update bundle
[rails.git] / app / controllers / users_controller.rb
1 class UsersController < ApplicationController
2   include SessionMethods
3   include UserMethods
4
5   layout "site"
6
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
12
13   authorize_resource
14
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]
19
20   def terms
21     @legale = params[:legale] || OSM.ip_to_country(request.remote_ip) || Settings.default_legale
22     @text = OSM.legal_text_for_country(@legale)
23
24     if request.xhr?
25       render :partial => "terms"
26     else
27       @title = t "users.terms.title"
28
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)
34       end
35     end
36   end
37
38   def save
39     @title = t "users.new.title"
40
41     if params[:decline] || !(params[:read_tou] && params[:read_ct])
42       if current_user
43         current_user.terms_seen = true
44
45         flash[:notice] = { :partial => "users/terms_declined_flash" } if current_user.save
46
47         referer = safe_referer(params[:referer]) if params[:referer]
48
49         redirect_to referer || edit_account_path
50       elsif params[:decline]
51         redirect_to t("users.terms.declined")
52       else
53         redirect_to :action => :terms
54       end
55     elsif current_user
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
61
62         flash[:notice] = t "users.new.terms accepted" if current_user.save
63       end
64
65       referer = safe_referer(params[:referer]) if params[:referer]
66
67       redirect_to referer || edit_account_path
68     else
69       self.current_user = session.delete(:new_user)
70
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.getutc
77         current_user.tou_agreed = Time.now.getutc
78         current_user.terms_seen = true
79
80         if current_user.auth_uid.blank?
81           current_user.auth_provider = nil
82           current_user.auth_uid = nil
83         end
84
85         if current_user.save
86           flash[:piwik_goal] = PIWIK["goals"]["signup"] if defined?(PIWIK)
87
88           referer = welcome_path
89
90           begin
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],
95                                        "lat" => m[2],
96                                        "lon" => m[3] }.merge(editor))
97             end
98           rescue StandardError
99             # Use default
100           end
101
102           if current_user.status == "active"
103             session[:referer] = referer
104             successful_login(current_user)
105           else
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
109           end
110         else
111           render :action => "new", :referer => params[:referer]
112         end
113       end
114     end
115   end
116
117   def go_public
118     current_user.data_public = true
119     current_user.save
120     flash[:notice] = t "users.go_public.flash success"
121     redirect_to edit_account_path
122   end
123
124   def new
125     @title = t "users.new.title"
126     @referer = if params[:referer]
127                  safe_referer(params[:referer])
128                else
129                  session[:referer]
130                end
131
132     append_content_security_policy_directives(
133       :form_action => %w[accounts.google.com *.facebook.com login.live.com github.com meta.wikimedia.org]
134     )
135
136     if current_user
137       # The user is logged in already, so don't show them the signup
138       # page, instead send them to the home page
139       if @referer
140         redirect_to @referer
141       else
142         redirect_to :controller => "site", :action => "index"
143       end
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])
150
151       flash.now[:notice] = render_to_string :partial => "auth_association"
152     else
153       check_signup_allowed
154
155       self.current_user = User.new
156     end
157   end
158
159   def create
160     self.current_user = User.new(user_params)
161
162     if check_signup_allowed(current_user.email)
163       session[:referer] = safe_referer(params[:referer]) if params[:referer]
164
165       Rails.logger.info "create: #{session[:referer]}"
166
167       current_user.status = "pending"
168
169       if current_user.auth_provider.present? && current_user.pass_crypt.empty?
170         # We are creating an account with external authentication and
171         # no password was specified so create a random one
172         current_user.pass_crypt = SecureRandom.base64(16)
173         current_user.pass_crypt_confirmation = current_user.pass_crypt
174       end
175
176       if current_user.invalid?
177         # Something is wrong with a new user, so rerender the form
178         render :action => "new"
179       elsif current_user.auth_provider.present?
180         # Verify external authenticator before moving on
181         session[:new_user] = current_user
182         redirect_to auth_url(current_user.auth_provider, current_user.auth_uid), :status => :temporary_redirect
183       else
184         # Save the user record
185         session[:new_user] = current_user
186         redirect_to :action => :terms
187       end
188     end
189   end
190
191   def show
192     @user = User.find_by(:display_name => params[:display_name])
193
194     if @user &&
195        (@user.visible? || current_user&.administrator?)
196       @title = @user.display_name
197     else
198       render_unknown_user params[:display_name]
199     end
200   end
201
202   ##
203   # sets a user's status
204   def set_status
205     @user.status = params[:status]
206     @user.save
207     redirect_to user_path(:display_name => params[:display_name])
208   end
209
210   ##
211   # destroy a user, marking them as deleted and removing personal data
212   def destroy
213     @user.destroy
214     redirect_to user_path(:display_name => params[:display_name])
215   end
216
217   ##
218   # display a list of users matching specified criteria
219   def index
220     if request.post?
221       ids = params[:user].keys.collect(&:to_i)
222
223       User.where(:id => ids).update_all(:status => "confirmed") if params[:confirm]
224       User.where(:id => ids).update_all(:status => "deleted") if params[:hide]
225
226       redirect_to url_for(:status => params[:status], :ip => params[:ip], :page => params[:page])
227     else
228       @params = params.permit(:status, :ip)
229
230       conditions = {}
231       conditions[:status] = @params[:status] if @params[:status]
232       conditions[:creation_ip] = @params[:ip] if @params[:ip]
233
234       @user_pages, @users = paginate(:users,
235                                      :conditions => conditions,
236                                      :order => :id,
237                                      :per_page => 50)
238     end
239   end
240
241   ##
242   # omniauth success callback
243   def auth_success
244     auth_info = request.env["omniauth.auth"]
245
246     provider = auth_info[:provider]
247     uid = auth_info[:uid]
248     name = auth_info[:info][:name]
249     email = auth_info[:info][:email]
250
251     email_verified = case provider
252                      when "openid"
253                        uid.match(%r{https://www.google.com/accounts/o8/id?(.*)}) ||
254                        uid.match(%r{https://me.yahoo.com/(.*)})
255                      when "google", "facebook"
256                        true
257                      else
258                        false
259                      end
260
261     if settings = session.delete(:new_user_settings)
262       current_user.auth_provider = provider
263       current_user.auth_uid = uid
264
265       update_user(current_user, settings)
266
267       flash.discard
268
269       session[:user_errors] = current_user.errors.as_json
270
271       redirect_to edit_account_path
272     elsif session[:new_user]
273       session[:new_user].auth_provider = provider
274       session[:new_user].auth_uid = uid
275
276       session[:new_user].status = "active" if email_verified && email == session[:new_user].email
277
278       redirect_to :action => "terms"
279     else
280       user = User.find_by(:auth_provider => provider, :auth_uid => uid)
281
282       if user.nil? && provider == "google"
283         openid_url = auth_info[:extra][:id_info]["openid_id"]
284         user = User.find_by(:auth_provider => "openid", :auth_uid => openid_url) if openid_url
285         user&.update(:auth_provider => provider, :auth_uid => uid)
286       end
287
288       if user
289         case user.status
290         when "pending"
291           unconfirmed_login(user)
292         when "active", "confirmed"
293           successful_login(user, request.env["omniauth.params"]["referer"])
294         when "suspended"
295           failed_login t("sessions.new.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe
296         else
297           failed_login t("sessions.new.auth failure")
298         end
299       else
300         redirect_to :action => "new", :nickname => name, :email => email,
301                     :auth_provider => provider, :auth_uid => uid
302       end
303     end
304   end
305
306   ##
307   # omniauth failure callback
308   def auth_failure
309     flash[:error] = t(params[:message], :scope => "users.auth_failure", :default => t("users.auth_failure.unknown_error"))
310
311     origin = safe_referer(params[:origin]) if params[:origin]
312
313     redirect_to origin || login_url
314   end
315
316   private
317
318   ##
319   # ensure that there is a "user" instance variable
320   def lookup_user_by_name
321     @user = User.find_by(:display_name => params[:display_name])
322   rescue ActiveRecord::RecordNotFound
323     redirect_to :action => "view", :display_name => params[:display_name] unless @user
324   end
325
326   ##
327   # return permitted user parameters
328   def user_params
329     params.require(:user).permit(:email, :email_confirmation, :display_name,
330                                  :auth_provider, :auth_uid,
331                                  :pass_crypt, :pass_crypt_confirmation)
332   end
333
334   ##
335   # check signup acls
336   def check_signup_allowed(email = nil)
337     domain = if email.nil?
338                nil
339              else
340                email.split("@").last
341              end
342
343     mx_servers = if domain.nil?
344                    nil
345                  else
346                    domain_mx_servers(domain)
347                  end
348
349     if blocked = Acl.no_account_creation(request.remote_ip, :domain => domain, :mx => mx_servers)
350       logger.info "Blocked signup from #{request.remote_ip} for #{email}"
351
352       render :action => "blocked"
353     end
354
355     !blocked
356   end
357
358   ##
359   # get list of MX servers for a domains
360   def domain_mx_servers(domain)
361     Resolv::DNS.open do |dns|
362       dns.getresources(domain, Resolv::DNS::Resource::IN::MX).collect(&:exchange).collect(&:to_s)
363     end
364   end
365 end