Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

[Admin product page] Add permission to publishing section (#14200)

Kacper Mękarskia338d917cae6
diff --git a/spree/admin/app/views/spree/admin/products/form/_publishing.html.erb b/spree/admin/app/views/spree/admin/products/form/_publishing.html.erb
index 9118cd99dd..733f2a18b5 100644
--- a/spree/admin/app/views/spree/admin/products/form/_publishing.html.erb
+++ b/spree/admin/app/views/spree/admin/products/form/_publishing.html.erb
@@ -18,6 +18,7 @@
tz_help = Spree.t('admin.datetime_field_timezone_help', zone: store_timezone.name)
store_channels = current_store.channels.order(:name).to_a
publications_by_channel = @product.product_publications.includes(:channel).index_by(&:channel_id)
+ can_manage_publications = can?(:manage, Spree::ProductPublication)
# Build one record per store channel: the existing publication when
# attached, or a fresh ProductPublication when not. Stable channel-keyed
@@ -33,7 +34,7 @@
<div class="card mb-6" data-controller="product-publishing">
<div class="card-header d-flex align-items-center justify-content-between">
<h6 class="card-title mb-0"><%= Spree.t('admin.publishing.title') %></h6>
- <% if store_channels.any? %>
+ <% if store_channels.any? && can_manage_publications %>
<button type="button"
class="btn btn-sm btn-outline-secondary"
data-action="product-publishing#toggleManage">
@@ -47,6 +48,7 @@
<p class="text-xs text-muted mb-0"><%= Spree.t('admin.publishing.no_channels') %></p>
<% else %>
<%# ---- Manage panel (hidden by default) ---- %>
+ <% if can_manage_publications %>
<div class="d-none flex flex-col gap-2 mb-3" data-product-publishing-target="manage">
<p class="text-xs text-muted mb-1"><%= Spree.t('admin.publishing.manage_description') %></p>
<% channel_rows.each_with_index do |(channel, publication), index| %>
@@ -73,6 +75,7 @@
<% end %>
<% end %>
</div>
+ <% end %>
<%# ---- Per-publication scheduling rows ---- %>
<% if publications_by_channel.empty? %>
@@ -80,43 +83,57 @@
<% else %>
<% channel_rows.each_with_index do |(channel, publication), index| %>
<% next unless publication.persisted? %>
- <%= f.fields_for :legacy_product_publications, publication, child_index: index do |pf| %>
- <details class="publication-row group rounded -mx-2 px-2 hover:bg-gray-50">
- <summary class="flex items-start justify-between gap-3 py-2 cursor-pointer" style="list-style: none;">
- <div class="flex min-w-0 flex-1 flex-col gap-0.5">
- <div class="flex items-center gap-2">
- <span class="truncate text-sm font-medium"><%= channel.name %></span>
- <%= publication_status_badge(@product.status, publication) %>
+ <% if can_manage_publications %>
+ <%= f.fields_for :legacy_product_publications, publication, child_index: index do |pf| %>
+ <details class="publication-row group rounded -mx-2 px-2 hover:bg-gray-50">
+ <summary class="flex items-start justify-between gap-3 py-2 cursor-pointer" style="list-style: none;">
+ <div class="flex min-w-0 flex-1 flex-col gap-0.5">
+ <div class="flex items-center gap-2">
+ <span class="truncate text-sm font-medium"><%= channel.name %></span>
+ <%= publication_status_badge(@product.status, publication) %>
+ </div>
+ <span class="text-xs text-muted">
+ <%= publication_caption(@product.status, publication, current_store) %>
+ </span>
</div>
- <span class="text-xs text-muted">
- <%= publication_caption(@product.status, publication, current_store) %>
+ <span class="shrink-0 mt-1 opacity-0 transition-opacity group-hover:opacity-100 text-muted">
+ <%= icon('pencil', class: 'h-3.5 w-3.5') %>
</span>
+ </summary>
+ <div class="mt-2 ps-2 pb-2 flex flex-col gap-3">
+ <div>
+ <%= pf.label :published_at,
+ Spree.t('admin.publishing.published_at_label'),
+ class: 'form-label text-xs mb-1' %>
+ <%= pf.datetime_field :published_at,
+ value: to_store_local.call(publication.published_at),
+ class: 'form-control form-control-sm' %>
+ <small class="text-muted text-xs"><%= tz_help %></small>
+ </div>
+ <div>
+ <%= pf.label :unpublished_at,
+ Spree.t('admin.publishing.unpublished_at_label'),
+ class: 'form-label text-xs mb-1' %>
+ <%= pf.datetime_field :unpublished_at,
+ value: to_store_local.call(publication.unpublished_at),
+ class: 'form-control form-control-sm' %>
+ <small class="text-muted text-xs"><%= tz_help %></small>
+ </div>
</div>
- <span class="shrink-0 mt-1 opacity-0 transition-opacity group-hover:opacity-100 text-muted">
- <%= icon('pencil', class: 'h-3.5 w-3.5') %>
- </span>
- </summary>
- <div class="mt-2 ps-2 pb-2 flex flex-col gap-3">
- <div>
- <%= pf.label :published_at,
- Spree.t('admin.publishing.published_at_label'),
- class: 'form-label text-xs mb-1' %>
- <%= pf.datetime_field :published_at,
- value: to_store_local.call(publication.published_at),
- class: 'form-control form-control-sm' %>
- <small class="text-muted text-xs"><%= tz_help %></small>
- </div>
- <div>
- <%= pf.label :unpublished_at,
- Spree.t('admin.publishing.unpublished_at_label'),
- class: 'form-label text-xs mb-1' %>
- <%= pf.datetime_field :unpublished_at,
- value: to_store_local.call(publication.unpublished_at),
- class: 'form-control form-control-sm' %>
- <small class="text-muted text-xs"><%= tz_help %></small>
+ </details>
+ <% end %>
+ <% else %>
+ <div class="publication-row -mx-2 px-2">
+ <div class="flex min-w-0 flex-1 flex-col gap-0.5 py-2">
+ <div class="flex items-center gap-2">
+ <span class="truncate text-sm font-medium"><%= channel.name %></span>
+ <%= publication_status_badge(@product.status, publication) %>
</div>
+ <span class="text-xs text-muted">
+ <%= publication_caption(@product.status, publication, current_store) %>
+ </span>
</div>
- </details>
+ </div>
<% end %>
<% end %>
<% end %>
diff --git a/spree/core/app/models/spree/permission_sets/product_management.rb b/spree/core/app/models/spree/permission_sets/product_management.rb
index 5081d6ccf4..715b99adca 100644
--- a/spree/core/app/models/spree/permission_sets/product_management.rb
+++ b/spree/core/app/models/spree/permission_sets/product_management.rb
@@ -21,6 +21,7 @@ module Spree
can :manage, Spree::PriceList
can :manage, Spree::PriceRule
can :manage, Spree::Asset
+ can :manage, Spree::ProductPublication
end
end
end
diff --git a/spree/core/app/services/spree/products/prepare_nested_attributes.rb b/spree/core/app/services/spree/products/prepare_nested_attributes.rb
index bdd6f32c8f..e0b0f8a60b 100644
--- a/spree/core/app/services/spree/products/prepare_nested_attributes.rb
+++ b/spree/core/app/services/spree/products/prepare_nested_attributes.rb
@@ -67,6 +67,8 @@ module Spree
end
end
+ params.delete(:legacy_product_publications_attributes) unless can?(:manage, Spree::ProductPublication)
+
# ensure the product is owned by a store
params[:store_id] = store.id if params[:store_id].blank? && product.store_id.blank?
diff --git a/spree/core/spec/models/spree/permission_sets/product_management_spec.rb b/spree/core/spec/models/spree/permission_sets/product_management_spec.rb
index 2020b35986..8535d9cb4e 100644
--- a/spree/core/spec/models/spree/permission_sets/product_management_spec.rb
+++ b/spree/core/spec/models/spree/permission_sets/product_management_spec.rb
@@ -42,6 +42,10 @@ RSpec.describe Spree::PermissionSets::ProductManagement do
expect(ability.can?(:manage, Spree::Price)).to be true
end
+ it 'grants manage access to ProductPublication' do
+ expect(ability.can?(:manage, Spree::ProductPublication)).to be true
+ end
+
it 'does not grant manage access to Order' do
expect(ability.can?(:manage, Spree::Order)).to be false
end
diff --git a/spree/core/spec/services/spree/products/prepare_nested_attributes_spec.rb b/spree/core/spec/services/spree/products/prepare_nested_attributes_spec.rb
index 6a0cfebe76..09e68e5c0b 100644
--- a/spree/core/spec/services/spree/products/prepare_nested_attributes_spec.rb
+++ b/spree/core/spec/services/spree/products/prepare_nested_attributes_spec.rb
@@ -242,6 +242,33 @@ RSpec.describe Spree::Products::PrepareNestedAttributes do
end
end
+ describe 'legacy_product_publications_attributes handling' do
+ let(:channel) { create(:channel, store: store) }
+ let(:raw_params) do
+ {
+ legacy_product_publications_attributes: {
+ '0' => { channel_id: channel.id, _destroy: '0' }
+ }
+ }
+ end
+
+ context 'when user cannot manage publications' do
+ before do
+ ability.cannot :manage, Spree::ProductPublication
+ end
+
+ it 'strips the publications attributes' do
+ expect(prepared_params).not_to have_key(:legacy_product_publications_attributes)
+ end
+ end
+
+ context 'when user can manage publications' do
+ it 'preserves the publications attributes' do
+ expect(prepared_params[:legacy_product_publications_attributes]['0'][:channel_id]).to eq(channel.id)
+ end
+ end
+ end
+
describe 'stock_items_attributes handling' do
let(:variant) { product.master }