mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Merge pull request #10554 from mkllnk/dfc-variants
Ignore master variants exporting to DFC
This commit is contained in:
@@ -42,7 +42,7 @@ module DfcProvider
|
||||
end
|
||||
|
||||
def authorization_control
|
||||
DfcProvider::AuthorizationControl.new(request)
|
||||
AuthorizationControl.new(request)
|
||||
end
|
||||
|
||||
def not_found
|
||||
|
||||
@@ -22,7 +22,7 @@ module DfcProvider
|
||||
|
||||
def variant
|
||||
@variant ||=
|
||||
DfcProvider::VariantFetcher.new(current_enterprise).scope.find(params[:id])
|
||||
VariantFetcher.new(current_enterprise).scope.find(params[:id])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -21,7 +21,7 @@ module DfcProvider
|
||||
|
||||
def variant
|
||||
@variant ||=
|
||||
DfcProvider::VariantFetcher.new(current_enterprise).scope.find(params[:id])
|
||||
VariantFetcher.new(current_enterprise).scope.find(params[:id])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,11 +32,11 @@ module DfcProvider
|
||||
end
|
||||
|
||||
def supplies
|
||||
DfcProvider::VariantFetcher.new(object).scope
|
||||
VariantFetcher.new(object).scope
|
||||
end
|
||||
|
||||
def manages
|
||||
DfcProvider::VariantFetcher.new(object).scope
|
||||
VariantFetcher.new(object).scope
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
54
engines/dfc_provider/app/services/authorization_control.rb
Normal file
54
engines/dfc_provider/app/services/authorization_control.rb
Normal file
@@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Service used to authorize the user on DCF Provider API
|
||||
# It controls an OICD Access token and an enterprise.
|
||||
class AuthorizationControl
|
||||
# Copied from: https://login.lescommuns.org/auth/realms/data-food-consortium/
|
||||
LES_COMMUNES_PUBLIC_KEY = <<~KEY
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl68JGqAILFzoi/1+6siXXp2vylu+7mPjYKjKelTtHFYXWVkbmVptCsamHlY3jRhqSQYe6M1SKfw8D+uXrrWsWficYvpdlV44Vm7uETZOr1/XBOjpWOi1vLmBVtX6jFeqN1BxfE1PxLROAiGn+MeMg90AJKShD2c5RoNv26e20dgPhshRVFPUGru+0T1RoKyIa64z/qcTcTVD2V7KX+ANMweRODdoPAzQFGGjTnL1uUqIdUwSfHSpXYnKxXOsnPC3Mowkv8UIGWWDxS/yzhWc7sOk1NmC7pb+Cg7G8NKj+Pp9qQZnXF39Dg95ZsxJrl6fyPFvTo3zf9CPG/fUM1CkkwIDAQAB
|
||||
-----END PUBLIC KEY-----
|
||||
KEY
|
||||
|
||||
def self.public_key
|
||||
OpenSSL::PKey::RSA.new(LES_COMMUNES_PUBLIC_KEY)
|
||||
end
|
||||
|
||||
def initialize(request)
|
||||
@request = request
|
||||
end
|
||||
|
||||
def user
|
||||
oidc_user || ofn_user
|
||||
rescue JWT::ExpiredSignature
|
||||
nil
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def oidc_user
|
||||
find_ofn_user(decode_token) if access_token
|
||||
end
|
||||
|
||||
def ofn_user
|
||||
@request.env['warden']&.user
|
||||
end
|
||||
|
||||
def decode_token
|
||||
JWT.decode(
|
||||
access_token,
|
||||
self.class.public_key,
|
||||
true, { algorithm: "RS256" }
|
||||
).first
|
||||
end
|
||||
|
||||
def access_token
|
||||
@request.headers['Authorization'].to_s.split(' ').last
|
||||
end
|
||||
|
||||
def find_ofn_user(payload)
|
||||
return if payload["email"].blank?
|
||||
|
||||
Spree::User.find_by(uid: payload["email"])
|
||||
end
|
||||
end
|
||||
@@ -1,56 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Service used to authorize the user on DCF Provider API
|
||||
# It controls an OICD Access token and an enterprise.
|
||||
module DfcProvider
|
||||
class AuthorizationControl
|
||||
# Copied from: https://login.lescommuns.org/auth/realms/data-food-consortium/
|
||||
LES_COMMUNES_PUBLIC_KEY = <<~KEY
|
||||
-----BEGIN PUBLIC KEY-----
|
||||
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl68JGqAILFzoi/1+6siXXp2vylu+7mPjYKjKelTtHFYXWVkbmVptCsamHlY3jRhqSQYe6M1SKfw8D+uXrrWsWficYvpdlV44Vm7uETZOr1/XBOjpWOi1vLmBVtX6jFeqN1BxfE1PxLROAiGn+MeMg90AJKShD2c5RoNv26e20dgPhshRVFPUGru+0T1RoKyIa64z/qcTcTVD2V7KX+ANMweRODdoPAzQFGGjTnL1uUqIdUwSfHSpXYnKxXOsnPC3Mowkv8UIGWWDxS/yzhWc7sOk1NmC7pb+Cg7G8NKj+Pp9qQZnXF39Dg95ZsxJrl6fyPFvTo3zf9CPG/fUM1CkkwIDAQAB
|
||||
-----END PUBLIC KEY-----
|
||||
KEY
|
||||
|
||||
def self.public_key
|
||||
OpenSSL::PKey::RSA.new(LES_COMMUNES_PUBLIC_KEY)
|
||||
end
|
||||
|
||||
def initialize(request)
|
||||
@request = request
|
||||
end
|
||||
|
||||
def user
|
||||
oidc_user || ofn_user
|
||||
rescue JWT::ExpiredSignature
|
||||
nil
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def oidc_user
|
||||
find_ofn_user(decode_token) if access_token
|
||||
end
|
||||
|
||||
def ofn_user
|
||||
@request.env['warden']&.user
|
||||
end
|
||||
|
||||
def decode_token
|
||||
JWT.decode(
|
||||
access_token,
|
||||
self.class.public_key,
|
||||
true, { algorithm: "RS256" }
|
||||
).first
|
||||
end
|
||||
|
||||
def access_token
|
||||
@request.headers['Authorization'].to_s.split(' ').last
|
||||
end
|
||||
|
||||
def find_ofn_user(payload)
|
||||
return if payload["email"].blank?
|
||||
|
||||
Spree::User.find_by(uid: payload["email"])
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,18 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Service used to fetch variants related to an entreprise.
|
||||
# It improves maintenance as it is the central point requesting
|
||||
# Spree::Varaint inside the DfcProvider engine.
|
||||
module DfcProvider
|
||||
class VariantFetcher
|
||||
def initialize(enterprise)
|
||||
@enterprise = enterprise
|
||||
end
|
||||
|
||||
def scope
|
||||
Spree::Variant.
|
||||
joins(product: :supplier).
|
||||
where('enterprises.id' => @enterprise.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
16
engines/dfc_provider/app/services/variant_fetcher.rb
Normal file
16
engines/dfc_provider/app/services/variant_fetcher.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Service used to fetch variants related to an enterprise.
|
||||
# It improves maintenance as it is the central point requesting
|
||||
# Spree::Variant inside the DfcProvider engine.
|
||||
class VariantFetcher
|
||||
def initialize(enterprise)
|
||||
@enterprise = enterprise
|
||||
end
|
||||
|
||||
def scope
|
||||
Spree::Variant.not_master.
|
||||
joins(:product).
|
||||
where(spree_products: { supplier: @enterprise })
|
||||
end
|
||||
end
|
||||
@@ -17,7 +17,7 @@ describe DfcProvider::EnterprisesController, type: :controller do
|
||||
|
||||
context 'with an authenticated user' do
|
||||
before do
|
||||
allow_any_instance_of(DfcProvider::AuthorizationControl)
|
||||
allow_any_instance_of(AuthorizationControl)
|
||||
.to receive(:user)
|
||||
.and_return(user)
|
||||
end
|
||||
|
||||
@@ -15,7 +15,7 @@ describe DfcProvider::PersonsController, type: :controller do
|
||||
|
||||
context 'with an authenticated user' do
|
||||
before do
|
||||
allow_any_instance_of(DfcProvider::AuthorizationControl)
|
||||
allow_any_instance_of(AuthorizationControl)
|
||||
.to receive(:user)
|
||||
.and_return(user)
|
||||
end
|
||||
|
||||
@@ -20,7 +20,7 @@ describe DfcProvider::SuppliedProductsController, type: :controller do
|
||||
|
||||
context 'with an authenticated user' do
|
||||
before do
|
||||
allow_any_instance_of(DfcProvider::AuthorizationControl)
|
||||
allow_any_instance_of(AuthorizationControl)
|
||||
.to receive(:user)
|
||||
.and_return(user)
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
require DfcProvider::Engine.root.join("spec/spec_helper")
|
||||
|
||||
describe DfcProvider::AuthorizationControl do
|
||||
describe AuthorizationControl do
|
||||
include AuthorizationHelper
|
||||
|
||||
let(:user) { create(:oidc_user) }
|
||||
|
||||
26
engines/dfc_provider/spec/services/variant_fetcher_spec.rb
Normal file
26
engines/dfc_provider/spec/services/variant_fetcher_spec.rb
Normal file
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require DfcProvider::Engine.root.join("spec/spec_helper")
|
||||
|
||||
describe VariantFetcher do
|
||||
subject { VariantFetcher.new(enterprise) }
|
||||
let(:enterprise) { build(:enterprise) }
|
||||
let(:other_enterprise) { build(:enterprise) }
|
||||
|
||||
it "returns an empty set" do
|
||||
expect(subject.scope).to eq []
|
||||
end
|
||||
|
||||
it "returns the variants of a supplier" do
|
||||
product = create(:product, supplier: enterprise)
|
||||
|
||||
expect(subject.scope.count).to eq 1
|
||||
expect(subject.scope).to eq product.variants
|
||||
end
|
||||
|
||||
it "ignores the variants of another enterprise" do
|
||||
create(:product, supplier: other_enterprise)
|
||||
|
||||
expect(subject.scope).to eq []
|
||||
end
|
||||
end
|
||||
@@ -8,7 +8,7 @@ module AuthorizationHelper
|
||||
|
||||
def allow_token_for(payload)
|
||||
private_key = OpenSSL::PKey::RSA.generate 2048
|
||||
allow(DfcProvider::AuthorizationControl).to receive(:public_key).
|
||||
allow(AuthorizationControl).to receive(:public_key).
|
||||
and_return(private_key.public_key)
|
||||
|
||||
JWT.encode(payload, private_key, "RS256")
|
||||
|
||||
Reference in New Issue
Block a user