Skip to content

Commit 9f7a53f

Browse files
authored
Merge pull request #6090 from mamhoff/move-permission-sets-to-app
Move permission sets back to app/
2 parents b1b8567 + fba1c16 commit 9f7a53f

37 files changed

+768
-675
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ Layout/EmptyLinesAroundAttributeAccessor:
9292
- "core/app/models/spree/stock_quantities.rb"
9393
- "core/app/models/spree/variant.rb"
9494
- "core/lib/spree/app_configuration.rb"
95-
- "core/lib/spree/permission_sets/base.rb"
9695
- "core/lib/spree/preferences/configuration.rb"
9796
- "core/spec/lib/spree/core/validators/email_spec.rb"
9897
- "core/spec/models/spree/preferences/statically_configurable_spec.rb"
@@ -145,7 +144,6 @@ Layout/MultilineOperationIndentation:
145144
Exclude:
146145
- "core/lib/spree/core/engine.rb"
147146
- "core/lib/spree/core/importer/order.rb"
148-
- "core/lib/spree/permission_sets/default_customer.rb"
149147
- "core/lib/spree/testing_support/factories/address_factory.rb"
150148

151149
# Offense count: 3
@@ -525,7 +523,6 @@ Style/CommentAnnotation:
525523
- "backend/app/controllers/spree/admin/products_controller.rb"
526524
- "backend/app/controllers/spree/admin/resource_controller.rb"
527525
- "core/app/models/spree/payment_method/store_credit.rb"
528-
- "core/lib/spree/permission_sets/user_management.rb"
529526
- "core/lib/spree/testing_support/rake.rb"
530527
- "core/spec/models/spree/variant/scopes_spec.rb"
531528

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module PermissionSets
5+
# This is the base class used for crafting permission sets.
6+
#
7+
# This is used by {Spree::RoleConfiguration} when adding custom behavior to {Spree::Ability}.
8+
# See one of the subclasses for example structure such as {Spree::PermissionSets::UserDisplay}
9+
#
10+
# @see Spree::RoleConfiguration
11+
# @see Spree::PermissionSets
12+
class Base
13+
# @param ability [CanCan::Ability]
14+
# The ability that will be extended with the current permission set.
15+
# The ability passed in must respond to #user
16+
def initialize(ability)
17+
@ability = ability
18+
end
19+
20+
# Activate permissions on the ability. Put your can and cannot statements here.
21+
# Must be overridden by subclasses
22+
def activate!
23+
raise NotImplementedError.new
24+
end
25+
26+
# Provide the permission set privilege in the form of a :symbol.
27+
# Must be overridden by subclasses.
28+
def self.privilege
29+
raise NotImplementedError, "Subclass #{name} must define a privilege using `self.privilege :symbol`"
30+
end
31+
32+
# Provide the permission set category in the form of a :symbol.
33+
# Must be overridden by subclasses.
34+
def self.category
35+
raise NotImplementedError, "Subclass #{name} must define a category using `self.category :symbol`"
36+
end
37+
38+
private
39+
40+
attr_reader :ability
41+
42+
delegate :can, :cannot, :user, to: :ability
43+
end
44+
end
45+
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module PermissionSets
5+
# Read-only permissions for e-commerce settings.
6+
#
7+
# Roles with this permission will be able to view information, also from the admin
8+
# panel, about:
9+
#
10+
# - Tax categories
11+
# - Tax rates
12+
# - Zones
13+
# - Countries
14+
# - States
15+
# - Payment methods
16+
# - Taxonomies
17+
# - Shipping methods
18+
# - Shipping categories
19+
# - Stock locations
20+
# - Stock movements
21+
# - Refund reasons
22+
# - Reimbursement types
23+
# - Return reasons
24+
class ConfigurationDisplay < PermissionSets::Base
25+
class << self
26+
def privilege
27+
:display
28+
end
29+
30+
def category
31+
:configuration
32+
end
33+
end
34+
35+
def activate!
36+
can [:read, :admin], Spree::TaxCategory
37+
can [:read, :admin], Spree::TaxRate
38+
can [:read, :admin], Spree::Zone
39+
can [:read, :admin], Spree::Country
40+
can [:read, :admin], Spree::State
41+
can [:read, :admin], Spree::PaymentMethod
42+
can [:read, :admin], Spree::Taxonomy
43+
can [:read, :admin], Spree::ShippingMethod
44+
can [:read, :admin], Spree::ShippingCategory
45+
can [:read, :admin], Spree::StockLocation
46+
can [:read, :admin], Spree::StockMovement
47+
can [:read, :admin], Spree::RefundReason
48+
can [:read, :admin], Spree::ReimbursementType
49+
can [:read, :admin], Spree::ReturnReason
50+
end
51+
end
52+
end
53+
end
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module PermissionSets
5+
# Read and write permissions for e-commerce settings.
6+
#
7+
# Roles with this permission set will have full control over:
8+
#
9+
# - Tax categories
10+
# - Tax rates
11+
# - Zones
12+
# - Countries
13+
# - States
14+
# - Payment methods
15+
# - Taxonomies
16+
# - Shipping methods
17+
# - Shipping categories
18+
# - Stock locations
19+
# - Stock movements
20+
# - Refund reasons
21+
# - Reimbursement types
22+
# - Return reasons
23+
class ConfigurationManagement < PermissionSets::Base
24+
class << self
25+
def privilege
26+
:management
27+
end
28+
29+
def category
30+
:configuration
31+
end
32+
end
33+
34+
def activate!
35+
can :manage, Spree::TaxCategory
36+
can :manage, Spree::TaxRate
37+
can :manage, Spree::Zone
38+
can :manage, Spree::Country
39+
can :manage, Spree::State
40+
can :manage, Spree::PaymentMethod
41+
can :manage, Spree::Taxonomy
42+
can :manage, Spree::ShippingMethod
43+
can :manage, Spree::ShippingCategory
44+
can :manage, Spree::StockLocation
45+
can :manage, Spree::StockMovement
46+
can :manage, Spree::RefundReason
47+
can :manage, Spree::ReimbursementType
48+
can :manage, Spree::ReturnReason
49+
end
50+
end
51+
end
52+
end
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module PermissionSets
5+
# Permissions for viewing the admin dashboard.
6+
#
7+
# Roles with this permission set will be able to view the admin dashboard,
8+
# which may or not contain sensitive information depending on
9+
# customizations.
10+
class DashboardDisplay < PermissionSets::Base
11+
class << self
12+
def privilege
13+
:other
14+
end
15+
16+
def category
17+
:dashboard_display
18+
end
19+
end
20+
21+
def activate!
22+
Spree.deprecator.warn "The #{self.class.name} module is deprecated. " \
23+
"If you still use dashboards, please copy all controllers and views from #{self.class.name} to your application."
24+
can [:admin, :home], :dashboards
25+
end
26+
end
27+
end
28+
end
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module PermissionSets
5+
# Permissions for e-commerce customers.
6+
#
7+
# This permission set is always added to the `:default` role, which in turn
8+
# is the default role for all users without any explicit roles.
9+
#
10+
# Permissions include reading and updating orders when the ability's user
11+
# has been assigned as the order's user, unless the order is already
12+
# completed. Same is true for guest checkout orders.
13+
#
14+
# It grants read-only permissions for the following resources typically used
15+
# during a checkout process:
16+
#
17+
# - Zones
18+
# - Countries
19+
# - States
20+
# - Taxons
21+
# - Taxonomies
22+
# - Products
23+
# - Properties
24+
# - Product properties
25+
# - Variants
26+
# - Option types
27+
# - Option values
28+
# - Stock items
29+
# - Stock locations
30+
#
31+
# Abilities with this role can also create refund authorizations for orders
32+
# with the same user, as well as reading and updating the user record and
33+
# their associated cards.
34+
class DefaultCustomer < PermissionSets::Base
35+
class << self
36+
def privilege
37+
:other
38+
end
39+
40+
def category
41+
:default_customer
42+
end
43+
end
44+
45+
def activate!
46+
can :read, Country
47+
can :read, OptionType
48+
can :read, OptionValue
49+
can :create, Order do |order, token|
50+
# same user, or both nil
51+
order.user == user ||
52+
# guest checkout order
53+
order.email.present? ||
54+
# via API, just like with show and update
55+
(order.guest_token.present? && token == order.guest_token)
56+
end
57+
can [:show, :update], Order, Order.where(user:) do |order, token|
58+
order.user == user || (order.guest_token.present? && token == order.guest_token)
59+
end
60+
cannot :update, Order do |order|
61+
order.completed?
62+
end
63+
can :create, ReturnAuthorization do |return_authorization|
64+
return_authorization.order.user == user
65+
end
66+
can [:read, :update], CreditCard, user_id: user.id
67+
can :read, Product
68+
can :read, ProductProperty
69+
can :read, Property
70+
can :create, Spree.user_class
71+
can [:show, :update, :update_email], Spree.user_class, id: user.id
72+
can :read, State
73+
can :read, StockItem, stock_location: { active: true }
74+
can :read, StockLocation, active: true
75+
can :read, Taxon
76+
can :read, Taxonomy
77+
can [:save_in_address_book, :remove_from_address_book], Spree.user_class, id: user.id
78+
can [:read, :view_out_of_stock], Variant
79+
can :read, Zone
80+
end
81+
end
82+
end
83+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module PermissionSets
5+
# Read permissions for orders.
6+
#
7+
# This permission set allows users to view all related information about
8+
# orders, also from the admin panel, including:
9+
#
10+
# - Orders
11+
# - Payments
12+
# - Shipments
13+
# - Adjustments
14+
# - Line items
15+
# - Return authorizations
16+
# - Customer returns
17+
# - Order cancellations
18+
# - Reimbursements
19+
# - Return items
20+
# - Refunds
21+
#
22+
# However, it does not allow any modifications to be made to any of these
23+
# resources.
24+
class OrderDisplay < PermissionSets::Base
25+
class << self
26+
def privilege
27+
:display
28+
end
29+
30+
def category
31+
:order
32+
end
33+
end
34+
35+
def activate!
36+
can [:read, :admin, :edit, :cart], Spree::Order
37+
can [:read, :admin], Spree::Payment
38+
can [:read, :admin], Spree::Shipment
39+
can [:read, :admin], Spree::Adjustment
40+
can [:read, :admin], Spree::LineItem
41+
can [:read, :admin], Spree::ReturnAuthorization
42+
can [:read, :admin], Spree::CustomerReturn
43+
can [:read, :admin], Spree::OrderCancellations
44+
can [:read, :admin], Spree::Reimbursement
45+
can [:read, :admin], Spree::ReturnItem
46+
can [:read, :admin], Spree::Refund
47+
end
48+
end
49+
end
50+
end
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
module Spree
4+
module PermissionSets
5+
# Full permissions for order management.
6+
#
7+
# This permission set grants full control over all order and related resources,
8+
# including:
9+
#
10+
# - Orders
11+
# - Payments
12+
# - Shipments
13+
# - Adjustments
14+
# - Line items
15+
# - Return authorizations
16+
# - Customer returns
17+
# - Order cancellations
18+
# - Reimbursements
19+
# - Return items
20+
# - Refunds
21+
#
22+
# It also allows reading reimbursement types, but not modifying them.
23+
class OrderManagement < PermissionSets::Base
24+
class << self
25+
def privilege
26+
:management
27+
end
28+
29+
def category
30+
:order
31+
end
32+
end
33+
34+
def activate!
35+
can :read, Spree::ReimbursementType
36+
can :manage, Spree::Order
37+
can :manage, Spree::Payment
38+
can :manage, Spree::Shipment
39+
can :manage, Spree::Adjustment
40+
can :manage, Spree::LineItem
41+
can :manage, Spree::ReturnAuthorization
42+
can :manage, Spree::CustomerReturn
43+
can :manage, Spree::OrderCancellations
44+
can :manage, Spree::Reimbursement
45+
can :manage, Spree::ReturnItem
46+
can :manage, Spree::Refund
47+
end
48+
end
49+
end
50+
end

0 commit comments

Comments
 (0)