generated from OpenSourcePolitics/decidim-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Automatically vote initiatives after sign up / in when the moda…
…l is used - add new request param for auto sign when the modal is activated - manage new param for standard sign in - forward and manage new param for sign up - forward and manage new param for france connect sign in - forward and manage new param for france connect sign up with tos aggreement - fix post logout redirect for france connect sign out 🔥 - add flash message to notify that the initiative was voted
- Loading branch information
Showing
16 changed files
with
289 additions
and
6 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
app/controllers/decidim/omniauth_registrations_controller_override.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module OmniauthRegistrationsControllerOverride | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include Decidim::AfterSignInActionHelper | ||
|
||
def after_sign_in_path_for(user) | ||
after_sign_in_action_for(user, request.params[:after_action]) if request.params[:after_action].present? | ||
|
||
if user.present? && user.blocked? | ||
check_user_block_status(user) | ||
elsif user.present? && !user.tos_accepted? && request.params[:after_action].present? | ||
session["tos_after_action"] = request.params[:after_action] | ||
super | ||
elsif !pending_redirect?(user) && first_login_and_not_authorized?(user) | ||
decidim_verifications.authorizations_path | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
16 changes: 16 additions & 0 deletions
16
app/controllers/decidim/registrations_controller_override.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module RegistrationsControllerOverride | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
include Decidim::AfterSignInActionHelper | ||
|
||
def after_sign_in_path_for(user) | ||
after_sign_in_action_for(user, request.params[:after_action]) if request.params[:after_action].present? | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module AfterSignInActionHelper | ||
extend ActiveSupport::Concern | ||
include Decidim::FormFactory | ||
|
||
included do | ||
def default_url_options | ||
url_options = {} | ||
url_options[:locale] = current_locale unless current_locale == default_locale.to_s | ||
url_options[:after_action] = request.params[:after_action] if request.params[:after_action].present? | ||
url_options | ||
end | ||
end | ||
|
||
def after_sign_in_action_for(user, action) | ||
return if user.blank? | ||
return unless action == "vote-initiative" && (scan = %r{/initiatives/i-(\d+)(\?.*)?}.match(read_stored_location_for(user))) | ||
|
||
initiative = Decidim::Initiative.find(scan[1]) | ||
|
||
return unless allowed_to? :vote, :initiative, initiative: initiative, user: user, chain: permission_class_chain.push(Decidim::Initiatives::Permissions) | ||
|
||
form = form(Decidim::Initiatives::VoteForm).from_params( | ||
initiative: initiative, | ||
signer: user | ||
) | ||
|
||
Decidim::Initiatives::VoteInitiative.call(form) do | ||
on(:ok) do | ||
after_action_flash_message!(:secondary, "initiative_votes.create.success", "decidim.initiatives") | ||
end | ||
|
||
on(:invalid) do | ||
after_action_flash_message!(:error, "initiative_votes.create.error", "decidim.initiatives") | ||
end | ||
end | ||
end | ||
|
||
def read_stored_location_for(resource_or_scope) | ||
store_location_for(resource_or_scope, stored_location_for(resource_or_scope)) | ||
end | ||
|
||
def after_action_flash_message!(level, key, scope) | ||
if is_a? DeviseController | ||
set_flash_message! level, key, { scope: scope } | ||
else | ||
flash.now[level] = t(key, scope: scope) | ||
end | ||
end | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
app/helpers/decidim/initiatives/initiative_helper_vote_modal_override.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Decidim | ||
module Initiatives | ||
module InitiativeHelperVoteModalOverride | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
def authorized_vote_modal_button(initiative, html_options, &block) | ||
return if current_user && action_authorized_to("vote", resource: initiative, permissions_holder: initiative.type).ok? | ||
|
||
tag = "button" | ||
html_options ||= {} | ||
|
||
html_options["data-after-action"] = "vote-initiative" | ||
|
||
if current_user | ||
html_options["data-open"] = "authorizationModal" | ||
html_options["data-open-url"] = authorization_sign_modal_initiative_path(initiative) | ||
else | ||
html_options["data-open"] = "loginModal" | ||
end | ||
|
||
html_options["onclick"] = "event.preventDefault();" | ||
|
||
send("#{tag}_to", "", html_options, &block) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* eslint-disable multiline-ternary, no-ternary */ | ||
|
||
/* | ||
* Triggered when adding data-after-action to a link or button | ||
* | ||
* This is used to add an after action after sign in. | ||
* | ||
* When a button or link trigger a login modal we capture | ||
* the event and inject the after action to be done | ||
* after sign in (the after_action param). | ||
* | ||
* The code is injected to any form or link in the modal | ||
* and when the modal is closed we remove the injected | ||
* code. | ||
* | ||
* In order for this to work the button or link must have | ||
* a data-open attribute with the ID of the modal to open | ||
* and a data-after-action attribute the action to be done. | ||
* If any of this is missing no code will be injected. | ||
*/ | ||
$(() => { | ||
const removeAfterActionParameter = (url, parameter) => { | ||
const urlParts = url.split("?"); | ||
|
||
if (urlParts.length >= 2) { | ||
// Get first part, and remove from array | ||
const urlBase = urlParts.shift(); | ||
|
||
// Join it back up | ||
const queryString = urlParts.join("?"); | ||
|
||
const prefix = `${encodeURIComponent(parameter)}=`; | ||
const parts = queryString.split(/[&;]/g); | ||
|
||
// Reverse iteration as may be destructive | ||
for (let index = parts.length - 1; index >= 0; index -= 1) { | ||
// Idiom for string.startsWith | ||
if (parts[index].lastIndexOf(prefix, 0) !== -1) { | ||
parts.splice(index, 1); | ||
} | ||
} | ||
|
||
if (parts.length === 0) { | ||
return urlBase; | ||
} | ||
|
||
return `${urlBase}?${parts.join("&")}`; | ||
} | ||
|
||
return url; | ||
} | ||
|
||
$(document).on("click.zf.trigger", (event) => { | ||
const target = `#${$(event.target).data("open")}`; | ||
const afterAction = $(event.target).data("afterAction"); | ||
|
||
if (target && afterAction) { | ||
$("<input type='hidden' />"). | ||
attr("id", "after_action"). | ||
attr("name", "after_action"). | ||
attr("value", afterAction). | ||
appendTo(`${target} form`); | ||
|
||
$(`${target} a`).attr("href", (index, href) => { | ||
const querystring = jQuery.param({"after_action": afterAction}); | ||
return href + (href.match(/\?/) ? "&" : "?") + querystring; | ||
}); | ||
} | ||
}); | ||
|
||
$(document).on("closed.zf.reveal", (event) => { | ||
$("#after_action", event.target).remove(); | ||
$("a", event.target).attr("href", (index, href) => { | ||
if (href && href.indexOf("after_action") !== -1) { | ||
return removeAfterActionParameter(href, "after_action"); | ||
} | ||
|
||
return href; | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
// This file is compiled inside Decidim core pack. Code can be added here and will be executed | ||
// as part of that pack | ||
|
||
import "src/decidim/append_after_action_to_modals" | ||
|
||
// Load images | ||
require.context("../../images", true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
lib/extends/controllers/decidim/homepage_controller_extends.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module HomepageControllerExtends | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :france_connect_after_sign_out, only: [:show] | ||
|
||
def france_connect_after_sign_out | ||
if session["omniauth.france_connect.post_logout_redirect_uri"].present? | ||
post_logout_redirect_uri = session["omniauth.france_connect.post_logout_redirect_uri"] | ||
session.delete("omniauth.france_connect.post_logout_redirect_uri") | ||
redirect_to post_logout_redirect_uri | ||
end | ||
end | ||
end | ||
end | ||
|
||
Decidim::HomepageController.class_eval do | ||
include(HomepageControllerExtends) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module FranceConnectExtends | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
private | ||
|
||
def redirect_uri | ||
"#{omniauth_callback_url}?#{params.slice("redirect_uri", "after_action").to_query}" | ||
end | ||
end | ||
end | ||
|
||
OmniAuth::Strategies::FranceConnect.class_eval do | ||
include(FranceConnectExtends) | ||
end |
Oops, something went wrong.