To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / app / controllers / account_controller.rb @ 743:7ded87cc4b80

History | View | Annotate | Download (9.5 KB)

1
# Redmine - project management software
2
# Copyright (C) 2006-2009  Jean-Philippe Lang
3
#
4
# This program is free software; you can redistribute it and/or
5
# modify it under the terms of the GNU General Public License
6
# as published by the Free Software Foundation; either version 2
7
# of the License, or (at your option) any later version.
8
# 
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
# 
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17

    
18
class AccountController < ApplicationController
19
  helper :custom_fields
20
  include CustomFieldsHelper   
21
  
22
  # prevents login action to be filtered by check_if_login_required application scope filter
23
  skip_before_filter :check_if_login_required
24

    
25
  # Login request and validation
26
  def login
27
    if request.get?
28
      logout_user
29
    else
30
      authenticate_user
31
    end
32
  end
33

    
34
  # Log out current user and redirect to welcome page
35
  def logout
36
    logout_user
37
    redirect_to home_url
38
  end
39
  
40
  # Enable user to choose a new password
41
  def lost_password
42
    redirect_to(home_url) && return unless Setting.lost_password?
43
    if params[:token]
44
      @token = Token.find_by_action_and_value("recovery", params[:token])
45
      redirect_to(home_url) && return unless @token and !@token.expired?
46
      @user = @token.user
47
      if request.post?
48
        @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
49
        if @user.save
50
          @token.destroy
51
          flash[:notice] = l(:notice_account_password_updated)
52
          redirect_to :action => 'login'
53
          return
54
        end 
55
      end
56
      render :template => "account/password_recovery"
57
      return
58
    else
59
      if request.post?
60
        user = User.find_by_mail(params[:mail])
61
        # 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
        # create a new token for password recovery
66
        token = Token.new(:user => user, :action => "recovery")
67
        if token.save
68
          Mailer.deliver_lost_password(token)
69
          flash[:notice] = l(:notice_account_lost_email_sent)
70
          redirect_to :action => 'login'
71
          return
72
        end
73
      end
74
    end
75
  end
76
  
77
  # User self-registration
78
  def register
79
    redirect_to(home_url) && return unless Setting.self_registration? || session[:auth_source_registration]
80

    
81
    if request.get?
82
      session[:auth_source_registration] = nil
83
      @user = User.new(:language => Setting.default_language)
84

    
85
      @ssamr_user_details = SsamrUserDetail.new
86

    
87
    else
88
      @user = User.new(params[:user])
89
      @user.admin = false
90
                  
91
      @user.register
92
      
93
      if session[:auth_source_registration]
94
        @user.activate
95
        @user.login = session[:auth_source_registration][:login]
96
        @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
97
        if @user.save
98
          session[:auth_source_registration] = nil
99
          self.logged_user = @user
100
          flash[:notice] = l(:notice_account_activated)
101
          redirect_to :controller => 'my', :action => 'account'
102
        end
103
      else
104
        @user.login = params[:user][:login]
105
        @user.password, @user.password_confirmation = params[:password], params[:password_confirmation]
106

    
107
        @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details])
108

    
109
        # associates the 2 objects
110
        @user.ssamr_user_detail = @ssamr_user_details
111
        @selected_institution_id = params[:ssamr_user_details][:institution_id].to_i
112

    
113
        
114
        case Setting.self_registration
115
        when '1'
116
          register_by_email_activation(@user)
117
        when '3'
118
          register_automatically(@user)
119
        else
120
          register_manually_by_administrator(@user)
121
        end
122
      end
123
    end
124
  end
125
  
126
  # Token based account activation
127
  def activate
128
    redirect_to(home_url) && return unless Setting.self_registration? && params[:token]
129
    token = Token.find_by_action_and_value('register', params[:token])
130
    redirect_to(home_url) && return unless token and !token.expired?
131
    user = token.user
132
    redirect_to(home_url) && return unless user.registered?
133
    user.activate
134
    if user.save
135
      token.destroy
136
      flash[:notice] = l(:notice_account_activated)
137
    end
138
    redirect_to :action => 'login'
139
  end
140
  
141
  private
142
  
143
  def logout_user
144
    if User.current.logged?
145
      cookies.delete :autologin
146
      Token.delete_all(["user_id = ? AND action = ?", User.current.id, 'autologin'])
147
      self.logged_user = nil
148
    end
149
  end
150
  
151
  def authenticate_user
152
    if Setting.openid? && using_open_id?
153
      open_id_authenticate(params[:openid_url])
154
    else
155
      password_authentication
156
    end
157
  end
158

    
159
  def password_authentication
160
    user = User.try_to_login(params[:username], params[:password])
161

    
162
    if user.nil?
163
      invalid_credentials
164
    elsif user.new_record?
165
      onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
166
    else
167
      # Valid user
168
      successful_authentication(user)
169
    end
170
  end
171

    
172
  
173
  def open_id_authenticate(openid_url)
174
    authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => signin_url) do |result, identity_url, registration|
175
      if result.successful?
176
        user = User.find_or_initialize_by_identity_url(identity_url)
177
        if user.new_record?
178
          # Self-registration off
179
          redirect_to(home_url) && return unless Setting.self_registration?
180

    
181
          # Create on the fly
182
          user.login = registration['nickname'] unless registration['nickname'].nil?
183
          user.mail = registration['email'] unless registration['email'].nil?
184
          user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
185
          user.random_password
186
          user.register
187

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

    
238
  # Onthefly creation failed, display the registration form to fill/fix attributes
239
  def onthefly_creation_failed(user, auth_source_options = { })
240
    @user = user
241
    session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
242
    render :action => 'register'
243
  end
244

    
245
  def invalid_credentials
246
    logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
247
    flash.now[:error] = l(:notice_account_invalid_creditentials)
248
  end
249

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

    
286
       @ssamr_user_details.save!
287

    
288
      # Sends an email to the administrators
289
      Mailer.deliver_account_activation_request(user)
290
      account_pending
291
    else
292
      yield if block_given?
293
    end
294
  end
295

    
296
  def account_pending
297
    flash[:notice] = l(:notice_account_pending)
298
    redirect_to :action => 'login'
299
  end
300
end