Skip to content

Commit bcbc420

Browse files
committed
Update School.find_for_user!
This fixes a problem in editor-standalone where previously a user that had created a school, but didn't yet have a role in the school, would be allowed to go through the school onboarding flow a second time. The method changed in this commit is used by the `MySchoolController#show` action (exposed at `/api/school`) to return the school associated with the authenticated user. The `/api/school` endpoint will now return either the school that the user created, or that they have a role in. This matches the behaviour of the `SchoolsController` which uses our Ability class to determine which school(s) a user can view. Our ability class includes a rule that allows users to view unverified schools they created[1]. [1]: https://github.com/RaspberryPiFoundation/editor-api/blob/20fbcaddfbe4a320836f3dbd2d274406396c2c57/app/models/ability.rb#L29
1 parent d4b1f2a commit bcbc420

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

app/models/school.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class School < ApplicationRecord
2121
before_validation :normalize_reference
2222

2323
def self.find_for_user!(user)
24-
Role.find_by!(user_id: user.id).school
24+
school = Role.find_by(user_id: user.id)&.school || find_by(creator_id: user.id)
25+
raise ActiveRecord::RecordNotFound unless school
26+
27+
school
2528
end
2629

2730
def creator

spec/models/school_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,12 @@
199199
expect(described_class.find_for_user!(user)).to eq(school)
200200
end
201201

202+
it "returns the school that the user created if they don't have a role in any school" do
203+
creator = create(:user)
204+
school.update!(creator_id: creator.id)
205+
expect(described_class.find_for_user!(creator)).to eq(school)
206+
end
207+
202208
it "raises ActiveRecord::RecordNotFound if the user doesn't have a role in a school" do
203209
user = build(:user)
204210
expect { described_class.find_for_user!(user) }.to raise_error(ActiveRecord::RecordNotFound)

0 commit comments

Comments
 (0)