Skip to content

Commit 67576db

Browse files
tuzzfloehopper
authored andcommitted
Validate that the Project#user belongs to the same school as the project
1 parent 29ef33e commit 67576db

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

app/models/project.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Project < ApplicationRecord
1616
validates :identifier, presence: true, uniqueness: { scope: :locale }
1717
validate :identifier_cannot_be_taken_by_another_user
1818
validates :locale, presence: true, unless: :user_id
19+
validate :user_has_a_role_within_the_school
1920

2021
scope :internal_projects, -> { where(user_id: nil) }
2122

@@ -49,4 +50,16 @@ def identifier_cannot_be_taken_by_another_user
4950

5051
errors.add(:identifier, "can't be taken by another user")
5152
end
53+
54+
def user_has_a_role_within_the_school
55+
return unless user_id_changed? && errors.blank? && school
56+
57+
_, user = with_user
58+
59+
return if user.blank?
60+
return if user.org_roles(organisation_id: school.id).any?
61+
62+
msg = "'#{user_id}' does not have any roles for for organisation '#{school.id}'"
63+
errors.add(:user, msg)
64+
end
5265
end

app/models/user.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ def attributes
2727
ATTRIBUTES.index_with { |_k| nil }
2828
end
2929

30-
def role?(role:)
31-
return false if roles.nil?
32-
33-
roles.to_s.split(',').map(&:strip).include? role.to_s
34-
end
35-
3630
def organisation_ids
3731
organisations&.keys || []
3832
end
@@ -58,7 +52,7 @@ def school_student?(organisation_id:)
5852
end
5953

6054
def admin?
61-
role?(role: 'editor-admin')
55+
organisation_ids.any? { |organisation_id| org_role?(organisation_id:, role: 'editor-admin') }
6256
end
6357

6458
def ==(other)

spec/factories/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
organisations { {} }
99

1010
factory :admin_user do
11-
roles { 'editor-admin' }
11+
organisations { { '12345678-1234-1234-1234-123456789abc' => 'editor-admin' } }
1212
end
1313

1414
skip_create

spec/features/project/creating_a_project_spec.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
RSpec.describe 'Creating a project', type: :request do
66
before do
77
stub_hydra_public_api
8+
stub_user_info_api
89
mock_phrase_generation
910
end
1011

spec/models/project_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@
2525
let(:project) { create(:project) }
2626
let(:identifier) { project.identifier }
2727

28+
it 'has a valid default factory' do
29+
expect(build(:project)).to be_valid
30+
end
31+
32+
it 'can save the default factory' do
33+
expect { build(:project).save! }.not_to raise_error
34+
end
35+
2836
it 'is invalid if no user or locale' do
2937
invalid_project = build(:project, locale: nil, user_id: nil)
3038
expect(invalid_project).to be_invalid
@@ -62,6 +70,17 @@
6270
expect(new_project).to be_invalid
6371
end
6472
end
73+
74+
context 'when the project has a school' do
75+
before do
76+
project.update!(school: create(:school))
77+
end
78+
79+
it 'requires a user that has a role within the school' do
80+
project.user_id = SecureRandom.uuid
81+
expect(project).to be_invalid
82+
end
83+
end
6584
end
6685

6786
describe 'check_unique_not_null', :sample_words do

0 commit comments

Comments
 (0)