]> git.openstreetmap.org Git - rails.git/blob - app/controllers/sessions_controller.rb
Merge pull request #4633 from tomhughes/trace-images
[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
19     parse_oauth_referer session[:referer]
20   end
21
22   def create
23     session[:remember_me] ||= params[:remember_me]
24     session[:referer] = safe_referer(params[:referer]) if params[:referer]
25     password_authentication(params[:username].strip, params[:password])
26   end
27
28   def destroy
29     @title = t ".title"
30
31     if request.post?
32       session.delete(:pending_user)
33       session.delete(:user)
34       session_expires_automatically
35
36       referer = safe_referer(params[:referer]) if params[:referer]
37
38       redirect_to referer || { :controller => "site", :action => "index" }
39     end
40   end
41
42   private
43
44   ##
45   # handle password authentication
46   def password_authentication(username, password)
47     if (user = User.authenticate(:username => username, :password => password))
48       successful_login(user)
49     elsif (user = User.authenticate(:username => username, :password => password, :pending => true))
50       unconfirmed_login(user)
51     elsif User.authenticate(:username => username, :password => password, :suspended => true)
52       failed_login({ :partial => "sessions/suspended_flash" }, username)
53     else
54       failed_login t("sessions.new.auth failure"), username
55     end
56   end
57 end