From fc33cd5812563fa2571f81afe171caaacd2a0258 Mon Sep 17 00:00:00 2001 From: rishabh gupta Date: Mon, 1 Jun 2020 11:11:13 +0530 Subject: [PATCH 1/3] changed decorator files and updated migrations --- .../spree/admin/base_controller_decorator.rb | 27 ---------------- .../base_controller_decorator.rb | 31 +++++++++++++++++++ app/models/spree/role_decorator.rb | 18 ----------- app/models/spree/user_decorator.rb | 5 --- .../ability_decorator.rb | 9 +++--- .../role_decorator.rb | 24 ++++++++++++++ .../user_decorator.rb | 7 +++++ ...20130709104101_create_spree_permissions.rb | 2 +- ...09104945_create_spree_roles_permissions.rb | 2 +- ...able_is_default_and_name_to_spree_roles.rb | 2 +- ...0503090436_create_spree_permission_sets.rb | 2 +- ...1013_create_spree_roles_permission_sets.rb | 2 +- ...reate_spree_permissions_permission_sets.rb | 2 +- ...tion_to_permissions_and_permission_sets.rb | 2 +- ..._add_display_boolean_to_permission_sets.rb | 2 +- ..._deprecate_legacy_roles_and_permissions.rb | 2 +- ..._remove_column_boolean_from_permissions.rb | 2 +- ...170509090346_add_admin_boolean_to_roles.rb | 2 +- spec/spec_helper.rb | 1 + spree_admin_roles_and_access.gemspec | 5 +-- 20 files changed, 82 insertions(+), 67 deletions(-) delete mode 100644 app/controllers/spree/admin/base_controller_decorator.rb create mode 100644 app/controllers/spree_admin_roles_and_access/base_controller_decorator.rb delete mode 100644 app/models/spree/role_decorator.rb delete mode 100644 app/models/spree/user_decorator.rb rename app/models/{spree => spree_admin_roles_and_access}/ability_decorator.rb (80%) create mode 100644 app/models/spree_admin_roles_and_access/role_decorator.rb create mode 100644 app/models/spree_admin_roles_and_access/user_decorator.rb diff --git a/app/controllers/spree/admin/base_controller_decorator.rb b/app/controllers/spree/admin/base_controller_decorator.rb deleted file mode 100644 index 990928f..0000000 --- a/app/controllers/spree/admin/base_controller_decorator.rb +++ /dev/null @@ -1,27 +0,0 @@ -Spree::Admin::BaseController.class_eval do - def authorize_admin - begin - if params[:id] - record = model_class.where(PARAM_ATTRIBUTE[controller_name] => params[:id]).first - elsif new_action? - record = model_class.new - else - record = model_class - end - raise if record.blank? - rescue - record = "#{params[:controller]}" - end - authorize! :admin, record - authorize_with_attributes! params[:action].to_sym, record, params[controller_name.singularize] - end - - private - def unauthorized - redirect_unauthorized_access - end - - def new_action? - NEW_ACTIONS.include?(params[:action].to_sym) - end -end diff --git a/app/controllers/spree_admin_roles_and_access/base_controller_decorator.rb b/app/controllers/spree_admin_roles_and_access/base_controller_decorator.rb new file mode 100644 index 0000000..45a5dde --- /dev/null +++ b/app/controllers/spree_admin_roles_and_access/base_controller_decorator.rb @@ -0,0 +1,31 @@ +module SpreeAdminRolesAndAccess + module BaseControllerDecorator + def authorize_admin + begin + if params[:id] + record = model_class.where(PARAM_ATTRIBUTE[controller_name] => params[:id]).first + elsif new_action? + record = model_class.new + else + record = model_class + end + raise if record.blank? + rescue + record = "#{params[:controller]}" + end + authorize! :admin, record + authorize_with_attributes! params[:action].to_sym, record, params[controller_name.singularize] + end + + private + def unauthorized + redirect_unauthorized_access + end + + def new_action? + NEW_ACTIONS.include?(params[:action].to_sym) + end + end +end + +Spree::Admin::BaseController.prepend SpreeAdminRolesAndAccess::BaseControllerDecorator \ No newline at end of file diff --git a/app/models/spree/role_decorator.rb b/app/models/spree/role_decorator.rb deleted file mode 100644 index bb1f04a..0000000 --- a/app/models/spree/role_decorator.rb +++ /dev/null @@ -1,18 +0,0 @@ -Spree::Role.class_eval do - - 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 - has_and_belongs_to_many :legacy_permissions, join_table: 'spree_roles_permissions', class_name: 'Spree::Permission' - - validates :name, uniqueness: true, allow_blank: true - validates :permission_sets, length: { minimum: 1, too_short: :atleast_one_permission_set_is_required }, on: :update - - def has_permission?(permission_title) - permissions.pluck(:title).include?(permission_title) - end - - scope :default_role, lambda { where(is_default: true) } -end diff --git a/app/models/spree/user_decorator.rb b/app/models/spree/user_decorator.rb deleted file mode 100644 index 35b09e7..0000000 --- a/app/models/spree/user_decorator.rb +++ /dev/null @@ -1,5 +0,0 @@ -module Spree - Spree.user_class.class_eval do - alias_attribute :roles, :spree_roles - end -end diff --git a/app/models/spree/ability_decorator.rb b/app/models/spree_admin_roles_and_access/ability_decorator.rb similarity index 80% rename from app/models/spree/ability_decorator.rb rename to app/models/spree_admin_roles_and_access/ability_decorator.rb index 52e9ef7..6fbd346 100644 --- a/app/models/spree/ability_decorator.rb +++ b/app/models/spree_admin_roles_and_access/ability_decorator.rb @@ -1,6 +1,5 @@ -module Spree - Ability.class_eval do - +module SpreeAdminRolesAndAccess + module AbilityDecorator def initialize(user) self.clear_aliased_actions @@ -15,7 +14,7 @@ def initialize(user) user_roles(user).map(&:permissions).flatten.uniq.map { |permission| permission.ability(self, user) } - Ability.abilities.each do |clazz| + ::Spree::Ability.abilities.each do |clazz| ability = clazz.send(:new, user) @rules = rules + ability.send(:rules) end @@ -26,3 +25,5 @@ def user_roles(user) end end end + +Spree::Ability.prepend SpreeAdminRolesAndAccess::AbilityDecorator \ No newline at end of file diff --git a/app/models/spree_admin_roles_and_access/role_decorator.rb b/app/models/spree_admin_roles_and_access/role_decorator.rb new file mode 100644 index 0000000..aa78101 --- /dev/null +++ b/app/models/spree_admin_roles_and_access/role_decorator.rb @@ -0,0 +1,24 @@ +module SpreeAdminRolesAndAccess + module 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 + + # 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' + + 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 + base.scope :default_role, lambda { where(is_default: true) } + end + + def has_permission?(permission_title) + permissions.pluck(:title).include?(permission_title) + end + + end +end + +Spree::Role.prepend SpreeAdminRolesAndAccess::RoleDecorator \ No newline at end of file diff --git a/app/models/spree_admin_roles_and_access/user_decorator.rb b/app/models/spree_admin_roles_and_access/user_decorator.rb new file mode 100644 index 0000000..e9deb47 --- /dev/null +++ b/app/models/spree_admin_roles_and_access/user_decorator.rb @@ -0,0 +1,7 @@ +module SpreeAdminRolesAndAccess + module UserDecorator + alias_attribute :roles, :spree_roles + end +end + +Spree::User.prepend SpreeAdminRolesAndAccess::UserDecorator \ No newline at end of file diff --git a/db/migrate/20130709104101_create_spree_permissions.rb b/db/migrate/20130709104101_create_spree_permissions.rb index 6f6c01c..86c19f2 100644 --- a/db/migrate/20130709104101_create_spree_permissions.rb +++ b/db/migrate/20130709104101_create_spree_permissions.rb @@ -1,4 +1,4 @@ -class CreateSpreePermissions < ActiveRecord::Migration[4.2] +class CreateSpreePermissions < SpreeExtension::Migration[4.2] def change create_table :spree_permissions do |t| t.string :title, null: false, unique: true diff --git a/db/migrate/20130709104945_create_spree_roles_permissions.rb b/db/migrate/20130709104945_create_spree_roles_permissions.rb index 32cb61a..8dfca9d 100644 --- a/db/migrate/20130709104945_create_spree_roles_permissions.rb +++ b/db/migrate/20130709104945_create_spree_roles_permissions.rb @@ -1,4 +1,4 @@ -class CreateSpreeRolesPermissions < ActiveRecord::Migration[4.2] +class CreateSpreeRolesPermissions < SpreeExtension::Migration[4.2] def change create_table :spree_roles_permissions, id: false do |t| t.integer :role_id, null: false diff --git a/db/migrate/20130709105614_add_editable_is_default_and_index_on_editable_is_default_and_name_to_spree_roles.rb b/db/migrate/20130709105614_add_editable_is_default_and_index_on_editable_is_default_and_name_to_spree_roles.rb index 82a9ee9..df6f6de 100644 --- a/db/migrate/20130709105614_add_editable_is_default_and_index_on_editable_is_default_and_name_to_spree_roles.rb +++ b/db/migrate/20130709105614_add_editable_is_default_and_index_on_editable_is_default_and_name_to_spree_roles.rb @@ -1,4 +1,4 @@ -class AddEditableIsDefaultAndIndexOnEditableIsDefaultAndNameToSpreeRoles < ActiveRecord::Migration[4.2] +class AddEditableIsDefaultAndIndexOnEditableIsDefaultAndNameToSpreeRoles < SpreeExtension::Migration[4.2] def change add_column :spree_roles, :editable, :boolean, default: true add_column :spree_roles, :is_default, :boolean, default: false diff --git a/db/migrate/20170503090436_create_spree_permission_sets.rb b/db/migrate/20170503090436_create_spree_permission_sets.rb index 0a88077..f3eeb97 100644 --- a/db/migrate/20170503090436_create_spree_permission_sets.rb +++ b/db/migrate/20170503090436_create_spree_permission_sets.rb @@ -1,4 +1,4 @@ -class CreateSpreePermissionSets < ActiveRecord::Migration[4.2] +class CreateSpreePermissionSets < SpreeExtension::Migration[4.2] def change create_table :spree_permission_sets do |t| t.string :name, null: false, unique: true diff --git a/db/migrate/20170503091013_create_spree_roles_permission_sets.rb b/db/migrate/20170503091013_create_spree_roles_permission_sets.rb index 253bd65..1019459 100644 --- a/db/migrate/20170503091013_create_spree_roles_permission_sets.rb +++ b/db/migrate/20170503091013_create_spree_roles_permission_sets.rb @@ -1,4 +1,4 @@ -class CreateSpreeRolesPermissionSets < ActiveRecord::Migration[4.2] +class CreateSpreeRolesPermissionSets < SpreeExtension::Migration[4.2] def change create_table :spree_roles_permission_sets do |t| t.references :role, index: true diff --git a/db/migrate/20170503101648_create_spree_permissions_permission_sets.rb b/db/migrate/20170503101648_create_spree_permissions_permission_sets.rb index 10a35a8..19f545f 100644 --- a/db/migrate/20170503101648_create_spree_permissions_permission_sets.rb +++ b/db/migrate/20170503101648_create_spree_permissions_permission_sets.rb @@ -1,4 +1,4 @@ -class CreateSpreePermissionsPermissionSets < ActiveRecord::Migration[4.2] +class CreateSpreePermissionsPermissionSets < SpreeExtension::Migration[4.2] def change create_table :spree_permissions_permission_sets do |t| t.references :permission, index: true diff --git a/db/migrate/20170508082615_add_description_to_permissions_and_permission_sets.rb b/db/migrate/20170508082615_add_description_to_permissions_and_permission_sets.rb index 3bdf9fe..d55a8b7 100644 --- a/db/migrate/20170508082615_add_description_to_permissions_and_permission_sets.rb +++ b/db/migrate/20170508082615_add_description_to_permissions_and_permission_sets.rb @@ -1,4 +1,4 @@ -class AddDescriptionToPermissionsAndPermissionSets < ActiveRecord::Migration[4.2] +class AddDescriptionToPermissionsAndPermissionSets < SpreeExtension::Migration[4.2] def change add_column :spree_permissions, :description, :string, default: '' add_column :spree_permission_sets, :description, :string, default: '' diff --git a/db/migrate/20170508082722_add_display_boolean_to_permission_sets.rb b/db/migrate/20170508082722_add_display_boolean_to_permission_sets.rb index 0de4517..c022b7b 100644 --- a/db/migrate/20170508082722_add_display_boolean_to_permission_sets.rb +++ b/db/migrate/20170508082722_add_display_boolean_to_permission_sets.rb @@ -1,4 +1,4 @@ -class AddDisplayBooleanToPermissionSets < ActiveRecord::Migration[4.2] +class AddDisplayBooleanToPermissionSets < SpreeExtension::Migration[4.2] def change add_column :spree_permission_sets, :display_permission, :boolean, default: false end diff --git a/db/migrate/20170508091139_deprecate_legacy_roles_and_permissions.rb b/db/migrate/20170508091139_deprecate_legacy_roles_and_permissions.rb index 7d3371e..6bdffa8 100644 --- a/db/migrate/20170508091139_deprecate_legacy_roles_and_permissions.rb +++ b/db/migrate/20170508091139_deprecate_legacy_roles_and_permissions.rb @@ -1,4 +1,4 @@ -class DeprecateLegacyRolesAndPermissions < ActiveRecord::Migration[4.2] +class DeprecateLegacyRolesAndPermissions < SpreeExtension::Migration[4.2] def display_warning ActiveSupport::Deprecation.warn('Direct relationship between roles and permissions is deprecated. Use #legacy_permissions to access old permissions') end diff --git a/db/migrate/20170509082144_remove_column_boolean_from_permissions.rb b/db/migrate/20170509082144_remove_column_boolean_from_permissions.rb index fb85d99..d63d0c6 100644 --- a/db/migrate/20170509082144_remove_column_boolean_from_permissions.rb +++ b/db/migrate/20170509082144_remove_column_boolean_from_permissions.rb @@ -1,4 +1,4 @@ -class RemoveColumnBooleanFromPermissions < ActiveRecord::Migration[4.2] +class RemoveColumnBooleanFromPermissions < SpreeExtension::Migration[4.2] def change remove_column :spree_permissions, :boolean, :boolean, default: true end diff --git a/db/migrate/20170509090346_add_admin_boolean_to_roles.rb b/db/migrate/20170509090346_add_admin_boolean_to_roles.rb index 80b2b9c..2bfc83d 100644 --- a/db/migrate/20170509090346_add_admin_boolean_to_roles.rb +++ b/db/migrate/20170509090346_add_admin_boolean_to_roles.rb @@ -1,4 +1,4 @@ -class AddAdminBooleanToRoles < ActiveRecord::Migration[4.2] +class AddAdminBooleanToRoles < SpreeExtension::Migration[4.2] def change add_column :spree_roles, :admin_accessible, :boolean, default: false end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 5fc97ae..4e54552 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -29,6 +29,7 @@ require 'spree/testing_support/controller_requests' require 'spree/testing_support/authorization_helpers' require 'spree/testing_support/url_helpers' +require 'spree_dev_tools/rspec/spec_helper' RSpec.configure do |config| # == Requests support diff --git a/spree_admin_roles_and_access.gemspec b/spree_admin_roles_and_access.gemspec index 3cc1032..5973735 100644 --- a/spree_admin_roles_and_access.gemspec +++ b/spree_admin_roles_and_access.gemspec @@ -14,14 +14,14 @@ Gem::Specification.new do |s| s.require_path = 'lib' s.requirements << 'none' - spree_version = '>= 3.2.0', '< 4.0.0' + spree_version = '>= 4.0.0' s.add_dependency 'spree_core', spree_version s.add_dependency 'spree_auth_devise' s.add_development_dependency 'capybara' s.add_development_dependency 'ffaker' - s.add_development_dependency 'rspec-rails', '~> 3.5.0' + s.add_development_dependency 'rspec-rails', '~> 4.0.0' s.add_development_dependency 'shoulda-matchers', '~> 3.1' s.add_development_dependency 'rspec-activemodel-mocks' s.add_development_dependency 'rails-controller-testing' @@ -37,5 +37,6 @@ Gem::Specification.new do |s| s.add_development_dependency 'simplecov' s.add_development_dependency 'byebug' s.add_development_dependency 'appraisal' + s.add_development_dependency 'spree_dev_tools' end From a52a30fd42131f252e5fe58921911dad1a34ef58 Mon Sep 17 00:00:00 2001 From: rishabh gupta Date: Mon, 15 Jun 2020 16:28:47 +0530 Subject: [PATCH 2/3] changed base_controller_Decorator and user_model_decorator --- .../user_decorator.rb | 6 ++-- ...20130709104101_create_spree_permissions.rb | 2 +- ...09104945_create_spree_roles_permissions.rb | 2 +- ...able_is_default_and_name_to_spree_roles.rb | 2 +- ...0503090436_create_spree_permission_sets.rb | 2 +- ...1013_create_spree_roles_permission_sets.rb | 2 +- ...reate_spree_permissions_permission_sets.rb | 2 +- ...tion_to_permissions_and_permission_sets.rb | 2 +- ..._add_display_boolean_to_permission_sets.rb | 2 +- ..._deprecate_legacy_roles_and_permissions.rb | 2 +- ..._remove_column_boolean_from_permissions.rb | 2 +- ...170509090346_add_admin_boolean_to_roles.rb | 2 +- .../spree/admin}/base_controller_decorator.rb | 6 ++-- lib/spree/permissions.rb | 4 +-- spec/models/spree/ability_decorator_spec.rb | 28 +++++++++---------- spec/spec_helper.rb | 1 - spree_admin_roles_and_access.gemspec | 1 - 17 files changed, 33 insertions(+), 35 deletions(-) rename {app/controllers/spree_admin_roles_and_access => lib/controllers/backend/spree/admin}/base_controller_decorator.rb (85%) diff --git a/app/models/spree_admin_roles_and_access/user_decorator.rb b/app/models/spree_admin_roles_and_access/user_decorator.rb index e9deb47..af5196e 100644 --- a/app/models/spree_admin_roles_and_access/user_decorator.rb +++ b/app/models/spree_admin_roles_and_access/user_decorator.rb @@ -1,7 +1,9 @@ module SpreeAdminRolesAndAccess module UserDecorator - alias_attribute :roles, :spree_roles + def self.prepended(base) + base.alias_attribute :roles, :spree_roles + end end end -Spree::User.prepend SpreeAdminRolesAndAccess::UserDecorator \ No newline at end of file +Spree.user_class.prepend SpreeAdminRolesAndAccess::UserDecorator \ No newline at end of file diff --git a/db/migrate/20130709104101_create_spree_permissions.rb b/db/migrate/20130709104101_create_spree_permissions.rb index 86c19f2..6f6c01c 100644 --- a/db/migrate/20130709104101_create_spree_permissions.rb +++ b/db/migrate/20130709104101_create_spree_permissions.rb @@ -1,4 +1,4 @@ -class CreateSpreePermissions < SpreeExtension::Migration[4.2] +class CreateSpreePermissions < ActiveRecord::Migration[4.2] def change create_table :spree_permissions do |t| t.string :title, null: false, unique: true diff --git a/db/migrate/20130709104945_create_spree_roles_permissions.rb b/db/migrate/20130709104945_create_spree_roles_permissions.rb index 8dfca9d..32cb61a 100644 --- a/db/migrate/20130709104945_create_spree_roles_permissions.rb +++ b/db/migrate/20130709104945_create_spree_roles_permissions.rb @@ -1,4 +1,4 @@ -class CreateSpreeRolesPermissions < SpreeExtension::Migration[4.2] +class CreateSpreeRolesPermissions < ActiveRecord::Migration[4.2] def change create_table :spree_roles_permissions, id: false do |t| t.integer :role_id, null: false diff --git a/db/migrate/20130709105614_add_editable_is_default_and_index_on_editable_is_default_and_name_to_spree_roles.rb b/db/migrate/20130709105614_add_editable_is_default_and_index_on_editable_is_default_and_name_to_spree_roles.rb index df6f6de..82a9ee9 100644 --- a/db/migrate/20130709105614_add_editable_is_default_and_index_on_editable_is_default_and_name_to_spree_roles.rb +++ b/db/migrate/20130709105614_add_editable_is_default_and_index_on_editable_is_default_and_name_to_spree_roles.rb @@ -1,4 +1,4 @@ -class AddEditableIsDefaultAndIndexOnEditableIsDefaultAndNameToSpreeRoles < SpreeExtension::Migration[4.2] +class AddEditableIsDefaultAndIndexOnEditableIsDefaultAndNameToSpreeRoles < ActiveRecord::Migration[4.2] def change add_column :spree_roles, :editable, :boolean, default: true add_column :spree_roles, :is_default, :boolean, default: false diff --git a/db/migrate/20170503090436_create_spree_permission_sets.rb b/db/migrate/20170503090436_create_spree_permission_sets.rb index f3eeb97..0a88077 100644 --- a/db/migrate/20170503090436_create_spree_permission_sets.rb +++ b/db/migrate/20170503090436_create_spree_permission_sets.rb @@ -1,4 +1,4 @@ -class CreateSpreePermissionSets < SpreeExtension::Migration[4.2] +class CreateSpreePermissionSets < ActiveRecord::Migration[4.2] def change create_table :spree_permission_sets do |t| t.string :name, null: false, unique: true diff --git a/db/migrate/20170503091013_create_spree_roles_permission_sets.rb b/db/migrate/20170503091013_create_spree_roles_permission_sets.rb index 1019459..253bd65 100644 --- a/db/migrate/20170503091013_create_spree_roles_permission_sets.rb +++ b/db/migrate/20170503091013_create_spree_roles_permission_sets.rb @@ -1,4 +1,4 @@ -class CreateSpreeRolesPermissionSets < SpreeExtension::Migration[4.2] +class CreateSpreeRolesPermissionSets < ActiveRecord::Migration[4.2] def change create_table :spree_roles_permission_sets do |t| t.references :role, index: true diff --git a/db/migrate/20170503101648_create_spree_permissions_permission_sets.rb b/db/migrate/20170503101648_create_spree_permissions_permission_sets.rb index 19f545f..10a35a8 100644 --- a/db/migrate/20170503101648_create_spree_permissions_permission_sets.rb +++ b/db/migrate/20170503101648_create_spree_permissions_permission_sets.rb @@ -1,4 +1,4 @@ -class CreateSpreePermissionsPermissionSets < SpreeExtension::Migration[4.2] +class CreateSpreePermissionsPermissionSets < ActiveRecord::Migration[4.2] def change create_table :spree_permissions_permission_sets do |t| t.references :permission, index: true diff --git a/db/migrate/20170508082615_add_description_to_permissions_and_permission_sets.rb b/db/migrate/20170508082615_add_description_to_permissions_and_permission_sets.rb index d55a8b7..3bdf9fe 100644 --- a/db/migrate/20170508082615_add_description_to_permissions_and_permission_sets.rb +++ b/db/migrate/20170508082615_add_description_to_permissions_and_permission_sets.rb @@ -1,4 +1,4 @@ -class AddDescriptionToPermissionsAndPermissionSets < SpreeExtension::Migration[4.2] +class AddDescriptionToPermissionsAndPermissionSets < ActiveRecord::Migration[4.2] def change add_column :spree_permissions, :description, :string, default: '' add_column :spree_permission_sets, :description, :string, default: '' diff --git a/db/migrate/20170508082722_add_display_boolean_to_permission_sets.rb b/db/migrate/20170508082722_add_display_boolean_to_permission_sets.rb index c022b7b..0de4517 100644 --- a/db/migrate/20170508082722_add_display_boolean_to_permission_sets.rb +++ b/db/migrate/20170508082722_add_display_boolean_to_permission_sets.rb @@ -1,4 +1,4 @@ -class AddDisplayBooleanToPermissionSets < SpreeExtension::Migration[4.2] +class AddDisplayBooleanToPermissionSets < ActiveRecord::Migration[4.2] def change add_column :spree_permission_sets, :display_permission, :boolean, default: false end diff --git a/db/migrate/20170508091139_deprecate_legacy_roles_and_permissions.rb b/db/migrate/20170508091139_deprecate_legacy_roles_and_permissions.rb index 6bdffa8..7d3371e 100644 --- a/db/migrate/20170508091139_deprecate_legacy_roles_and_permissions.rb +++ b/db/migrate/20170508091139_deprecate_legacy_roles_and_permissions.rb @@ -1,4 +1,4 @@ -class DeprecateLegacyRolesAndPermissions < SpreeExtension::Migration[4.2] +class DeprecateLegacyRolesAndPermissions < ActiveRecord::Migration[4.2] def display_warning ActiveSupport::Deprecation.warn('Direct relationship between roles and permissions is deprecated. Use #legacy_permissions to access old permissions') end diff --git a/db/migrate/20170509082144_remove_column_boolean_from_permissions.rb b/db/migrate/20170509082144_remove_column_boolean_from_permissions.rb index d63d0c6..fb85d99 100644 --- a/db/migrate/20170509082144_remove_column_boolean_from_permissions.rb +++ b/db/migrate/20170509082144_remove_column_boolean_from_permissions.rb @@ -1,4 +1,4 @@ -class RemoveColumnBooleanFromPermissions < SpreeExtension::Migration[4.2] +class RemoveColumnBooleanFromPermissions < ActiveRecord::Migration[4.2] def change remove_column :spree_permissions, :boolean, :boolean, default: true end diff --git a/db/migrate/20170509090346_add_admin_boolean_to_roles.rb b/db/migrate/20170509090346_add_admin_boolean_to_roles.rb index 2bfc83d..80b2b9c 100644 --- a/db/migrate/20170509090346_add_admin_boolean_to_roles.rb +++ b/db/migrate/20170509090346_add_admin_boolean_to_roles.rb @@ -1,4 +1,4 @@ -class AddAdminBooleanToRoles < SpreeExtension::Migration[4.2] +class AddAdminBooleanToRoles < ActiveRecord::Migration[4.2] def change add_column :spree_roles, :admin_accessible, :boolean, default: false end diff --git a/app/controllers/spree_admin_roles_and_access/base_controller_decorator.rb b/lib/controllers/backend/spree/admin/base_controller_decorator.rb similarity index 85% rename from app/controllers/spree_admin_roles_and_access/base_controller_decorator.rb rename to lib/controllers/backend/spree/admin/base_controller_decorator.rb index 45a5dde..848b4d6 100644 --- a/app/controllers/spree_admin_roles_and_access/base_controller_decorator.rb +++ b/lib/controllers/backend/spree/admin/base_controller_decorator.rb @@ -1,4 +1,4 @@ -module SpreeAdminRolesAndAccess +module Spree::Admin module BaseControllerDecorator def authorize_admin begin @@ -26,6 +26,4 @@ def new_action? NEW_ACTIONS.include?(params[:action].to_sym) end end -end - -Spree::Admin::BaseController.prepend SpreeAdminRolesAndAccess::BaseControllerDecorator \ No newline at end of file +Spree::Admin::BaseController.prepend Spree::Admin::BaseControllerDecorator \ No newline at end of file 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| diff --git a/spec/models/spree/ability_decorator_spec.rb b/spec/models/spree/ability_decorator_spec.rb index fd78816..6834063 100644 --- a/spec/models/spree/ability_decorator_spec.rb +++ b/spec/models/spree/ability_decorator_spec.rb @@ -26,7 +26,7 @@ let(:permission17) { Spree::Permission.create(title: 'can-create-spree/products', priority: 3) } let(:permission_set) { Spree::PermissionSet.create!(name: 'test') } - let(:user) { Spree::User.create!(email: 'abc@test.com', password: '123456') } + let(:user) { Spree.user_class.create!(email: 'abc@test.com', password: '123456') } let(:role) { Spree::Role.where(name: 'user').first_or_create! } let(:roles) { [role] } @@ -125,8 +125,8 @@ it_should_behave_like 'access denied' it_should_behave_like 'no index allowed' it_should_behave_like 'default admin permissions' - it { expect(new_ability).to_not be_able_to :create, Spree::User, :role_ids } - it { expect(new_ability).to_not be_able_to :update, Spree::User, :role_ids } + it { expect(new_ability).to_not be_able_to :create, Spree.user_class, :role_ids } + it { expect(new_ability).to_not be_able_to :update, Spree.user_class, :role_ids } end context 'with warehouse_admin user' do @@ -166,11 +166,11 @@ let(:resource) { Object.new } let(:resource_shipment) { Spree::Shipment.new } let(:resource_product) { Spree::Product.new } - let(:resource_user) { Spree::User.new } + let(:resource_user) { Spree.user_class.new } let(:resource_order) { Spree::Order.new } - let(:fakedispatch_user) { Spree::User.new } + let(:fakedispatch_user) { Spree.user_class.new } let(:admin_role) { Spree::Role.where(name: 'admin').first_or_create! } - let(:user1) { Spree::User.new } + let(:user1) { Spree.user_class.new } let(:ability) { Spree::Ability.new(user) } context 'with admin user' do @@ -262,7 +262,7 @@ it_should_behave_like 'access granted' end context 'requested by other user' do - let(:resource) { Spree::User.new } + let(:resource) { Spree.user_class.new } it_should_behave_like 'create only' end end @@ -276,7 +276,7 @@ end context 'requested by other user' do - before(:each) { resource.user = Spree::User.new } + before(:each) { resource.user = Spree.user_class.new } it_should_behave_like 'create only' end @@ -325,12 +325,12 @@ end it 'should receive new on Spree::User when there is no user passed' do - expect(Spree::User).to receive(:new).and_return(user) + expect(Spree.user_class).to receive(:new).and_return(user) Spree::Ability.new(nil) end it 'should not receive new on Spree::User when there is no user passed' do - expect(Spree::User).to_not receive(:new) + expect(Spree.user_class).to_not receive(:new) Spree::Ability.new(user) end @@ -359,10 +359,10 @@ subject { ability } - it { expect(subject).to be_able_to :create, Spree::User.new } - it { expect(subject).to be_able_to :update, Spree::User.new } - it { expect(subject).to_not be_able_to :create, Spree::User.new, :role_ids } - it { expect(subject).to_not be_able_to :update, Spree::User.new, :role_ids } + it { expect(subject).to be_able_to :create, Spree.user_class.new } + it { expect(subject).to be_able_to :update, Spree.user_class.new } + it { expect(subject).to_not be_able_to :create, Spree.user_class.new, :role_ids } + it { expect(subject).to_not be_able_to :update, Spree.user_class.new, :role_ids } end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4e54552..5fc97ae 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -29,7 +29,6 @@ require 'spree/testing_support/controller_requests' require 'spree/testing_support/authorization_helpers' require 'spree/testing_support/url_helpers' -require 'spree_dev_tools/rspec/spec_helper' RSpec.configure do |config| # == Requests support diff --git a/spree_admin_roles_and_access.gemspec b/spree_admin_roles_and_access.gemspec index 5973735..1e38374 100644 --- a/spree_admin_roles_and_access.gemspec +++ b/spree_admin_roles_and_access.gemspec @@ -37,6 +37,5 @@ Gem::Specification.new do |s| s.add_development_dependency 'simplecov' s.add_development_dependency 'byebug' s.add_development_dependency 'appraisal' - s.add_development_dependency 'spree_dev_tools' end From 6d3828db88c89505badc0227d96d56257f8d37b3 Mon Sep 17 00:00:00 2001 From: Rishabh Gupta <55984752+rishabhguptarishi@users.noreply.github.com> Date: Tue, 30 Jun 2020 17:00:12 +0530 Subject: [PATCH 3/3] added heroku button for spree 4.1 --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index cf5bac0..456733d 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ Try Spree Admin Roles and Access for Spree master with direct deployment on Hero [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/vinsol-spree-contrib/spree-demo-heroku/tree/spree-admin-roles-and-access-master) +Try Spree Admin Roles and Access for Spree 4-1 with direct deployment on Heroku: + +[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/vinsol-spree-contrib/spree-demo-heroku/tree/spree-admin-roles-and-access-4-1) + Try Spree Admin Roles and Access for Spree 3-4 with direct deployment on Heroku: [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/vinsol-spree-contrib/spree-demo-heroku/tree/spree-admin-roles-and-access-3-4)