Class: AccountController

Inherits:
ApplicationController show all
Includes:
CustomFieldsHelper
Defined in:
app/controllers/account_controller.rb

Overview

Redmine - project management software Copyright © 2006-2022 Jean-Philippe Lang

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.

Constant Summary

Constants included from CustomFieldsHelper

CustomFieldsHelper::CUSTOM_FIELDS_TABS

Instance Method Summary collapse

Methods included from CustomFieldsHelper

#custom_field_formats_for_select, #custom_field_label_tag, #custom_field_name_tag, #custom_field_tag, #custom_field_tag_for_bulk_edit, #custom_field_tag_id, #custom_field_tag_name, #custom_field_tag_with_label, #custom_field_title, #custom_field_type_options, #custom_field_value_tag, #edit_tag_style_tag, #format_value, #render_api_custom_values, #render_custom_field_format_partial, #render_custom_field_values, #render_custom_fields_tabs, #select_type_radio_buttons, #show_value

Methods inherited from ApplicationController

#_include_layout?, accept_api_auth, #accept_api_auth?, accept_rss_auth, #accept_rss_auth?, #api_key_from_request, #api_offset_and_limit, #api_request?, #api_switch_user_from_request, #authorize, #authorize_global, #autologin_cookie_name, #back_url, #check_if_login_required, #check_password_change, #check_project_privacy, #check_twofa_activation, #deny_access, #filename_for_content_disposition, #find_attachments, #find_current_user, #find_issue, #find_issues, #find_model_object, #find_optional_project, #find_optional_project_by_id, #find_project, #find_project_by_project_id, #find_project_from_association, #handle_unverified_request, #init_twofa_pairing_and_send_code_for, #logged_user=, #logout_user, #missing_template, model_object, #parse_params_for_bulk_update, #parse_qvalues, #per_page_option, #query_error, #query_statement_invalid, #record_project_usage, #redirect_back_or_default, #redirect_to_referer_or, #render_403, #render_404, #render_api_errors, #render_api_head, #render_api_ok, #render_attachment_warning_if_needed, #render_error, #render_feed, #render_validation_errors, #replace_none_values_with_blank, #require_admin, #require_admin_or_api_request, #require_login, #session_expiration, #session_expired?, #set_localization, #start_user_session, #try_to_autologin, #use_layout, #user_setup, #verify_authenticity_token

Methods included from Redmine::SudoMode::Controller

#process_sudo_form, #render_sudo_form, #require_sudo_mode, #sudo_mode, #sudo_timestamp_valid?, #update_sudo_timestamp!

Methods included from Redmine::MenuManager::MenuController

#current_menu, #current_menu_item, included, #menu_items, #redirect_to_menu_item, #redirect_to_project_menu_item

Methods included from Redmine::Search::Controller

#default_search_scope, #default_search_scopes, included

Methods included from AvatarsHelper

#assignee_avatar, #author_avatar, #avatar, #avatar_edit_link

Methods included from GravatarHelper::PublicMethods

#gravatar, #gravatar_api_url, #gravatar_for, #gravatar_url

Methods included from RoutesHelper

#_bulk_update_issues_path, #_new_project_issue_path, #_new_time_entry_path, #_project_calendar_path, #_project_gantt_path, #_project_issues_path, #_project_issues_url, #_project_news_path, #_report_time_entries_path, #_time_entries_path, #board_path

Methods included from Redmine::Hook::Helper

#call_hook

Methods included from Redmine::Pagination

#paginate, #paginator

Methods included from Redmine::I18n

#current_language, #day_letter, #day_name, #find_language, #format_date, #format_hours, #format_time, included, #l, #l_hours, #l_hours_short, #l_or_humanize, #languages_options, #ll, #lu, #month_name, #set_language_if_valid, #valid_languages

Instance Method Details

#activateObject

Token based account activation



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/account_controller.rb', line 173

def activate
  (redirect_to(home_url); return) unless Setting.self_registration? && params[:token].present?
  token = Token.find_token('register', params[:token].to_s)
  (redirect_to(home_url); return) unless token and !token.expired?
  user = token.user
  (redirect_to(home_url); return) unless user.registered?
  user.activate
  if user.save
    token.destroy
    flash[:notice] = l(:notice_account_activated)
  end
  redirect_to 
end

#activation_emailObject

Sends a new account activation email



188
189
190
191
192
193
194
195
196
197
198
# File 'app/controllers/account_controller.rb', line 188

def activation_email
  if session[:registered_user_id] && Setting.self_registration == '1'
    user_id = session.delete(:registered_user_id).to_i
    user = User.find_by_id(user_id)
    if user && user.registered?
      register_by_email_activation(user)
      return
    end
  end
  redirect_to(home_url)
end

#loginObject

Login request and validation



31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/account_controller.rb', line 31

def 
  if request.post?
    authenticate_user
  else
    if User.current.logged?
      redirect_back_or_default home_url, :referer => true
    end
  end
rescue AuthSourceException => e
  logger.error "An error occurred when authenticating #{params[:username]}: #{e.message}"
  render_error :message => e.message
end

#logoutObject

Log out current user and redirect to welcome page



45
46
47
48
49
50
51
52
53
# File 'app/controllers/account_controller.rb', line 45

def logout
  if User.current.anonymous?
    redirect_to home_url
  elsif request.post?
    logout_user
    redirect_to home_url
  end
  # display the logout form
end

#lost_passwordObject

Lets user choose a new password



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
126
127
128
129
130
# File 'app/controllers/account_controller.rb', line 56

def lost_password
  (redirect_to(home_url); return) unless Setting.lost_password?
  if prt = (params[:token] || session[:password_recovery_token])
    @token = Token.find_token("recovery", prt.to_s)
    if @token.nil?
      redirect_to home_url
      return
    elsif @token.expired?
      # remove expired token from session and let user try again
      session[:password_recovery_token] = nil
      flash[:error] = l(:error_token_expired)
      redirect_to lost_password_url
      return
    end

    # redirect to remove the token query parameter from the URL and add it to the session
    if request.query_parameters[:token].present?
      session[:password_recovery_token] = @token.value
      redirect_to lost_password_url
      return
    end

    @user = @token.user
    unless @user && @user.active?
      redirect_to home_url
      return
    end
    if request.post?
      if @user.must_change_passwd? && @user.check_password?(params[:new_password])
        flash.now[:error] = l(:notice_new_password_must_be_different)
      else
        @user.password, @user.password_confirmation = params[:new_password], params[:new_password_confirmation]
        @user.must_change_passwd = false
        if @user.save
          @token.destroy
          Mailer.deliver_password_updated(@user, User.current)
          flash[:notice] = l(:notice_account_password_updated)
          redirect_to 
          return
        end
      end
    end
    render :template => "account/password_recovery"
    return
  else
    if request.post?
      email = params[:mail].to_s.strip
      user = User.find_by_mail(email)
      # user not found
      unless user
        flash.now[:error] = l(:notice_account_unknown_email)
        return
      end
      unless user.active?
        handle_inactive_user(user, lost_password_path)
        return
      end
      # user cannot change its password
      unless user.change_password_allowed?
        flash.now[:error] = l(:notice_can_t_change_password)
        return
      end
      # create a new token for password recovery
      token = Token.new(:user => user, :action => "recovery")
      if token.save
        # Don't use the param to send the email
        recipent = user.mails.detect {|e| email.casecmp(e) == 0} || user.mail
        Mailer.deliver_lost_password(user, token, recipent)
        flash[:notice] = l(:notice_account_lost_email_sent)
        redirect_to 
        return
      end
    end
  end
end

#registerObject

User self-registration



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/account_controller.rb', line 133

def register
  (redirect_to(home_url); return) unless Setting.self_registration? || session[:auth_source_registration]
  if !request.post?
    session[:auth_source_registration] = nil
    @user = User.new(:language => current_language.to_s)
  else
    user_params = params[:user] || {}
    @user = User.new
    @user.safe_attributes = user_params
    @user.pref.safe_attributes = params[:pref]
    @user.admin = false
    @user.register
    if session[:auth_source_registration]
      @user.activate
      @user. = session[:auth_source_registration][:login]
      @user.auth_source_id = session[:auth_source_registration][:auth_source_id]
      if @user.save
        session[:auth_source_registration] = nil
        self.logged_user = @user
        flash[:notice] = l(:notice_account_activated)
        redirect_to 
      end
    else
      unless user_params[:password].blank? && user_params[:password_confirmation].blank?
        @user.password, @user.password_confirmation = user_params[:password], user_params[:password_confirmation]
      end

      case Setting.self_registration
      when '1'
        register_by_email_activation(@user)
      when '3'
        register_automatically(@user)
      else
        register_manually_by_administrator(@user)
      end
    end
  end
end

#twofaObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'app/controllers/account_controller.rb', line 221

def twofa
  if @twofa.verify!(params[:twofa_code].to_s)
    destroy_twofa_session
    handle_active_user(@user)
  # allow at most 3 otp entry tries per successfull password entry
  # this allows using anti brute force techniques on the password entry to also
  # prevent brute force attacks on the one-time password
  elsif session[:twofa_tries_counter] > 3
    destroy_twofa_session
    flash[:error] = l('twofa_too_many_tries')
    redirect_to home_url
  else
    flash[:error] = l('twofa_invalid_code')
    redirect_to 
  end
end

#twofa_confirmObject



217
218
219
# File 'app/controllers/account_controller.rb', line 217

def twofa_confirm
  @twofa_view = @twofa.otp_confirm_view_variables
end

#twofa_resendObject



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/controllers/account_controller.rb', line 203

def twofa_resend
  # otp resends count toward the maximum of 3 otp entry tries per password entry
  if session[:twofa_tries_counter] > 3
    destroy_twofa_session
    flash[:error] = l('twofa_too_many_tries')
    redirect_to home_url
  else
    if @twofa.send_code(controller: 'account', action: 'twofa')
      flash[:notice] = l('twofa_code_sent')
    end
    redirect_to 
  end
end