Skip to content

Commit

Permalink
Remove current organisation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jamie committed Oct 4, 2024
1 parent 7fdb2e9 commit c5eae01
Show file tree
Hide file tree
Showing 21 changed files with 52 additions and 87 deletions.
15 changes: 0 additions & 15 deletions app/controllers/placements/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@ class Placements::ApplicationController < ApplicationController
after_action :verify_policy_scoped, if: ->(c) { c.action_name == "index" }
before_action :authorize_support_user!

def current_user
@current_user ||= sign_in_user&.user&.tap do |user|
organisation_id = session.dig("current_organisation", "id")
organisation_type = session.dig("current_organisation", "type")
organisation = user.user_memberships.find_by(organisation_id:, organisation_type:)&.organisation

user.current_organisation = case organisation
when School
organisation.becomes(Placements::School)
when Provider
organisation.becomes(Placements::Provider)
end
end
end

private

def authorize_support_user!
Expand Down
5 changes: 0 additions & 5 deletions app/controllers/placements/organisations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,9 @@ def memberships
def load_organisation(membership)
organisation = membership.organisation

set_current_organisation(organisation)
redirect_to landing_page_path(organisation)
end

def set_current_organisation(organisation)
session["current_organisation"] = { "id" => organisation.id, "type" => organisation.class.name }
end

def landing_page_path(organisation)
if organisation.is_a?(School)
placements_school_placements_path(organisation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ def index
@schools = schools_scope.order_by_name.select(:id, :name)
@year_groups ||= Placement.year_groups_as_options
@terms = Placements::Term.order_by_term.select(:id, :name)
scope = policy_scope(Placements::PlacementsQuery.call(params: query_params))
scope = policy_scope(
Placements::PlacementsQuery.call(params: query_params),
policy_scope_class: Placements::Provider::PlacementPolicy::Scope,
)

@pagy, @placements = pagy(scope)
end
Expand Down
2 changes: 0 additions & 2 deletions app/models/placements/support_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,4 @@
#
class Placements::SupportUser < User
include ActsAsSupportUser

attribute :current_organisation
end
2 changes: 0 additions & 2 deletions app/models/placements/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class Placements::User < User
source: :organisation,
source_type: "Provider"

attribute :current_organisation

def service
:placements
end
Expand Down
10 changes: 3 additions & 7 deletions app/policies/placement_policy.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
class PlacementPolicy < ApplicationPolicy
class Scope < ApplicationPolicy::Scope
def resolve
if user.current_organisation.is_a?(Placements::School)
scope.where(school: user.current_organisation)
elsif user.current_organisation.is_a?(Placements::Provider) || user.support_user?
scope
else
scope.none
end
return scope if user.support_user?

scope.where(school: user.schools)
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/policies/placements/mentor_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Scope < ApplicationPolicy::Scope
def resolve
return scope if user.support_user?

scope.where(id: Placements::MentorMembership.select(:mentor_id).where(school: user.current_organisation))
scope.where(id: Placements::MentorMembership.select(:mentor_id).where(school: user.schools))
end
end
end
7 changes: 7 additions & 0 deletions app/policies/placements/provider/placement_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Placements::Provider::PlacementPolicy < ApplicationPolicy
class Scope < ApplicationPolicy::Scope
def resolve
scope
end
end
end
10 changes: 5 additions & 5 deletions app/policies/placements/user_policy.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
class Placements::UserPolicy < ApplicationPolicy
class Scope < ApplicationPolicy::Scope
def resolve
if user.support_user?
scope
else
scope.where(id: UserMembership.select(:user_id).where(organisation: user.current_organisation))
end
return scope if user.support_user?

memberships = UserMembership.where(organisation: user.schools)
.or(UserMembership.where(organisation: user.providers))
scope.where(id: memberships.select(:user_id))
end
end
end
2 changes: 1 addition & 1 deletion app/policies/provider_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Scope < ApplicationPolicy::Scope
def resolve
return scope if user.support_user?

scope.where(id: user.current_organisation.partner_providers.select(:id))
scope.where(id: Placements::Partnership.select(:provider_id).where(school: user.schools))
end
end
end
6 changes: 1 addition & 5 deletions app/policies/school_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ class Scope < ApplicationPolicy::Scope
def resolve
return scope if user.support_user?

if user.current_organisation.is_a?(Placements::School)
scope.where(id: user.current_organisation.partner_providers.select(:id))
else
scope.where(id: user.current_organisation.partner_schools.select(:id))
end
scope.where(id: Placements::Partnership.select(:school_id).where(provider: user.providers))
end
end
end
1 change: 0 additions & 1 deletion config/analytics_blocklist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ shared:
- first_name
- last_name
- email
- current_organisation
:mentors:
- first_name
- last_name
Expand Down
4 changes: 0 additions & 4 deletions spec/models/placements/support_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@
require "rails_helper"

RSpec.describe Placements::SupportUser do
describe "attributes" do
it { is_expected.to have_attributes(current_organisation: nil) }
end

context "with validations" do
subject { build(:placements_support_user) }

Expand Down
4 changes: 0 additions & 4 deletions spec/models/placements/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
end
end

describe "attributes" do
it { is_expected.to have_attributes(current_organisation: nil) }
end

describe "default scope" do
let(:email) { "[email protected]" }
let!(:user_with_placements_service) { create(:placements_user, email:) }
Expand Down
22 changes: 0 additions & 22 deletions spec/policies/placement_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,34 +67,12 @@
let(:placement_2) { create(:placement) }

before do
user.current_organisation = school
placement_2
end

it "returns the school's placements" do
expect(placement_policy::Scope.new(user, scope).resolve).to eq([placement_1])
end
end

context "when the user is a provider user" do
let(:user) { create(:placements_user, providers: [provider]) }
let(:provider) { create(:placements_provider) }

before do
user.current_organisation = provider
end

it "returns the provider's placements" do
expect(placement_policy::Scope.new(user, scope).resolve).to eq(scope)
end
end

context "when the user is none of the above" do
let(:user) { create(:placements_user) }

it "returns no placements" do
expect(placement_policy::Scope.new(user, scope).resolve).to eq(scope.none)
end
end
end
end
1 change: 0 additions & 1 deletion spec/policies/placements/mentor_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
let(:mentor_2) { create(:placements_mentor) }

before do
user.current_organisation = school
mentor_2
end

Expand Down
30 changes: 30 additions & 0 deletions spec/policies/placements/provider/placement_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require "rails_helper"

RSpec.describe Placements::Provider::PlacementPolicy do
subject(:placement_policy) { described_class }

describe "scope" do
let(:scope) { Placement.all }

before do
create_list(:placement, 3)
end

context "when the user is a support user" do
let(:user) { create(:placements_support_user) }

it "returns all placements" do
expect(placement_policy::Scope.new(user, scope).resolve).to eq(scope)
end
end

context "when the user is a provider user" do
let(:provider) { build(:placements_provider) }
let(:user) { create(:placements_user, providers: [provider]) }

it "returns the school's placements" do
expect(placement_policy::Scope.new(user, scope).resolve).to eq(scope)
end
end
end
end
1 change: 0 additions & 1 deletion spec/policies/placements/user_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
let(:user_2) { create(:placements_user) }

before do
user_1.current_organisation = school
user_2
end

Expand Down
1 change: 0 additions & 1 deletion spec/policies/provider_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
let(:provider_2) { create(:placements_provider) }

before do
user.current_organisation = school
provider_2
end

Expand Down
8 changes: 0 additions & 8 deletions spec/policies/school_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@
let(:user) { create(:placements_user, schools: [school]) }
let(:school) { create(:placements_school) }

before do
user.current_organisation = school
end

it "returns the school's partner providers" do
expect(school_policy::Scope.new(user, scope).resolve).to eq(school.partner_providers)
end
Expand All @@ -31,10 +27,6 @@
let(:user) { create(:placements_user, providers: [provider]) }
let(:provider) { create(:placements_provider) }

before do
user.current_organisation = provider
end

it "returns the provider's partner schools" do
expect(school_policy::Scope.new(user, scope).resolve).to eq(provider.partner_schools)
end
Expand Down
1 change: 0 additions & 1 deletion spec/policies/user_membership_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
let(:user_membership_2) { create(:user_membership) }

before do
user.current_organisation = school
user_membership_2
end

Expand Down

0 comments on commit c5eae01

Please sign in to comment.