Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

472 lines
14 KiB
Ruby
Raw Permalink Normal View History

2025-07-18 17:36:37 +01:00
# frozen_string_literal: true
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2011-05-30 20:52:25 +02:00
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
2011-05-30 20:52:25 +02:00
#
2013-09-16 17:59:31 +02:00
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
2021-01-13 17:47:45 +01:00
# Copyright (C) 2006-2013 Jean-Philippe Lang
2013-09-16 17:59:31 +02:00
# Copyright (C) 2010-2013 the ChiliProject Team
#
# 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.
#
# See COPYRIGHT and LICENSE files for more details.
#++
class AccountController < ApplicationController
2011-05-30 20:52:25 +02:00
include CustomFieldsHelper
include OmniauthHelper
2024-08-29 12:44:02 +02:00
include Accounts::Registration
2020-02-11 08:04:46 +01:00
include Accounts::UserConsent
include Accounts::UserLimits
include Accounts::UserLogin
2020-02-11 08:04:46 +01:00
include Accounts::UserPasswordChange
2011-05-30 20:52:25 +02:00
# prevents login action to be filtered by check_if_login_required application scope filter
2016-09-06 15:40:49 +02:00
skip_before_action :check_if_login_required
no_authorization_required! :login,
:internal_login,
:logout,
:lost_password,
:register,
:activate,
:consent,
:confirm_consent,
:decline_consent,
:stage_success,
:stage_failure,
:change_password,
:auth_source_sso_failed
before_action :apply_csp_appends, only: %i[login]
2016-09-06 15:40:49 +02:00
before_action :disable_api
2017-08-30 15:17:02 +01:00
before_action :check_auth_source_sso_failure, only: :auth_source_sso_failed
before_action :check_internal_login_enabled, only: :internal_login
2017-08-30 15:17:02 +01:00
layout "no_menu"
# Login request and validation
def login
user = User.current
if user.logged?
2018-02-13 07:52:58 +01:00
redirect_after_login(user)
elsif request.get? && omniauth_direct_login?
direct_login(user)
2014-07-15 15:32:38 +01:00
elsif request.post?
authenticate_user
end
end
2023-04-25 09:14:52 +02:00
def internal_login
render "account/login"
2023-04-25 09:14:52 +02:00
end
# Log out current user and redirect to welcome page
def logout
2020-05-11 11:05:33 +02:00
# Keep attributes from the session
# to identify the user
previous_session = session.to_h.with_indifferent_access
2020-06-11 15:11:01 +02:00
previous_user = current_user
2010-03-04 05:33:49 +00:00
logout_user
2020-05-11 11:05:33 +02:00
2020-06-11 15:11:01 +02:00
perform_post_logout previous_session, previous_user
end
2011-05-30 20:52:25 +02:00
# Enable user to choose a new password
2025-12-08 16:18:18 +01:00
def lost_password # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
return redirect_to(home_url, status: :see_other) unless allow_lost_password_recovery?
if params[:token]
@token = ::Token::Recovery.find_by_plaintext_value(params[:token])
2025-12-08 16:18:18 +01:00
redirect_to(home_url, status: :see_other) && return unless @token and !@token.expired?
2021-02-11 16:02:18 +01:00
@user = @token.user
if request.post?
call = ::Users::ChangePasswordService.new(current_user: @user, session:).call(params)
call.apply_flash_message!(flash) if call.errors.empty?
if call.success?
@token.destroy
2025-12-08 16:18:18 +01:00
redirect_to action: "login", status: :see_other
return
2011-05-30 20:52:25 +02:00
end
end
render template: "account/password_recovery"
2018-02-13 07:52:58 +01:00
elsif request.post?
mail = params[:mail]
user = User.find_by_mail(mail) if mail.present?
2018-02-13 07:52:58 +01:00
# Ensure the same request is sent regardless of which email is entered
# to avoid detecability of mails
2020-09-16 11:26:15 +02:00
flash[:notice] = I18n.t(:notice_account_lost_email_sent)
2018-02-13 07:52:58 +01:00
unless user
# user not found in db
Rails.logger.error "Lost password unknown email input: #{mail}"
2025-12-08 16:18:18 +01:00
redirect_to action: :lost_password, status: :see_other
2018-02-13 07:52:58 +01:00
return
end
2018-02-13 07:52:58 +01:00
unless user.change_password_allowed?
# user uses an external authentication
2023-08-17 08:14:56 +02:00
UserMailer.password_change_not_possible(user).deliver_later
Rails.logger.warn "Password cannot be changed for user: #{mail}"
2025-12-08 16:18:18 +01:00
redirect_to action: :lost_password, status: :see_other
2018-02-13 07:52:58 +01:00
return
end
2018-02-13 07:52:58 +01:00
# create a new token for password recovery
token = Token::Recovery.new(user_id: user.id)
if token.save
UserMailer.password_lost(token).deliver_later
2020-09-16 11:26:15 +02:00
flash[:notice] = I18n.t(:notice_account_lost_email_sent)
2025-12-08 16:18:18 +01:00
redirect_to action: :lost_password, status: :see_other
2021-02-11 16:02:18 +01:00
nil
end
end
end
2011-05-30 20:52:25 +02:00
# User self-registration
def register
return self_registration_disabled unless allow_registration?
@user = invited_user
if request.get?
registration_through_invitation!
else
if Setting.email_login?
params[:user][:login] = params[:user][:mail]
end
self_registration!
call_hook :user_registered, { user: @user } if @user.persisted?
end
end
2011-05-30 20:52:25 +02:00
# Token based account activation
def activate
2017-11-07 10:00:32 +01:00
token = ::Token::Invitation.find_by_plaintext_value(params[:token])
2019-09-03 11:37:07 +02:00
if token.nil? || token.user.nil?
invalid_token_and_redirect
elsif token.expired?
handle_expired_token token
elsif token.user.invited?
2017-11-07 10:00:32 +01:00
activate_by_invite_token token
elsif Setting::SelfRegistration.enabled?
activate_self_registered token
else
2019-09-03 11:37:07 +02:00
invalid_token_and_redirect
end
end
# Process a password change form, used when the user is forced
# to change the password.
# When making changes here, also check MyController.change_password
def change_password
# Retrieve user login name from session
@user = User.find_by!(login: params[:password_change_user])
change_password_flow(user: @user, params:, show_user_name: true) do
password_authentication(@user.login, params[:new_password])
end
rescue ActiveRecord::RecordNotFound
Rails.logger.error "Failed to find user for change_password request: #{flash[:_password_change_user]}"
render_404
end
def auth_source_sso_failed
failure = session.delete :auth_source_sso_failure
user = auth_source_sso_failure_user(failure)
if user.try(:new_record?)
return onthefly_creation_failed user, login: user.login, ldap_auth_source_id: user.ldap_auth_source_id
end
show_sso_error_for(user, failure)
render action: "login", back_url: failure[:back_url]
end
private
def handle_expired_token(token)
2020-09-10 15:47:05 +02:00
send_activation_email! token.user
2019-09-03 11:37:07 +02:00
flash[:warning] = I18n.t :warning_registration_token_expired, email: token.user.mail
redirect_to home_url
end
2020-09-10 15:47:05 +02:00
def send_activation_email!(user)
new_token = Token::Invitation.create!(user:)
UserMailer.user_signed_up(new_token).deliver_later
end
def activate_self_registered(token)
return if enforce_activation_user_limit(user: token.user)
2018-06-06 10:39:04 +01:00
user = token.user
if user.registered?
user.activate
2015-09-02 17:28:13 +01:00
if user.save
token.destroy
flash[:notice] = I18n.t(:notice_account_activated)
2015-09-02 17:28:13 +01:00
else
flash[:error] = I18n.t(:notice_activation_failed)
end
2024-01-04 17:01:17 +01:00
elsif user.active?
flash[:notice] = I18n.t(:notice_account_already_activated)
else
2024-01-04 17:01:17 +01:00
flash[:error] = I18n.t(:notice_activation_failed)
end
redirect_to signin_path(back_url: params[:back_url])
end
def activate_by_invite_token(token)
2019-09-03 11:37:07 +02:00
return if enforce_activation_user_limit(user: token.user)
2018-06-06 10:39:04 +01:00
2019-09-03 11:37:07 +02:00
activate_invited token
2015-09-11 15:33:34 +01:00
end
2015-09-02 17:28:13 +01:00
2015-09-11 15:33:34 +01:00
def activate_invited(token)
session[:invitation_token] = token.value
session[:back_url] = params[:back_url]
2015-09-11 15:33:34 +01:00
user = token.user
2015-09-02 17:28:13 +01:00
2023-07-26 13:22:28 +02:00
if user.ldap_auth_source
activate_through_ldap user
2015-09-11 15:33:34 +01:00
else
activate_user user
end
end
2015-09-02 17:28:13 +01:00
2015-09-11 15:33:34 +01:00
def activate_user(user)
if omniauth_direct_login?
2015-09-11 15:33:34 +01:00
direct_login user
elsif OpenProject::Configuration.disable_password_login?
flash[:notice] = I18n.t("account.omniauth_login")
2015-09-11 15:33:34 +01:00
redirect_to signin_path
else
redirect_to account_register_path
end
end
2011-05-30 20:52:25 +02:00
def activate_through_ldap(user)
2015-09-11 15:33:34 +01:00
session[:auth_source_registration] = {
login: user.login,
2023-07-26 08:17:18 +02:00
ldap_auth_source_id: user.ldap_auth_source_id
2015-09-11 15:33:34 +01:00
}
2026-02-18 07:47:11 +01:00
flash[:notice] = helpers.t("account.auth_source_login_html", login: user.login)
2015-09-11 15:33:34 +01:00
redirect_to signin_path(username: user.login)
end
2011-05-30 20:52:25 +02:00
def allow_registration?
allow = Setting::SelfRegistration.enabled? && !OpenProject::Configuration.disable_password_login?
2017-08-30 15:17:02 +01:00
invited = session[:invitation_token].present?
get = request.get? && allow
post = (request.post? || request.patch?) && (session[:auth_source_registration].present? || allow)
2017-08-30 15:17:02 +01:00
invited || get || post
2017-08-30 15:17:02 +01:00
end
def allow_lost_password_recovery?
Setting.lost_password? && !OpenProject::Configuration.disable_password_login?
end
2011-05-30 20:52:25 +02:00
2017-08-30 15:17:02 +01:00
def check_auth_source_sso_failure
redirect_to home_url unless session[:auth_source_sso_failure].present?
end
def show_sso_error_for(user, failure)
2017-08-30 15:17:02 +01:00
if user.nil?
flash_and_log_invalid_credentials
2017-08-30 15:17:02 +01:00
elsif not user.active?
account_inactive user, flash_now: true
end
flash.now[:error] = "#{I18n.t(:error_auth_source_sso_failed, value: failure[:login])}: #{String(flash.now[:error])}"
2017-08-30 15:17:02 +01:00
end
def registration_through_invitation!
session[:auth_source_registration] = nil
if @user.nil?
@user = assign_user_attributes({ language: Setting.default_language })
elsif user_with_placeholder_name?(@user)
@user.firstname = nil
@user.lastname = nil
end
end
def self_registration!
@user = assign_user_attributes({ admin: false, status: User.statuses[:registered] }) if @user.nil?
2018-06-22 13:02:27 +01:00
return if enforce_activation_user_limit(user: user_with_email(@user))
2018-05-25 13:52:48 +02:00
# Set consent if received from registration form
if consent_param?
@user.consented_at = DateTime.now
end
if session[:auth_source_registration].present?
2020-09-10 15:47:05 +02:00
register_with_auth_source(@user)
else
2020-09-10 15:47:05 +02:00
register_plain_user(@user)
end
end
2017-09-07 10:16:40 +02:00
def assign_user_attributes(attrs)
Users::SetAttributesService
.new(model: User.new, user: current_user, contract_class: EmptyContract)
.call(attrs)
.result
end
def user_with_placeholder_name?(user)
user.firstname == user.login and user.login == user.mail
end
def direct_login(user)
if !flash_message_pending?
2026-05-04 15:47:50 +02:00
ps = {}
ps[:origin] = params[:back_url] if params[:back_url]
redirect_to direct_login_provider_url(ps)
2019-09-03 11:37:07 +02:00
elsif Setting.login_required?
# I'm not sure why it is considered an error if we don't have the anonymous user here.
# Before the line read `user.active? || flash[:error]` but since a recent
# change the anonymous user is active too which breaks this.
error = !user.anonymous? || flash[:error]
2019-09-03 11:37:07 +02:00
instructions = error ? :after_error : :after_registration
2019-09-03 11:37:07 +02:00
render :exit, locals: { instructions: }
end
end
2010-03-04 05:33:54 +00:00
def authenticate_user
if OpenProject::Configuration.disable_password_login?
render_404
else
password_authentication(params[:username]&.strip, params[:password])
end
2010-03-04 05:33:54 +00:00
end
2024-11-05 06:46:17 +01:00
def password_authentication(username, password) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
2015-09-02 17:28:13 +01:00
user = User.try_to_login(username, password, session)
2009-02-11 19:07:07 +00:00
if user.nil?
2013-07-22 14:31:12 +02:00
# login failed, now try to find out why and do the appropriate thing
user = User.find_by_login(username)
if user and user.check_password?(password)
2013-07-22 14:31:12 +02:00
# correct password
if not user.active?
account_inactive(user, flash_now: true)
2024-10-11 09:12:30 +02:00
render status: :unprocessable_entity
elsif user.force_password_change
return if redirect_if_password_change_not_allowed(user)
2021-02-11 16:02:18 +01:00
render_password_change(user, I18n.t(:notice_account_new_password_forced), show_user_name: true)
2013-07-22 14:31:12 +02:00
elsif user.password_expired?
return if redirect_if_password_change_not_allowed(user)
2021-02-11 16:02:18 +01:00
render_password_change(user, I18n.t(:notice_account_password_expired, days: Setting.password_days_valid.to_i),
show_user_name: true)
else
flash_and_log_invalid_credentials
2024-09-13 09:21:17 +02:00
render status: :unprocessable_entity
end
elsif user and user.invited?
invited_account_not_activated(user)
2024-09-13 09:21:17 +02:00
render status: :unprocessable_entity
else
2013-07-22 14:31:12 +02:00
# incorrect password
flash_and_log_invalid_credentials
2024-09-13 09:21:17 +02:00
render status: :unprocessable_entity
end
2009-02-11 19:07:07 +00:00
elsif user.new_record?
2023-07-26 08:17:18 +02:00
onthefly_creation_failed(user, login: user.login, ldap_auth_source_id: user.ldap_auth_source_id)
2009-02-11 19:07:07 +00:00
else
# Valid user
successful_authentication(user)
end
end
2021-02-11 16:02:18 +01:00
def invited_account_not_activated(_user)
2018-02-13 07:52:58 +01:00
flash_error_message(log_reason: "invited, NOT ACTIVATED", flash_now: false) do
"account.error_inactive_activation_by_mail"
end
end
def invited_user
if session.include? :invitation_token
2017-11-07 10:00:32 +01:00
token = Token::Invitation.find_by_plaintext_value session[:invitation_token]
token.user
end
end
2019-08-20 08:09:55 +02:00
def disable_api
# Changing this to not use api_request? to determine whether a request is an API
# request can have security implications regarding CSRF. See handle_unverified_request
# for more information.
head :gone if api_request?
end
2019-09-03 11:37:07 +02:00
def invalid_token_and_redirect
flash[:error] = I18n.t(:notice_account_invalid_token)
redirect_to signin_path
2019-09-03 11:37:07 +02:00
end
def flash_message_pending?
# confirm that the flash contains a message to be rendered, but ignore
# other flash content (such as _csp_appends)
flash.keys.map(&:to_s).intersect?(%w[notice warning error])
end
def apply_csp_appends
appends = flash[:_csp_appends]
return unless appends
append_content_security_policy_directives(appends)
end
def check_internal_login_enabled
render_404 unless omniauth_direct_login?
end
def auth_source_sso_failure_user(failure)
login = failure[:login]
find_user_from_auth_source(login) || build_user_from_auth_source(login)
end
2006-06-28 18:11:03 +00:00
end