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 @ 1298:4f746d8966dd

History | View | Annotate | Download (9.99 KB)

1 0:513646585e45 Chris
# Redmine - project management software
2 1295:622f24f53b42 Chris
# Copyright (C) 2006-2013  Jean-Philippe Lang
3 0:513646585e45 Chris
#
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 909:cbb26bc654de Chris
#
9 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
#
14 0:513646585e45 Chris
# 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 909:cbb26bc654de Chris
  include CustomFieldsHelper
21
22 0:513646585e45 Chris
  # 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 1295:622f24f53b42 Chris
      if User.current.logged?
29
        redirect_to home_url
30
      end
31 0:513646585e45 Chris
    else
32
      authenticate_user
33
    end
34 1115:433d4f72a19b Chris
  rescue AuthSourceException => e
35
    logger.error "An error occured when authenticating #{params[:username]}: #{e.message}"
36
    render_error :message => e.message
37 0:513646585e45 Chris
  end
38
39
  # Log out current user and redirect to welcome page
40
  def logout
41 1295:622f24f53b42 Chris
    if User.current.anonymous?
42
      redirect_to home_url
43
    elsif request.post?
44
      logout_user
45
      redirect_to home_url
46
    end
47
    # display the logout form
48 0:513646585e45 Chris
  end
49 909:cbb26bc654de Chris
50 1115:433d4f72a19b Chris
  # Lets user choose a new password
51 0:513646585e45 Chris
  def lost_password
52 1295:622f24f53b42 Chris
    (redirect_to(home_url); return) unless Setting.lost_password?
53 0:513646585e45 Chris
    if params[:token]
54 1295:622f24f53b42 Chris
      @token = Token.find_token("recovery", params[:token].to_s)
55 1115:433d4f72a19b Chris
      if @token.nil? || @token.expired?
56
        redirect_to home_url
57
        return
58
      end
59 0:513646585e45 Chris
      @user = @token.user
60 1115:433d4f72a19b Chris
      unless @user && @user.active?
61
        redirect_to home_url
62
        return
63
      end
64 0:513646585e45 Chris
      if request.post?
65
        @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
66
        if @user.save
67
          @token.destroy
68
          flash[:notice] = l(:notice_account_password_updated)
69 1115:433d4f72a19b Chris
          redirect_to signin_path
70 0:513646585e45 Chris
          return
71 909:cbb26bc654de Chris
        end
72 0:513646585e45 Chris
      end
73
      render :template => "account/password_recovery"
74
      return
75
    else
76
      if request.post?
77 1115:433d4f72a19b Chris
        user = User.find_by_mail(params[:mail].to_s)
78
        # user not found or not active
79
        unless user && user.active?
80
          flash.now[:error] = l(:notice_account_unknown_email)
81
          return
82
        end
83
        # user cannot change its password
84
        unless user.change_password_allowed?
85
          flash.now[:error] = l(:notice_can_t_change_password)
86
          return
87
        end
88 0:513646585e45 Chris
        # create a new token for password recovery
89
        token = Token.new(:user => user, :action => "recovery")
90
        if token.save
91 1115:433d4f72a19b Chris
          Mailer.lost_password(token).deliver
92 0:513646585e45 Chris
          flash[:notice] = l(:notice_account_lost_email_sent)
93 1115:433d4f72a19b Chris
          redirect_to signin_path
94 0:513646585e45 Chris
          return
95
        end
96
      end
97
    end
98
  end
99 909:cbb26bc654de Chris
100 0:513646585e45 Chris
  # User self-registration
101
  def register
102 1295:622f24f53b42 Chris
    (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration]
103 60:cf39b52d24b4 luisf
104 0:513646585e45 Chris
    if request.get?
105
      session[:auth_source_registration] = nil
106 1295:622f24f53b42 Chris
      @user = User.new(:language => current_language.to_s)
107 62:39db1b84d09f luisf
108
      @ssamr_user_details = SsamrUserDetail.new
109
110 0:513646585e45 Chris
    else
111 1115:433d4f72a19b Chris
      user_params = params[:user] || {}
112
      @user = User.new
113
      @user.safe_attributes = user_params
114 0:513646585e45 Chris
      @user.admin = false
115 60:cf39b52d24b4 luisf
116 14:1d32c0a0efbf Chris
      @user.register
117 55:bbb139d5ca95 luisf
118 0:513646585e45 Chris
      if session[:auth_source_registration]
119 14:1d32c0a0efbf Chris
        @user.activate
120 0:513646585e45 Chris
        @user.login = session[:auth_source_registration][:login]
121
        @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
122
        if @user.save
123
          session[:auth_source_registration] = nil
124
          self.logged_user = @user
125
          flash[:notice] = l(:notice_account_activated)
126 1295:622f24f53b42 Chris
          redirect_to my_account_path
127 0:513646585e45 Chris
        end
128
      else
129
        @user.login = params[:user][:login]
130 1115:433d4f72a19b Chris
        unless user_params[:identity_url].present? && user_params[:password].blank? && user_params[:password_confirmation].blank?
131
          @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
132
        end
133 0:513646585e45 Chris
134 62:39db1b84d09f luisf
        @ssamr_user_details = SsamrUserDetail.new(params[:ssamr_user_details])
135
136
        # associates the 2 objects
137
        @user.ssamr_user_detail = @ssamr_user_details
138 192:d1713ab10813 luis
        @selected_institution_id = params[:ssamr_user_details][:institution_id].to_i
139 62:39db1b84d09f luisf
140 192:d1713ab10813 luis
141 0:513646585e45 Chris
        case Setting.self_registration
142
        when '1'
143
          register_by_email_activation(@user)
144
        when '3'
145
          register_automatically(@user)
146
        else
147
          register_manually_by_administrator(@user)
148
        end
149
      end
150
    end
151
  end
152 909:cbb26bc654de Chris
153 0:513646585e45 Chris
  # Token based account activation
154
  def activate
155 1295:622f24f53b42 Chris
    (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present?
156
    token = Token.find_token('register', params[:token].to_s)
157
    (redirect_to(home_url); return) unless token and !token.expired?
158 0:513646585e45 Chris
    user = token.user
159 1295:622f24f53b42 Chris
    (redirect_to(home_url); return) unless user.registered?
160 14:1d32c0a0efbf Chris
    user.activate
161 0:513646585e45 Chris
    if user.save
162
      token.destroy
163
      flash[:notice] = l(:notice_account_activated)
164
    end
165 1115:433d4f72a19b Chris
    redirect_to signin_path
166 0:513646585e45 Chris
  end
167 909:cbb26bc654de Chris
168 0:513646585e45 Chris
  private
169 909:cbb26bc654de Chris
170 0:513646585e45 Chris
  def authenticate_user
171
    if Setting.openid? && using_open_id?
172
      open_id_authenticate(params[:openid_url])
173
    else
174
      password_authentication
175
    end
176
  end
177
178
  def password_authentication
179
    user = User.try_to_login(params[:username], params[:password])
180
181
    if user.nil?
182
      invalid_credentials
183
    elsif user.new_record?
184
      onthefly_creation_failed(user, {:login => user.login, :auth_source_id => user.auth_source_id })
185
    else
186
      # Valid user
187
      successful_authentication(user)
188
    end
189
  end
190
191
  def open_id_authenticate(openid_url)
192 1295:622f24f53b42 Chris
    back_url = signin_url(:autologin => params[:autologin])
193
194
    authenticate_with_open_id(openid_url, :required => [:nickname, :fullname, :email], :return_to => back_url, :method => :post) do |result, identity_url, registration|
195 0:513646585e45 Chris
      if result.successful?
196
        user = User.find_or_initialize_by_identity_url(identity_url)
197
        if user.new_record?
198
          # Self-registration off
199 1295:622f24f53b42 Chris
          (redirect_to(home_url); return) unless Setting.self_registration?
200 0:513646585e45 Chris
201
          # Create on the fly
202
          user.login = registration['nickname'] unless registration['nickname'].nil?
203
          user.mail = registration['email'] unless registration['email'].nil?
204
          user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
205
          user.random_password
206 14:1d32c0a0efbf Chris
          user.register
207 0:513646585e45 Chris
208
          case Setting.self_registration
209
          when '1'
210
            register_by_email_activation(user) do
211
              onthefly_creation_failed(user)
212
            end
213
          when '3'
214
            register_automatically(user) do
215
              onthefly_creation_failed(user)
216
            end
217
          else
218
            register_manually_by_administrator(user) do
219
              onthefly_creation_failed(user)
220
            end
221 909:cbb26bc654de Chris
          end
222 0:513646585e45 Chris
        else
223
          # Existing record
224
          if user.active?
225
            successful_authentication(user)
226
          else
227
            account_pending
228
          end
229
        end
230
      end
231
    end
232
  end
233 909:cbb26bc654de Chris
234 0:513646585e45 Chris
  def successful_authentication(user)
235 1115:433d4f72a19b Chris
    logger.info "Successful authentication for '#{user.login}' from #{request.remote_ip} at #{Time.now.utc}"
236 0:513646585e45 Chris
    # Valid user
237
    self.logged_user = user
238
    # generate a key and set cookie if autologin
239
    if params[:autologin] && Setting.autologin?
240 210:0579821a129a Chris
      set_autologin_cookie(user)
241 0:513646585e45 Chris
    end
242
    call_hook(:controller_account_success_authentication_after, {:user => user })
243 1295:622f24f53b42 Chris
    redirect_back_or_default my_page_path
244 0:513646585e45 Chris
  end
245 909:cbb26bc654de Chris
246 210:0579821a129a Chris
  def set_autologin_cookie(user)
247
    token = Token.create(:user => user, :action => 'autologin')
248
    cookie_options = {
249
      :value => token.value,
250
      :expires => 1.year.from_now,
251
      :path => (Redmine::Configuration['autologin_cookie_path'] || '/'),
252
      :secure => (Redmine::Configuration['autologin_cookie_secure'] ? true : false),
253
      :httponly => true
254
    }
255 1295:622f24f53b42 Chris
    cookies[autologin_cookie_name] = cookie_options
256 210:0579821a129a Chris
  end
257 0:513646585e45 Chris
258
  # Onthefly creation failed, display the registration form to fill/fix attributes
259
  def onthefly_creation_failed(user, auth_source_options = { })
260
    @user = user
261
    session[:auth_source_registration] = auth_source_options unless auth_source_options.empty?
262
    render :action => 'register'
263
  end
264
265
  def invalid_credentials
266
    logger.warn "Failed login for '#{params[:username]}' from #{request.remote_ip} at #{Time.now.utc}"
267
    flash.now[:error] = l(:notice_account_invalid_creditentials)
268
  end
269
270
  # Register a user for email activation.
271
  #
272
  # Pass a block for behavior when a user fails to save
273
  def register_by_email_activation(user, &block)
274
    token = Token.new(:user => user, :action => "register")
275
    if user.save and token.save
276 1115:433d4f72a19b Chris
      Mailer.register(token).deliver
277 0:513646585e45 Chris
      flash[:notice] = l(:notice_account_register_done)
278 1115:433d4f72a19b Chris
      redirect_to signin_path
279 0:513646585e45 Chris
    else
280
      yield if block_given?
281
    end
282
  end
283 909:cbb26bc654de Chris
284 0:513646585e45 Chris
  # Automatically register a user
285
  #
286
  # Pass a block for behavior when a user fails to save
287
  def register_automatically(user, &block)
288
    # Automatic activation
289 14:1d32c0a0efbf Chris
    user.activate
290 0:513646585e45 Chris
    user.last_login_on = Time.now
291
    if user.save
292
      self.logged_user = user
293
      flash[:notice] = l(:notice_account_activated)
294 1295:622f24f53b42 Chris
      redirect_to my_account_path
295 0:513646585e45 Chris
    else
296
      yield if block_given?
297
    end
298
  end
299 909:cbb26bc654de Chris
300 0:513646585e45 Chris
  # Manual activation by the administrator
301
  #
302
  # Pass a block for behavior when a user fails to save
303
  def register_manually_by_administrator(user, &block)
304
    if user.save
305 62:39db1b84d09f luisf
306
       @ssamr_user_details.save!
307
308 0:513646585e45 Chris
      # Sends an email to the administrators
309 1115:433d4f72a19b Chris
      Mailer.account_activation_request(user).deliver
310 0:513646585e45 Chris
      account_pending
311
    else
312
      yield if block_given?
313
    end
314
  end
315
316
  def account_pending
317
    flash[:notice] = l(:notice_account_pending)
318 1115:433d4f72a19b Chris
    redirect_to signin_path
319 0:513646585e45 Chris
  end
320
end