Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

override omniauth register #22

Merged
merged 11 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions app/overrides/omniauth_registrations_controller_override.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

# A command with all the business logic to create a user from omniauth
module OmniauthRegistrationsControllerOverride
extend ActiveSupport::Concern

included do
before_action :ensure_user_exists, only: :google_oauth2

def ensure_user_exists
form_params = user_params_from_oauth_hash || params[:user]
@form = form(Decidim::OmniauthRegistrationForm).from_params(form_params)
email = @form.email || verified_email

return if email.present? && Decidim::User.exists?(email: email, organization: current_organization)

# No registration allowed
Rails.logger.info "WARNING: Attempt to register via OAuth: #{email}"
flash[:alert] = I18n.t("email_not_registered", email: email)
redirect_to decidim.root_path
end
end
end
3 changes: 3 additions & 0 deletions config/initializers/omniauth_blocker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

Decidim::Devise::OmniauthRegistrationsController.include(OmniauthRegistrationsControllerOverride)
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
en:
en:
email_not_registered: Sorry, the email %{email} is not registered. Only existing users are allowed to login.
3 changes: 3 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
fr:
email_not_registered: Désolé, l'email %{email} n'est pas enregistré. Seuls les utilisateurs existants sont autorisés à se connecter.
5 changes: 5 additions & 0 deletions config/secrets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ development:
developer:
enabled: true
icon: phone
google_oauth2:
enabled: true
icon_path: decidim/brands/google.svg
client_id: <%= ENV["OMNIAUTH_GOOGLE_CLIENT_ID"] %>
client_secret: <%= ENV["OMNIAUTH_GOOGLE_CLIENT_SECRET"] %>

test:
<<: *default
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/overrides_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
{
package: "decidim-core",
files: {
# controllers
"/app/controllers/decidim/devise/omniauth_registrations_controller.rb" => "05bc35af4b5f855736f14efbd22e439b",
# layouts
"/app/views/layouts/decidim/_head_extra.html.erb" => "1b8237357754cf519f4e418135f78440",
# cells
Expand Down