]> git.openstreetmap.org Git - rails.git/blob - app/controllers/sessions_controller.rb
Merge branch 'master' into HEAD
[rails.git] / app / controllers / sessions_controller.rb
1 class SessionsController < ApplicationController
2   include SessionMethods
3
4   layout "site"
5
6   before_action :disable_terms_redirect, :only => [:destroy]
7   before_action :authorize_web
8   before_action :set_locale
9   before_action :check_database_readable
10   before_action :require_cookies, :only => [:new]
11
12   authorize_resource :class => false
13
14   def new
15     override_content_security_policy_directives(:form_action => []) if Settings.csp_enforce || Settings.key?(:csp_report_url)
16
17     session[:referer] = safe_referer(params[:referer]) if params[:referer]
18   end
19
20   def create
21     session[:remember_me] ||= params[:remember_me]
22     session[:referer] = safe_referer(params[:referer]) if params[:referer]
23     password_authentication(params[:username], params[:password])
24   end
25
26   def destroy
27     @title = t "sessions.destroy.title"
28
29     if request.post?
30       if session[:token]
31         token = UserToken.find_by(:token => session[:token])
32         token&.destroy
33         session.delete(:token)
34       end
35
36       session.delete(:user)
37       session_expires_automatically
38
39       referer = safe_referer(params[:referer]) if params[:referer]
40
41       if referer
42         redirect_to referer
43       else
44         redirect_to :controller => "site", :action => "index"
45       end
46     end
47   end
48
49   private
50
51   ##
52   # handle password authentication
53   def password_authentication(username, password)
54     if (user = User.authenticate(:username => username, :password => password))
55       successful_login(user)
56     elsif (user = User.authenticate(:username => username, :password => password, :pending => true))
57       unconfirmed_login(user)
58     elsif User.authenticate(:username => username, :password => password, :suspended => true)
59       failed_login t("sessions.new.account is suspended", :webmaster => "mailto:#{Settings.support_email}").html_safe, username
60     else
61       failed_login t("sessions.new.auth failure"), username
62     end
63   end
64 end