]> git.openstreetmap.org Git - rails.git/blob - app/controllers/concerns/session_methods.rb
Merge remote-tracking branch 'upstream/pull/4664'
[rails.git] / app / controllers / concerns / session_methods.rb
1 module SessionMethods
2   extend ActiveSupport::Concern
3
4   private
5
6   ##
7   # return the URL to use for authentication
8   def auth_url(provider, uid, referer = nil)
9     params = { :provider => provider }
10
11     params[:openid_url] = uid if provider == "openid"
12
13     if referer.nil?
14       params[:origin] = request.path
15     else
16       params[:origin] = "#{request.path}?referer=#{CGI.escape(referer)}"
17       params[:referer] = referer
18     end
19
20     auth_path(params)
21   end
22
23   ##
24   # process a successful login
25   def successful_login(user, referer = nil)
26     session[:user] = user.id
27     session[:fingerprint] = user.fingerprint
28     session_expires_after 28.days if session[:remember_me]
29
30     target = referer || session[:referer] || url_for(:controller => :site, :action => :index)
31
32     # The user is logged in, so decide where to send them:
33     #
34     # - If they haven't seen the contributor terms, send them there.
35     # - If they have a block on them, show them that.
36     # - If they were referred to the login, send them back there.
37     # - Otherwise, send them to the home page.
38     if !user.terms_seen
39       redirect_to :controller => :users, :action => :terms, :referer => target
40     elsif user.blocked_on_view
41       redirect_to user.blocked_on_view, :referer => target
42     else
43       redirect_to target
44     end
45
46     session.delete(:remember_me)
47     session.delete(:referer)
48   end
49
50   ##
51   # process a failed login
52   def failed_login(message, username = nil)
53     flash[:error] = message
54
55     redirect_to :controller => "sessions", :action => "new", :referer => session[:referer],
56                 :username => username, :remember_me => session[:remember_me]
57
58     session.delete(:remember_me)
59     session.delete(:referer)
60   end
61
62   ##
63   #
64   def unconfirmed_login(user)
65     session[:pending_user] = user.id
66
67     redirect_to :controller => "confirmations", :action => "confirm",
68                 :display_name => user.display_name, :referer => session[:referer]
69
70     session.delete(:remember_me)
71     session.delete(:referer)
72   end
73
74   ##
75   #
76   def disable_terms_redirect
77     # this is necessary otherwise going to the user terms page, when
78     # having not agreed already would cause an infinite redirect loop.
79     # it's .now so that this doesn't propagate to other pages.
80     flash.now[:skip_terms] = true
81   end
82 end