account_controller.rb 9.13 KB
Newer Older
1
# Redmine - project management software
2
# Copyright (C) 2006-2009  Jean-Philippe Lang
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# 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 General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class AccountController < ApplicationController
  helper :custom_fields
  include CustomFieldsHelper   
  
  # prevents login action to be filtered by check_if_login_required application scope filter
23
  skip_before_filter :check_if_login_required
24 25 26 27

  # Login request and validation
  def login
    if request.get?
Eric Davis's avatar
Eric Davis committed
28
      logout_user
29
    else
Eric Davis's avatar
Eric Davis committed
30
      authenticate_user
31 32 33 34 35
    end
  end

  # Log out current user and redirect to welcome page
  def logout
Eric Davis's avatar
Eric Davis committed
36
    logout_user
37
    redirect_to home_url
38 39 40 41
  end
  
  # Enable user to choose a new password
  def lost_password
42
    redirect_to(home_url) && return unless Setting.lost_password?
43 44
    if params[:token]
      @token = Token.find_by_action_and_value("recovery", params[:token])
45
      redirect_to(home_url) && return unless @token and !@token.expired?
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
      @user = @token.user
      if request.post?
        @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
        if @user.save
          @token.destroy
          flash[:notice] = l(:notice_account_password_updated)
          redirect_to :action => 'login'
          return
        end 
      end
      render :template => "account/password_recovery"
      return
    else
      if request.post?
        user = User.find_by_mail(params[:mail])
        # user not found in db
62
        (flash.now[:error] = l(:notice_account_unknown_email); return) unless user
63
        # user uses an external authentification
64
        (flash.now[:error] = l(:notice_can_t_change_password); return) if user.auth_source_id
65 66 67 68 69 70 71 72 73 74 75 76 77 78
        # create a new token for password recovery
        token = Token.new(:user => user, :action => "recovery")
        if token.save
          Mailer.deliver_lost_password(token)
          flash[:notice] = l(:notice_account_lost_email_sent)
          redirect_to :action => 'login'
          return
        end
      end
    end
  end
  
  # User self-registration
  def register
79
    redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
80
    if request.get?
81
      session[:auth_source_registration] = nil
82
      @user = User.new(:language => Setting.default_language)
83
    else
84 85
      @user = User.new(params[:user])
      @user.admin = false
86
      @user.register
87
      if session[:auth_source_registration]
88
        @user.activate
89 90
        @user.login = session[:auth_source_registration][:login]
        @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
91
        if @user.save
92
          session[:auth_source_registration] = nil
93
          self.logged_user = @user
94
          flash[:notice] = l(:notice_account_activated)
95
          redirect_to :controller => 'my', :action => 'account'
96 97
        end
      else
98 99
        @user.login = params[:user][:login]
        @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
100

101 102
        case Setting.self_registration
        when '1'
103
          register_by_email_activation(@user)
104
        when '3'
105
          register_automatically(@user)
106
        else
107
          register_manually_by_administrator(@user)
108 109 110 111
        end
      end
    end
  end
112
  
113 114 115 116 117 118
  # Token based account activation
  def activate
    redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
    token = Token.find_by_action_and_value('register', params[:token])
    redirect_to(home_url) && return unless token and !token.expired?
    user = token.user
119 120
    redirect_to(home_url) && return unless user.registered?
    user.activate
121 122 123 124 125 126 127
    if user.save
      token.destroy
      flash[:notice] = l(:notice_account_activated)
    end
    redirect_to :action => 'login'
  end
  
128
  private
Eric Davis's avatar
Eric Davis committed
129 130
  
  def logout_user
131 132 133 134 135
    if User.current.logged?
      cookies.delete :autologin
      Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin'])
      self.logged_user = nil
    end
Eric Davis's avatar
Eric Davis committed
136 137
  end
  
Eric Davis's avatar
Eric Davis committed
138 139 140 141 142 143 144 145
  def authenticate_user
    if Setting.openid? && using_open_id?
      open_id_authenticate(params[:openid_url])
    else
      password_authentication
    end
  end

146 147
  def password_authentication
    user = User.try_to_login(params[:username], params[:password])
Eric Davis's avatar
Eric Davis committed
148

149
    if user.nil?
Eric Davis's avatar
Eric Davis committed
150
      invalid_credentials
151
    elsif user.new_record?
152
      onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
153 154 155 156 157 158 159 160 161 162 163 164
    else
      # Valid user
      successful_authentication(user)
    end
  end

  
  def open_id_authenticate(openid_url)
    authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration|
      if result.successful?
        user = User.find_or_initialize_by_identity_url(identity_url)
        if user.new_record?
165 166 167
          # Self-registration off
          redirect_to(home_url) && return unless Setting.self_registration?

168
          # Create on the fly
169 170 171
          user.login = registration['nickname'] unless registration['nickname'].nil?
          user.mail = registration['email'] unless registration['email'].nil?
          user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
172
          user.random_password
173
          user.register
174 175 176

          case Setting.self_registration
          when '1'
177
            register_by_email_activation(user) do
178
              onthefly_creation_failed(user)
179 180
            end
          when '3'
181
            register_automatically(user) do
182
              onthefly_creation_failed(user)
183
            end
184
          else
185
            register_manually_by_administrator(user) do
186
              onthefly_creation_failed(user)
187 188
            end
          end          
189
        else
190
          # Existing record
191 192 193 194 195
          if user.active?
            successful_authentication(user)
          else
            account_pending
          end
196 197 198 199 200 201 202 203 204 205
        end
      end
    end
  end
  
  def successful_authentication(user)
    # Valid user
    self.logged_user = user
    # generate a key and set cookie if autologin
    if params[:autologin] && Setting.autologin?
206
      set_autologin_cookie(user)
207
    end
208
    call_hook(:controller_account_success_authentication_after, {:user => user })
209 210
    redirect_back_or_default :controller => 'my', :action => 'page'
  end
211 212 213 214 215 216 217 218
  
  def set_autologin_cookie(user)
    token = Token.create(:user => user, :action => 'autologin')
    cookie_name = Redmine::Configuration['autologin_cookie_name'] || 'autologin'
    cookie_options = {
      :value => token.value,
      :expires => 1.year.from_now,
      :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
219 220
      :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
      :httponly => true
221 222 223
    }
    cookies[cookie_name] = cookie_options
  end
224

225 226 227 228 229 230 231
  # Onthefly creation failed, display the registration form to fill/fix attributes
  def onthefly_creation_failed(user, auth_source_options = { })
    @user = user
    session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
    render :action => 'register'
  end

Eric Davis's avatar
Eric Davis committed
232
  def invalid_credentials
233
    logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
Eric Davis's avatar
Eric Davis committed
234 235 236
    flash.now[:error] = l(:notice_account_invalid_creditentials)
  end

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  # Register a user for email activation.
  #
  # Pass a block for behavior when a user fails to save
  def register_by_email_activation(user, &block)
    token = Token.new(:user => user, :action => "register")
    if user.save and token.save
      Mailer.deliver_register(token)
      flash[:notice] = l(:notice_account_register_done)
      redirect_to :action => 'login'
    else
      yield if block_given?
    end
  end
  
  # Automatically register a user
  #
  # Pass a block for behavior when a user fails to save
  def register_automatically(user, &block)
    # Automatic activation
256
    user.activate
257
    user.last_login_on = Time.now
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
    if user.save
      self.logged_user = user
      flash[:notice] = l(:notice_account_activated)
      redirect_to :controller => 'my', :action => 'account'
    else
      yield if block_given?
    end
  end
  
  # Manual activation by the administrator
  #
  # Pass a block for behavior when a user fails to save
  def register_manually_by_administrator(user, &block)
    if user.save
      # Sends an email to the administrators
      Mailer.deliver_account_activation_request(user)
274
      account_pending
275 276 277 278
    else
      yield if block_given?
    end
  end
279 280 281 282 283

  def account_pending
    flash[:notice] = l(:notice_account_pending)
    redirect_to :action => 'login'
  end
Jean-Philippe Lang's avatar
Jean-Philippe Lang committed
284
end