summaryrefslogtreecommitdiffstats
path: root/app/controllers/sessions_controller.rb
blob: c9f7ec51f607a55839284163509677a19889584c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# encoding: utf-8
#--
#   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
#   Copyright (C) 2007, 2008 Johan Sørensen <johan@johansorensen.com>
#   Copyright (C) 2008 August Lilleaas <augustlilleaas@gmail.com>
#   Copyright (C) 2008 Patrick Aljord <patcito@gmail.com>
#   Copyright (C) 2008 Tor Arne Vestbø <tavestbo@trolltech.com>
#   Copyright (C) 2009 Fabio Akita <fabio.akita@gmail.com>
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#++

require "openid"
require "yadis"

# This controller handles the login/logout function of the site.
class SessionsController < ApplicationController
  skip_before_filter :public_and_logged_in
  renders_in_site_specific_context
  ssl_required :new, :create, :destroy
  
  # render new.rhtml
  def new
  end

  def create
    if using_open_id?
      open_id_authentication(params[:openid_url])
    else
      password_authentication(params[:email], params[:password])
    end
  end

  def destroy
    self.current_user.forget_me if logged_in?
    cookies.delete :auth_token
    clear_varnish_auth_cookie
    reset_session
    flash[:notice] = "You have been logged out."
    redirect_back_or_default('/')
  end

  protected

  # if user doesn't exist, it gets created and activated,
  # else if the user already exists with same identity_url, it just logs in
  def open_id_authentication(openid_url)
    authenticate_with_open_id(openid_url, :required => [:nickname, :email], :optional => [:fullname]) do |result, identity_url, registration|
      if result.successful?
        @user = User.find_or_initialize_by_identity_url(identity_url)
        if @user.new_record?

          session[:openid_nickname] = registration['nickname']
          session[:openid_email]    = registration['email']
          session[:openid_fullname] = registration['fullname']
          session[:openid_url]      = identity_url
          flash[:notice] = "You now need to finalize your account"
          redirect_to :controller => 'users', :action => 'openid_build' and return
        end
        self.current_user = @user
        successful_login
      else
        failed_login result.message, 'openid'
      end
    end
  rescue ActiveRecord::RecordInvalid => invalid
    flash[:error] = %Q{This login (<strong>#{@user.login}</strong>) already exists, 
      please <a href="#{@user.identity_url}"> choose a different persona/nickname 
      or modify the current one</a>}
    redirect_to login_path(:method => 'openid')
  end

  def password_authentication(email, password)
    self.current_user = User.authenticate(email, password)
    if logged_in?
      successful_login
    else
      failed_login("Email and/or password didn't match, please try again.")
    end
  end

  def failed_login(message = "Authentication failed.",method="")
    if method==''
      flash.now[:error] = message
      render :action => 'new'
    else
      redirect_to login_path(:method=>method)
      flash[:error] = message
    end
  end

  def successful_login
    if params[:remember_me] == "1"
      self.current_user.remember_me
      cookies[:auth_token] = { 
        :value => self.current_user.remember_token , 
        :expires => self.current_user.remember_token_expires_at,
        :domain => ".#{GitoriousConfig['gitorious_host']}",
      }
    end
    check_state_and_redirect('/')
  end
  
  def check_state_and_redirect(redirection_url)
    if current_user.pending?
      flash[:notice] = "You need to accept the terms"
      redirect_to user_license_path(current_user) and return
    else
      flash[:notice] = "Logged in successfully"
      redirect_back_or_default(redirection_url)
    end
  end

end