mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-26 01:33:22 +00:00
Merge pull request #5925 from luisramos0/spree_core_user
[Bye bye spree] Bring classes related to users to OFN
This commit is contained in:
8
app/models/spree/role.rb
Normal file
8
app/models/spree/role.rb
Normal 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
|
||||
@@ -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?
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user