Skip to content

Commit

Permalink
Integrate Odoo users as action delegator method
Browse files Browse the repository at this point in the history
  • Loading branch information
fblupi committed Mar 6, 2024
1 parent d179144 commit 8d80e16
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 32 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ gem "faker", "~> 2.14"

gem "wicked_pdf", "~> 2.1"

gem "deface"

group :development, :test do
gem "byebug", "~> 11.0", platform: :mri

Expand Down
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,7 @@ DEPENDENCIES
decidim-dev (= 0.27.4)
decidim-odoo (~> 0.2.1)
decidim-term_customizer!
deface
faker (~> 2.14)
figaro (~> 1.2)
letter_opener_web (~> 2.0)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# frozen_string_literal: true

module Decidim
module ActionDelegator
module Verifications
module DelegationsVerifier
module AuthorizationsControllerOverride
extend ActiveSupport::Concern

included do
def new
@authorization.destroy! if authorization&.persisted? && !authorization&.granted?

enforce_permission_to :create, :authorization, authorization: authorization
@form = form(verifier_form).instance(setting: setting)
participant = @form&.participant

return unless ActionDelegator.authorize_on_login && setting&.verify_with_email?

Decidim::Verifications::PerformAuthorizationStep.call(authorization, @form) do
on(:ok) do
grant_and_redirect(participant)
end
on(:invalid) do
render :new
end
end
end

def create
enforce_permission_to :create, :authorization, authorization: authorization

@form = form(verifier_form).from_params(form_params, setting: setting)
participant = @form&.participant

Decidim::Verifications::PerformAuthorizationStep.call(authorization, @form) do
on(:ok) do
if setting.phone_required?
flash[:notice] = t("authorizations.create.success", scope: "decidim.verifications.sms")
authorization_method = Decidim::Verifications::Adapter.from_element(authorization.name)
redirect_to authorization_method.resume_authorization_path(redirect_url: redirect_url)
else
grant_and_redirect(participant)
end
end
on(:invalid) do
flash.now[:alert] = t("authorizations.create.error", scope: "decidim.verifications.sms")
render :new
end
end
end

def grant_and_redirect(participant)
authorization.grant!
if participant
participant.update!(decidim_user: authorization.user)
flash[:notice] = t("authorizations.update.success", scope: "decidim.verifications.sms")
end
redirect
end

def verifier_form
setting&.odoo_required? ? Decidim::Odoo::Verifications::OdooMember : DelegationsVerifierForm
end

def form_params
params.merge(user: current_user)
end
end
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Decidim
module Odoo
module Verifications
module OdooMemberOverride
extend ActiveSupport::Concern

included do
def participant
nil
end

# currently, we rely on the last setting.
# This could be improved by allowing the user to select the setting (or related phone).
def setting
@setting ||= context[:setting]
end
end
end
end
end
end
20 changes: 20 additions & 0 deletions app/models/concerns/decidim/action_delegator/setting_override.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Decidim
module ActionDelegator
module SettingOverride
extend ActiveSupport::Concern
included do
enum authorization_method: { phone: 0, email: 1, both: 2, odoo: 3 }

def odoo_required?
verify_with_odoo?
end

def verify_with_odoo?
authorization_method == "odoo"
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- replace 'erb[loud]:contains("decidim.action_delegator.admin.settings.authorization_method")' -->

<%= form.select :authorization_method, [:both, :phone, :email, :odoo].map { |op| [t(op, scope: "decidim.action_delegator.admin.settings.authorization_method"), op] } %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Decidim
module ActionDelegator
module Verifications
module DelegationsAuthorizerOverride
extend ActiveSupport::Concern

included do
def authorize
status = super
return status unless status == [:ok, {}]

# if used outside a consultation, allow all
return [:ok, {}] if consultation.blank?
return [:ok, {}] if setting.odoo_required?
return [:ok, {}] if belongs_to_consultation? && user_in_census?

[:unauthorized, { extra_explanation: extra_explanations }]
end
end
end
end
end
end
10 changes: 10 additions & 0 deletions config/initializers/decidim_overrides.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

Rails.application.config.to_prepare do
Decidim::ActionDelegator::Verifications::DelegationsVerifier::AuthorizationsController.include(
Decidim::ActionDelegator::Verifications::DelegationsVerifier::AuthorizationsControllerOverride
)
Decidim::ActionDelegator::Setting.include(Decidim::ActionDelegator::SettingOverride)
Decidim::ActionDelegator::Verifications::DelegationsAuthorizer.include(Decidim::ActionDelegator::Verifications::DelegationsAuthorizerOverride)
Decidim::Odoo::Verifications::OdooMember.include(Decidim::Odoo::Verifications::OdooMemberOverride)
end
8 changes: 8 additions & 0 deletions config/locales/ca.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
ca:
decidim:
action_delegator:
admin:
settings:
authorization_method:
odoo: Només membres d'Odoo
39 changes: 7 additions & 32 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
# Files in the config/locales directory are used for internationalization
# and are automatically loaded by Rails. If you want to use locales other
# than English, add the necessary files in this directory.
#
# To use the locales, use `I18n.t`:
#
# I18n.t 'hello'
#
# In views, this is aliased to just `t`:
#
# <%= t('hello') %>
#
# To use a different locale, set it with `I18n.locale`:
#
# I18n.locale = :es
#
# This would use the information in config/locales/es.yml.
#
# The following keys must be escaped otherwise they will not be retrieved by
# the default I18n backend:
#
# true, false, on, off, yes, no
#
# Instead, surround them with single quotes.
#
# en:
# 'true': 'foo'
#
# To learn more, please read the Rails Internationalization guide
# available at https://guides.rubyonrails.org/i18n.html.

---
en:
hello: "Hello world"
decidim:
action_delegator:
admin:
settings:
authorization_method:
odoo: Only Odoo members
8 changes: 8 additions & 0 deletions config/locales/es.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
es:
decidim:
action_delegator:
admin:
settings:
authorization_method:
odoo: Solo miembros de Odoo
15 changes: 15 additions & 0 deletions spec/lib/overrides_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@
files: {
"/app/views/layouts/decidim/_head_extra.html.erb" => "1b8237357754cf519f4e418135f78440"
}
},
{
package: "decidim-action_delegator",
files: {
"/app/views/decidim/action_delegator/admin/settings/_form.html.erb" => "900447a24b3914d56de6ed53185f73d6",
"/app/controllers/decidim/action_delegator/verifications/delegations_verifier/authorizations_controller.rb" => "39257248025a42c3b1b8870ed31d3721",
"/app/models/decidim/action_delegator/setting.rb" => "242e32c4027fd47fd72e677db75da2f2",
"/lib/decidim/action_delegator/verifications/delegations_authorizer.rb" => "81879b0c3efeb0e0747d5d4e69c686c1"
}
},
{
package: "decidim-odoo",
files: {
"/app/forms/decidim/odoo/verifications/odoo_member.rb" => "c684eb68ca7582c150691250e99fa483"
}
}
]

Expand Down

0 comments on commit 8d80e16

Please sign in to comment.