Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add separate controller for OmniAuth

Oliver Günther15dbe4b85439
diff --git a/app/controllers/account_controller.rb b/app/controllers/account_controller.rb
index bf66aca9349..e1fa97a0492 100644
--- a/app/controllers/account_controller.rb
+++ b/app/controllers/account_controller.rb
@@ -29,7 +29,7 @@
class AccountController < ApplicationController
include CustomFieldsHelper
include OmniauthHelper
- include Accounts::OmniauthLogin
+ include Accounts::Registration
include Accounts::UserConsent
include Accounts::UserLimits
include Accounts::UserPasswordChange
@@ -346,49 +346,6 @@ class AccountController < ApplicationController
.result
end
- def register_plain_user(user)
- user.attributes = permitted_params.user.transform_values do |val|
- if val.is_a? String
- val.strip!
- end
-
- val
- end
- user.login = params[:user][:login].strip if params[:user][:login].present?
- user.password = params[:user][:password]
- user.password_confirmation = params[:user][:password_confirmation]
-
- respond_for_registered_user(user)
- end
-
- def register_with_auth_source(user)
- # on-the-fly registration via omniauth or via auth source
- if pending_omniauth_registration?
- user.assign_attributes permitted_params.user_register_via_omniauth
- register_via_omniauth(session, user.attributes)
- else
- user.attributes = permitted_params.user
- user.activate
- user.login = session[:auth_source_registration][:login]
- user.ldap_auth_source_id = session[:auth_source_registration][:ldap_auth_source_id]
-
- respond_for_registered_user(user)
- end
- end
-
- def respond_for_registered_user(user)
- call = ::Users::RegisterUserService.new(user).call
-
- if call.success?
- flash[:notice] = call.message.presence
- login_user_if_active(call.result, just_registered: true)
- else
- flash[:error] = error = call.message
- Rails.logger.error "Registration of user #{user.login} failed: #{error}"
- onthefly_creation_failed(user)
- end
- end
-
def user_with_placeholder_name?(user)
user.firstname == user.login and user.login == user.mail
end
@@ -454,61 +411,6 @@ class AccountController < ApplicationController
end
end
- def login_user_if_active(user, just_registered:)
- if user.active?
- successful_authentication(user, just_registered:)
- return
- end
-
- # Show an appropriate error unless
- # the user was just registered
- if !(just_registered && user.registered?)
- account_inactive(user, flash_now: false)
- end
-
- redirect_to signin_path(back_url: params[:back_url])
- end
-
- def pending_auth_source_registration?
- session[:auth_source_registration] && !pending_omniauth_registration?
- end
-
- def pending_omniauth_registration?
- Hash(session[:auth_source_registration])[:omniauth]
- end
-
- # 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
-
- def self_registration_disabled
- flash[:error] = I18n.t("account.error_self_registration_disabled")
- redirect_to signin_url
- end
-
- # Call if an account is inactive - either registered or locked
- def account_inactive(user, flash_now: true)
- if user.registered?
- account_not_activated(flash_now:)
- else
- flash_and_log_invalid_credentials(flash_now:)
- end
- end
-
- # Log an attempt to log in to an account in "registered" state and show a flash message.
- def account_not_activated(flash_now: true)
- flash_error_message(log_reason: "NOT ACTIVATED", flash_now:) do
- if Setting::SelfRegistration.by_email?
- "account.error_inactive_activation_by_mail"
- else
- "account.error_inactive_manual_activation"
- end
- end
- end
-
def invited_account_not_activated(_user)
flash_error_message(log_reason: "invited, NOT ACTIVATED", flash_now: false) do
"account.error_inactive_activation_by_mail"
diff --git a/app/controllers/concerns/accounts/registration.rb b/app/controllers/concerns/accounts/registration.rb
new file mode 100644
index 00000000000..7c52a08c6e5
--- /dev/null
+++ b/app/controllers/concerns/accounts/registration.rb
@@ -0,0 +1,129 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# 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.
+#++
+
+require "uri"
+
+##
+# Intended to be used by the AccountController and OmniAuthLoginController to handle registration flows
+module Accounts::Registration
+ ##
+ # Sends a user who was just registered to the activation stages
+ # or to the signin page if the user could not be activated
+ def login_user_if_active(user, just_registered:)
+ if user.active?
+ successful_authentication(user, just_registered:)
+ return
+ end
+
+ # Show an appropriate error unless
+ # the user was just registered
+ if !(just_registered && user.registered?)
+ account_inactive(user, flash_now: false)
+ end
+
+ redirect_to signin_path(back_url: params[:back_url])
+ end
+
+ def register_plain_user(user)
+ user.attributes = permitted_params.user.transform_values do |val|
+ if val.is_a? String
+ val.strip!
+ end
+
+ val
+ end
+ user.login = params[:user][:login].strip if params[:user][:login].present?
+ user.password = params[:user][:password]
+ user.password_confirmation = params[:user][:password_confirmation]
+
+ respond_for_registered_user(user)
+ end
+
+ def register_with_auth_source(user)
+ # on-the-fly registration via omniauth or via auth source
+ if pending_omniauth_registration?
+ user.assign_attributes permitted_params.user_register_via_omniauth
+ register_via_omniauth(session, user.attributes)
+ else
+ user.attributes = permitted_params.user
+ user.activate
+ user.login = session[:auth_source_registration][:login]
+ user.ldap_auth_source_id = session[:auth_source_registration][:ldap_auth_source_id]
+
+ respond_for_registered_user(user)
+ end
+ end
+
+ def respond_for_registered_user(user)
+ call = ::Users::RegisterUserService.new(user).call
+
+ if call.success?
+ flash[:notice] = call.message.presence
+ login_user_if_active(call.result, just_registered: true)
+ else
+ flash[:error] = error = call.message
+ Rails.logger.error "Registration of user #{user.login} failed: #{error}"
+ onthefly_creation_failed(user)
+ end
+ end
+
+ # 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
+
+ def self_registration_disabled
+ flash[:error] = I18n.t("account.error_self_registration_disabled")
+ redirect_to signin_url
+ end
+
+ def account_inactive(user, flash_now: true)
+ if user.registered?
+ account_not_activated(flash_now:)
+ else
+ flash_and_log_invalid_credentials(flash_now:)
+ end
+ end
+
+ def pending_omniauth_registration?
+ Hash(session[:auth_source_registration])[:omniauth]
+ end
+
+ # Log an attempt to log in to an account in "registered" state and show a flash message.
+ def account_not_activated(flash_now: true)
+ flash_error_message(log_reason: "NOT ACTIVATED", flash_now:) do
+ if Setting::SelfRegistration.by_email?
+ "account.error_inactive_activation_by_mail"
+ else
+ "account.error_inactive_manual_activation"
+ end
+ end
+ end
+end
diff --git a/app/controllers/concerns/accounts/omniauth_login.rb b/app/controllers/omni_auth_login_controller.rb
similarity index 76%
rename from app/controllers/concerns/accounts/omniauth_login.rb
rename to app/controllers/omni_auth_login_controller.rb
index 93f36f80cc8..cb2eaf7e68e 100644
--- a/app/controllers/concerns/accounts/omniauth_login.rb
+++ b/app/controllers/omni_auth_login_controller.rb
@@ -26,30 +26,26 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-require "uri"
-
-##
-# Intended to be used by the AccountController to handle omniauth logins
-module Accounts::OmniauthLogin
- extend ActiveSupport::Concern
-
- included do
- # disable CSRF protection since that should be covered by the omniauth strategy
- # the other filters are not applicable either since OmniAuth is doing authentication
- # itself
- %i[
- verify_authenticity_token
- user_setup
- check_if_login_required
- check_session_lifetime
- ]
- .each { |key| skip_before_action key, only: [:omniauth_login] }
- no_authorization_required! :omniauth_login, :omniauth_failure
-
- helper :omniauth
- end
+class OmniAuthLoginController < ApplicationController
+ include OmniauthHelper
+ include Accounts::Registration
+
+ # disable CSRF protection since that should be covered by the omniauth strategy
+ # the other filters are not applicable either since OmniAuth is doing authentication
+ # itself
+
+ skip_before_action :verify_authenticity_token
+ skip_before_action :user_setup
+ skip_before_action :check_if_login_required
+ skip_before_action :check_session_lifetime
+
+ no_authorization_required! :login, :failure
+
+ helper :omniauth
- def omniauth_login
+ layout "no_menu"
+
+ def login
params[:back_url] = omniauth_back_url if remember_back_url?
# Extract auth info and perform check / login or activate user
@@ -57,28 +53,24 @@ module Accounts::OmniauthLogin
handle_omniauth_authentication(auth_hash)
end
- def omniauth_failure
+ def failure
logger.warn(params[:message]) if params[:message]
show_error I18n.t(:error_external_authentication_failed)
end
- def direct_login_provider_url(params = {})
- omniauth_start_url(direct_login_provider, params)
- end
-
private
def redirect_omniauth_register_modal(user, auth_hash)
# Store a timestamp so we can later make sure that authentication information can
# only be reused for a short time.
- session_info = auth_hash.merge(omniauth: true, timestamp: Time.new)
-
- onthefly_creation_failed(user, session_info)
+ session[:auth_source_registration] = auth_hash.merge(omniauth: true, timestamp: Time.current)
+ @user = user
+ render template: "/account/register"
end
# Avoid remembering the back_url if we're coming from the login page
def remember_back_url?
- return if omniauth_back_url.blank?
+ return false if omniauth_back_url.blank?
account_routes = /\/(login|account)/
omniauth_direct_login? || !omniauth_back_url.match?(account_routes)
@@ -92,14 +84,14 @@ module Accounts::OmniauthLogin
def show_error(error)
flash[:error] = error
- redirect_to action: "login"
+ redirect_to action: :login
end
def register_via_omniauth(session, user_attributes)
handle_omniauth_authentication(session[:auth_source_registration], user_params: user_attributes)
end
- def handle_omniauth_authentication(auth_hash, user_params: nil)
+ def handle_omniauth_authentication(auth_hash, user_params: nil) # rubocop:disable Metrics/AbcSize
call = ::Authentication::OmniauthService
.new(strategy: request.env["omniauth.strategy"], auth_hash:, controller: self)
.call(user_params)
diff --git a/app/helpers/omniauth_helper.rb b/app/helpers/omniauth_helper.rb
index f716e973de8..57d804991a5 100644
--- a/app/helpers/omniauth_helper.rb
+++ b/app/helpers/omniauth_helper.rb
@@ -31,6 +31,10 @@ module OmniauthHelper
direct_login_provider.is_a? String
end
+ def direct_login_provider_url(params = {})
+ omniauth_start_url(direct_login_provider, params)
+ end
+
##
# Per default the user may choose the usual password login as well as several omniauth providers
# on the login page and in the login drop down menu.
diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb
index 21ac2dd65d9..89bf62e8158 100644
--- a/config/initializers/omniauth.rb
+++ b/config/initializers/omniauth.rb
@@ -28,6 +28,10 @@
OmniAuth.config.logger = Rails.logger
+OmniAuth.config.on_failure = Proc.new do |env|
+ OmniAuthLoginController.action(:failure).call(env)
+end
+
Rails.application.config.middleware.use OmniAuth::Builder do
unless Rails.env.production?
provider :developer, fields: %i[first_name last_name email]
diff --git a/config/routes.rb b/config/routes.rb
index 4f095390345..ad69b10fc0b 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -63,9 +63,9 @@ Rails.application.routes.draw do
# Add catch method for Rack OmniAuth to allow route helpers
# Note: This renders a 404 in rails but is caught by omniauth in Rack before
- get "/auth/failure", to: "account#omniauth_failure"
+ get "/auth/failure", to: "account#failure"
get "/auth/:provider", to: proc { [404, {}, [""]] }, as: "omniauth_start"
- match "/auth/:provider/callback", to: "account#omniauth_login", as: "omniauth_login", via: %i[get post]
+ match "/auth/:provider/callback", to: "omni_auth_login#callback", as: "omniauth_callback", via: %i[get post]
# In case assets are actually delivered by a node server (e.g. in test env)
# forward requests to the proxy