Skip to content

Commit 32c9e86

Browse files
committed
Introduce permission for creating public projects
I plan to use this to restrict creating public projects to experience-cs admin users to be able to create public projects.
1 parent 8613b26 commit 32c9e86

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

app/models/ability.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ def initialize(user)
1414
define_school_teacher_abilities(user:, school:) if user.school_teacher?(school)
1515
define_school_owner_abilities(school:) if user.school_owner?(school)
1616
end
17+
18+
define_experience_cs_admin_abilities(user)
1719
end
1820

1921
private
@@ -100,6 +102,10 @@ def define_school_student_abilities(user:, school:)
100102
can(%i[show_finished set_finished], SchoolProject, project: { user_id: user.id, lesson_id: nil }, school_id: school.id)
101103
end
102104

105+
def define_experience_cs_admin_abilities(user)
106+
can :create, :public_project if user.experience_cs_admin?
107+
end
108+
103109
def school_teacher_can_manage_lesson?(user:, school:, lesson:)
104110
is_my_lesson = lesson.school_id == school.id && lesson.user_id == user.id
105111
is_my_class = lesson.school_class&.teacher_ids&.include?(user.id)

spec/factories/user.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
roles { 'editor-admin' }
1212
end
1313

14+
factory :experience_cs_admin_user do
15+
roles { 'experience-cs-admin' }
16+
end
17+
1418
factory :student do
1519
email { nil }
1620
username { Faker::Internet.username }

spec/models/ability_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
it { is_expected.not_to be_able_to(:update, project) }
3030
it { is_expected.not_to be_able_to(:destroy, project) }
3131
end
32+
33+
it { is_expected.not_to be_able_to(:create, :public_project) }
3234
end
3335

3436
context 'with a standard user' do
@@ -56,6 +58,8 @@
5658
it { is_expected.not_to be_able_to(:update, another_project) }
5759
it { is_expected.not_to be_able_to(:destroy, another_project) }
5860
end
61+
62+
it { is_expected.not_to be_able_to(:create, :public_project) }
5963
end
6064

6165
context 'with a teacher' do
@@ -83,6 +87,8 @@
8387
it { is_expected.not_to be_able_to(:update, another_project) }
8488
it { is_expected.not_to be_able_to(:destroy, another_project) }
8589
end
90+
91+
it { is_expected.not_to be_able_to(:create, :public_project) }
8692
end
8793

8894
context 'with an owner' do
@@ -110,6 +116,8 @@
110116
it { is_expected.not_to be_able_to(:update, another_project) }
111117
it { is_expected.not_to be_able_to(:destroy, another_project) }
112118
end
119+
120+
it { is_expected.not_to be_able_to(:create, :public_project) }
113121
end
114122

115123
context 'with a student' do
@@ -137,6 +145,14 @@
137145
it { is_expected.not_to be_able_to(:update, another_project) }
138146
it { is_expected.not_to be_able_to(:destroy, another_project) }
139147
end
148+
149+
it { is_expected.not_to be_able_to(:create, :public_project) }
150+
end
151+
152+
context 'with an experience-cs admin' do
153+
let(:user) { build(:experience_cs_admin_user) }
154+
155+
it { is_expected.to be_able_to(:create, :public_project) }
140156
end
141157

142158
# rubocop:disable RSpec/MultipleMemoizedHelpers

0 commit comments

Comments
 (0)