Skip to content

Commit

Permalink
Merge pull request consuldemocracy#3902 from consul/wordpress_login
Browse files Browse the repository at this point in the history
Enable Wordpress Oauth login and registration
  • Loading branch information
javierm committed Feb 7, 2020
2 parents 2f97828 + 3fcc3ed commit 7b9dc6c
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 0 deletions.
18 changes: 18 additions & 0 deletions app/assets/stylesheets/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,7 @@ table {
.button.button-twitter,
.button.button-facebook,
.button.button-google,
.button.button-wordpress,
.button.button-telegram {
background: #fff;
color: $text;
Expand Down Expand Up @@ -1678,6 +1679,23 @@ table {
}
}

.button.button-wordpress {
background: #dcdde3;
border-left: 3px solid #2f2f33;

&::before {
color: #2f2f33;
content: "J";
font-family: "icons" !important;
font-size: rem-calc(24);
left: 0;
line-height: $line-height * 2;
padding: 0 rem-calc(20);
position: absolute;
top: 0;
}
}

.button.button-telegram {
background: #ecf7fc;
border-left: 3px solid #08c;
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/users/omniauth_callbacks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def google_oauth2
sign_in_with :google_login, :google_oauth2
end

def wordpress_oauth2
sign_in_with :wordpress_login, :wordpress_oauth2
end

def after_sign_in_path_for(resource)
if resource.registering_with_oauth
finish_signup_path
Expand Down
1 change: 1 addition & 0 deletions app/models/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def defaults
"feature.facebook_login": true,
"feature.google_login": true,
"feature.twitter_login": true,
"feature.wordpress_login": false,
"feature.public_stats": true,
"feature.signature_sheets": true,
"feature.user.recommendations": true,
Expand Down
19 changes: 19 additions & 0 deletions app/views/devise/_omniauth_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
</div>
<% end %>
<% if feature? :wordpress_login %>
<div class="small-12 medium-6 large-4 column end">
<%= link_to t("omniauth.wordpress_oauth2.name"), user_wordpress_oauth2_omniauth_authorize_path,
title: t("omniauth.wordpress_oauth2.sign_in"),
class: "button-wordpress button expanded",
method: :post %>
</div>
<% end %>

<div class="small-12 column auth-divider">
<span><%= t("omniauth.or_fill") %></span>
</div>
Expand Down Expand Up @@ -72,6 +81,16 @@
title: t("omniauth.google_oauth2.sign_up"),
class: "button-google button expanded",
method: :post %>

</div>
<% end %>
<% if feature? :wordpress_login %>
<div class="small-12 medium-6 large-4 column end">
<%= link_to t("omniauth.wordpress_oauth2.name"), user_wordpress_oauth2_omniauth_authorize_path,
title: t("omniauth.wordpress_oauth2.sign_up"),
class: "button-wordpress button expanded",
method: :post %>
</div>
<% end %>

Expand Down
7 changes: 7 additions & 0 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require Rails.root.join("lib", "omniauth_wordpress")

# Use this hook to configure devise mailer, warden hooks and so forth.
# Many of these configuration options can be set straight in your model.
Devise.setup do |config|
Expand Down Expand Up @@ -242,6 +244,11 @@
config.omniauth :twitter, Rails.application.secrets.twitter_key, Rails.application.secrets.twitter_secret
config.omniauth :facebook, Rails.application.secrets.facebook_key, Rails.application.secrets.facebook_secret, scope: "email", info_fields: "email,name,verified"
config.omniauth :google_oauth2, Rails.application.secrets.google_oauth2_key, Rails.application.secrets.google_oauth2_secret
config.omniauth :wordpress_oauth2,
Rails.application.secrets.wordpress_oauth2_key,
Rails.application.secrets.wordpress_oauth2_secret,
strategy_class: OmniAuth::Strategies::Wordpress,
client_options: { site: Rails.application.secrets.wordpress_oauth2_site }

# ==> Warden configuration
# If you want to use other strategies, that are not supported by Devise, or
Expand Down
4 changes: 4 additions & 0 deletions config/locales/en/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ en:
sign_in: Sign in with Google
sign_up: Sign up with Google
name: Google
wordpress_oauth2:
sign_in: Sign in with Wordpress
sign_up: Sign up with Wordpress
name: Wordpress
twitter:
sign_in: Sign in with Twitter
sign_up: Sign up with Twitter
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ en:
facebook_login_description: "Allow users to sign up with their Facebook account"
google_login: "Google login"
google_login_description: "Allow users to sign up with their Google Account"
wordpress_login: "Wordpress login"
wordpress_login_description: "Allow users to sign up with their Wordpress Account"
featured_proposals: "Featured proposals"
featured_proposals_description: "Shows featured proposals on index proposals page"
signature_sheets: "Signature sheets"
Expand Down
4 changes: 4 additions & 0 deletions config/locales/es/general.yml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ es:
sign_in: Entra con Google
sign_up: Regístrate con Google
name: Google
wordpress_oauth2:
sign_in: Entra con Wordpress
sign_up: Regístrate con Wordpress
name: Wordpress
twitter:
sign_in: Entra con Twitter
sign_up: Regístrate con Twitter
Expand Down
2 changes: 2 additions & 0 deletions config/locales/es/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ es:
facebook_login_description: "Permitir que los usuarios se registren con su cuenta de Facebook"
google_login: "Registro con Google"
google_login_description: "Permitir que los usuarios se registren con su cuenta de Google"
wordpress_login: "Registro con Wordpress"
wordpress_login_description: "Permitir que los usuarios se registren con su cuenta de Wordpress"
featured_proposals: "Propuestas destacadas"
featured_proposals_description: "Muestra propuestas destacadas en la página principal de propuestas"
signature_sheets: "Hojas de firmas"
Expand Down
3 changes: 3 additions & 0 deletions config/secrets.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,8 @@ production:
facebook_secret: ""
google_oauth2_key: ""
google_oauth2_secret: ""
wordpress_oauth2_key: ""
wordpress_oauth2_secret: ""
wordpress_oauth2_site: ""
<<: *maps
<<: *apis
40 changes: 40 additions & 0 deletions lib/omniauth_wordpress.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# This code is based on this gem https://github.com/jwickard/omniauth-wordpress-oauth2-plugin

require "omniauth-oauth2"

module OmniAuth
module Strategies
class Wordpress < OmniAuth::Strategies::OAuth2
option :name, "wordpress_oauth2"

option :client_options, {}

uid { raw_info["ID"] }

info do
{
name: raw_info["display_name"],
email: raw_info["user_email"],
nickname: raw_info["user_nicename"],
urls: { "Website" => raw_info["user_url"] }
}
end

extra do
{ raw_info: raw_info }
end

def callback_url
full_host + script_name + callback_path
end

def raw_info
@raw_info ||= obtain_raw_info
end

def obtain_raw_info
access_token.get("/oauth/me", params: { "Authorization" => "Bearer #{access_token.token}" }).parsed
end
end
end
end
77 changes: 77 additions & 0 deletions spec/features/users_auth_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,83 @@
expect(page).to have_field("user_email", with: "somethingelse@example.com")
end
end

context "Wordpress" do
let(:wordpress_hash) do
{ provider: "wordpress",
uid: "12345",
info: {
name: "manuela",
email: "manuelacarmena@example.com" }}
end

before { Setting["feature.wordpress_login"] = true }

scenario "Sign up" do
OmniAuth.config.add_mock(:wordpress_oauth2, wordpress_hash)

visit "/"
click_link "Register"

click_link "Sign up with Wordpress"

expect(page).to have_current_path(new_user_session_path)
expect(page).to have_content "To continue, please click on the confirmation link that we have sent you via email"

confirm_email
expect(page).to have_content "Your account has been confirmed"

visit "/"
click_link "Sign in"
click_link "Sign in with Wordpress"
expect_to_be_signed_in

click_link "My account"
expect(page).to have_field("account_username", with: "manuela")

visit edit_user_registration_path
expect(page).to have_field("user_email", with: "manuelacarmena@example.com")
end

scenario "Try to register with username and email of an already existing user" do
create(:user, username: "manuela", email: "manuelacarmena@example.com", password: "judgementday")
OmniAuth.config.add_mock(:wordpress_oauth2, wordpress_hash)

visit "/"
click_link "Register"
click_link "Sign up with Wordpress"

expect(page).to have_current_path(finish_signup_path)

expect(page).to have_field("user_username", with: "manuela")

click_button "Register"

expect(page).to have_current_path(do_finish_signup_path)

fill_in "Username", with: "manuela2"
fill_in "Email", with: "manuela@consul.dev"
click_button "Register"

expect(page).to have_current_path(new_user_session_path)
expect(page).to have_content "To continue, please click on the confirmation link that we have sent you via email"

confirm_email
expect(page).to have_content "Your account has been confirmed"

visit "/"
click_link "Sign in"
click_link "Sign in with Wordpress"

expect_to_be_signed_in

click_link "My account"
expect(page).to have_field("account_username", with: "manuela2")

visit edit_user_registration_path
expect(page).to have_field("user_email", with: "manuela@consul.dev")
end
end
end

scenario "Sign out" do
Expand Down

0 comments on commit 7b9dc6c

Please sign in to comment.