Update scope query for new teacher definition

This commit is contained in:
Sebastian Serth
2022-09-20 11:31:25 +02:00
committed by Sebastian Serth
parent 964048927a
commit 02c65af034
4 changed files with 18 additions and 10 deletions

View File

@ -68,7 +68,7 @@ class InternalUsersController < ApplicationController
def index
@search = InternalUser.ransack(params[:q], {auth_object: current_user})
@users = @search.result.includes(:consumer).order(:name).paginate(page: params[:page], per_page: per_page_param)
@users = @search.result.in_study_group_of(current_user).includes(:consumer).order(:name).paginate(page: params[:page], per_page: per_page_param)
authorize!
end

View File

@ -46,13 +46,14 @@ class ExercisePolicy < AdminOrAuthorPolicy
if @user.admin?
@scope.all
elsif @user.teacher?
@scope.where(
'exercises.user_id IN (SELECT user_id FROM study_group_memberships WHERE study_group_id IN (?))
OR (exercises.user_id = ? AND exercises.user_type = ?)
OR public = TRUE',
@user.study_groups.pluck(:id),
@user.id, @user.class.name
)
@scope.distinct
.joins('LEFT OUTER JOIN study_group_memberships ON exercises.user_type = study_group_memberships.user_type AND exercises.user_id = study_group_memberships.user_id')
# The exercise's author is a teacher in the study group
.where(study_group_memberships: {role: StudyGroupMembership.roles[:teacher]})
# The current user is a teacher in the *same* study group
.where(study_group_memberships: {study_group_id: @user.study_group_memberships.where(role: :teacher).select(:study_group_id)})
.or(@scope.distinct.where(user: @user))
.or(@scope.distinct.where(public: true))
else
@scope.none
end

View File

@ -18,7 +18,14 @@ class ProxyExercisePolicy < AdminOrAuthorPolicy
if @user.admin?
@scope.all
elsif @user.teacher?
@scope.where('user_id = ? OR public = TRUE', @user.id)
@scope.distinct
.joins('LEFT OUTER JOIN study_group_memberships ON proxy_exercises.user_type = study_group_memberships.user_type AND proxy_exercises.user_id = study_group_memberships.user_id')
# The proxy_exercise's author is a teacher in the study group
.where(study_group_memberships: {role: StudyGroupMembership.roles[:teacher]})
# The current user is a teacher in the *same* study group
.where(study_group_memberships: {study_group_id: @user.study_group_memberships.where(role: :teacher).select(:study_group_id)})
.or(@scope.distinct.where(user: @user))
.or(@scope.distinct.where(public: true))
else
@scope.none
end

View File

@ -21,7 +21,7 @@ class StudyGroupPolicy < AdminOnlyPolicy
if @user.admin?
@scope.all
elsif @user.teacher?
@scope.joins(:study_group_memberships).where('user_id = ? AND user_type = ?', @user.id, @user.class.name)
@scope.joins(:study_group_memberships).where(study_group_memberships: {user: @user, role: StudyGroupMembership.roles[:teacher]})
else
@scope.none
end