Merge pull request #5925 from luisramos0/spree_core_user

[Bye bye spree] Bring classes related to users to OFN
This commit is contained in:
Luis Ramos
2020-09-15 17:13:27 +01:00
committed by GitHub
4 changed files with 31 additions and 29 deletions

8
app/models/spree/role.rb Normal file
View File

@@ -0,0 +1,8 @@
# frozen_string_literal: true
module Spree
class Role < ActiveRecord::Base
has_and_belongs_to_many :users, join_table: 'spree_roles_users',
class_name: Spree.user_class.to_s
end
end

View File

@@ -7,6 +7,13 @@ module Spree
belongs_to :ship_address, foreign_key: 'ship_address_id', class_name: 'Spree::Address'
belongs_to :bill_address, foreign_key: 'bill_address_id', class_name: 'Spree::Address'
has_and_belongs_to_many :spree_roles,
join_table: 'spree_roles_users',
foreign_key: "user_id",
class_name: "Spree::Role"
has_many :spree_orders, foreign_key: "user_id", class_name: "Spree::Order"
before_validation :set_login
before_destroy :check_completed_orders
@@ -41,6 +48,12 @@ module Spree
User.admin.count > 0
end
# Whether a user has a role or not.
def has_spree_role?(role_in_question)
spree_roles.where(name: role_in_question.to_s).any?
end
# Checks whether the specified user is a superadmin, with full control of the instance
def admin?
has_spree_role?('admin')
end
@@ -107,14 +120,6 @@ module Spree
end
end
# Checks whether the specified user is a superadmin, with full control of the
# instance
#
# @return [Boolean]
def superadmin?
has_spree_role?('admin')
end
def generate_spree_api_key!
self.spree_api_key = SecureRandom.hex(24)
save!
@@ -125,6 +130,10 @@ module Spree
save!
end
def last_incomplete_spree_order
spree_orders.incomplete.where(created_by_id: id).order('created_at DESC').first
end
protected
def password_required?

View File

@@ -1,7 +1,7 @@
Openfoodnetwork::Application.routes.draw do
namespace :admin do
authenticated :spree_user, -> user { user.superadmin? } do
authenticated :spree_user, -> user { user.admin? } do
mount DelayedJobWeb, at: '/delayed_job'
end

View File

@@ -167,31 +167,16 @@ describe Spree.user_class do
end
end
describe '#superadmin?' do
let(:user) { create(:user) }
context 'when the user has an admin spree role' do
before { user.spree_roles << Spree::Role.create(name: 'admin') }
it 'returns true' do
expect(user.superadmin?).to eq(true)
end
describe '#admin?' do
it 'returns true when the user has an admin spree role' do
expect(create(:admin_user).admin?).to be_truthy
end
context 'when the user does not have an admin spree role' do
it 'returns false' do
expect(user.superadmin?).to eq(false)
end
it 'returns false when the user does not have an admin spree role' do
expect(create(:user).admin?).to eq(false)
end
end
before(:all) { Spree::Role.create name: 'admin' }
it '#admin?' do
expect(create(:admin_user).admin?).to be_truthy
expect(create(:user).admin?).to be_falsey
end
context '#destroy' do
it 'can not delete if it has completed orders' do
order = build(:order, completed_at: Time.zone.now)