Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changed decorator files and updated migrations #62

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 0 additions & 27 deletions app/controllers/spree/admin/base_controller_decorator.rb

This file was deleted.

18 changes: 0 additions & 18 deletions app/models/spree/role_decorator.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/spree/user_decorator.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module Spree
Ability.class_eval do

module SpreeAdminRolesAndAccess
module AbilityDecorator
def initialize(user)
self.clear_aliased_actions

Expand All @@ -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
Expand All @@ -26,3 +25,5 @@ def user_roles(user)
end
end
end

Spree::Ability.prepend SpreeAdminRolesAndAccess::AbilityDecorator
24 changes: 24 additions & 0 deletions app/models/spree_admin_roles_and_access/role_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions app/models/spree_admin_roles_and_access/user_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module SpreeAdminRolesAndAccess
module UserDecorator
def self.prepended(base)
base.alias_attribute :roles, :spree_roles
end
end
end

Spree.user_class.prepend SpreeAdminRolesAndAccess::UserDecorator
29 changes: 29 additions & 0 deletions lib/controllers/backend/spree/admin/base_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Spree::Admin
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
Spree::Admin::BaseController.prepend Spree::Admin::BaseControllerDecorator
4 changes: 2 additions & 2 deletions lib/spree/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
28 changes: 14 additions & 14 deletions spec/models/spree/ability_decorator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]', password: '123456') }
let(:user) { Spree.user_class.create!(email: '[email protected]', password: '123456') }
let(:role) { Spree::Role.where(name: 'user').first_or_create! }
let(:roles) { [role] }

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions spree_admin_roles_and_access.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ Gem::Specification.new do |s|
s.require_path = 'lib'
s.requirements << 'none'

spree_version = '>= 3.2.0', '< 4.0.0'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets discuss

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'
Expand Down