From 49f8cf5c270e6070f9b59129ca5a256091877083 Mon Sep 17 00:00:00 2001 From: ghaithdabash Date: Fri, 29 Nov 2019 12:23:23 +0200 Subject: [PATCH 1/2] Change to spree 5 --- .../spree/admin/base_controller_decorator.rb | 2 +- app/models/spree/ability_decorator.rb | 4 +++- app/models/spree/role_decorator.rb | 22 +++++++++++-------- app/models/spree/user_decorator.rb | 8 +++++-- spree_admin_roles_and_access.gemspec | 2 +- 5 files changed, 24 insertions(+), 14 deletions(-) diff --git a/app/controllers/spree/admin/base_controller_decorator.rb b/app/controllers/spree/admin/base_controller_decorator.rb index 990928f..9584666 100644 --- a/app/controllers/spree/admin/base_controller_decorator.rb +++ b/app/controllers/spree/admin/base_controller_decorator.rb @@ -1,4 +1,4 @@ -Spree::Admin::BaseController.class_eval do +module Spree::Admin::BaseControllerDecorator def authorize_admin begin if params[:id] diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree/ability_decorator.rb index 52e9ef7..293ad7d 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree/ability_decorator.rb @@ -1,5 +1,5 @@ module Spree - Ability.class_eval do + module AbilityDecorator def initialize(user) self.clear_aliased_actions @@ -26,3 +26,5 @@ def user_roles(user) end end end + +::Spree::Ability.prepend(Spree::AbilityDecorator) diff --git a/app/models/spree/role_decorator.rb b/app/models/spree/role_decorator.rb index bb1f04a..f9e3a40 100644 --- a/app/models/spree/role_decorator.rb +++ b/app/models/spree/role_decorator.rb @@ -1,18 +1,22 @@ -Spree::Role.class_eval do +module Spree::RoleDecorator + def self.prepended(base) + base.has_many :roles_permission_sets, dependent: :destroy + base.has_many :permission_sets, through: :roles_permission_sets + base.has_many :permissions, through: :permission_sets - has_many :roles_permission_sets, dependent: :destroy - has_many :permission_sets, through: :roles_permission_sets - has_many :permissions, through: :permission_sets + # DEPRECATED: Use permission sets instead. Only here for aiding migration for existing users + base.has_and_belongs_to_many :legacy_permissions, join_table: 'spree_roles_permissions', class_name: 'Spree::Permission' - # DEPRECATED: Use permission sets instead. Only here for aiding migration for existing users - has_and_belongs_to_many :legacy_permissions, join_table: 'spree_roles_permissions', class_name: 'Spree::Permission' + base.validates :name, uniqueness: true, allow_blank: true + base.validates :permission_sets, length: { minimum: 1, too_short: :atleast_one_permission_set_is_required }, on: :update - validates :name, uniqueness: true, allow_blank: true - validates :permission_sets, length: { minimum: 1, too_short: :atleast_one_permission_set_is_required }, on: :update + base.scope :default_role, lambda { where(is_default: true) } + end def has_permission?(permission_title) permissions.pluck(:title).include?(permission_title) end - scope :default_role, lambda { where(is_default: true) } end + +::Spree::Role.prepend(Spree::RoleDecorator) diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb index 35b09e7..a8f7c41 100644 --- a/app/models/spree/user_decorator.rb +++ b/app/models/spree/user_decorator.rb @@ -1,5 +1,9 @@ module Spree - Spree.user_class.class_eval do - alias_attribute :roles, :spree_roles + module UserDecorator + def self.prepended(base) + base.alias_attribute :roles, :spree_roles + end end end + +::Spree.user_class.prepend(Spree::UserDecorator) diff --git a/spree_admin_roles_and_access.gemspec b/spree_admin_roles_and_access.gemspec index 3cc1032..904c18e 100644 --- a/spree_admin_roles_and_access.gemspec +++ b/spree_admin_roles_and_access.gemspec @@ -14,7 +14,7 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.requirements << 'none' - spree_version = '>= 3.2.0', '< 4.0.0' + spree_version = '>= 3.2.0', '< 5.0.0' s.add_dependency 'spree_core', spree_version s.add_dependency 'spree_auth_devise' From ee51bce961863e311aa87c05e43328c9e2afe22c Mon Sep 17 00:00:00 2001 From: ghaithdabash Date: Fri, 29 Nov 2019 13:22:09 +0200 Subject: [PATCH 2/2] Change guest_token to token in orders --- lib/spree/permissions.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/spree/permissions.rb b/lib/spree/permissions.rb index f214d75..7c2223f 100644 --- a/lib/spree/permissions.rb +++ b/lib/spree/permissions.rb @@ -25,11 +25,11 @@ def method_missing(name, *args, &block) current_ability.can :create, Spree::Order current_ability.can :read, Spree::Order, [] do |order, token| - order.user == user || (order.guest_token && token == order.guest_token) + order.user == user || (order.token && token == order.token) end current_ability.can :update, Spree::Order do |order, token| - !order.completed? && (order.user == user || order.guest_token && token == order.guest_token) + !order.completed? && (order.user == user || order.token && token == order.token) end current_ability.can :read, Spree::Address do |address|