2019-06-27 16:26:07 +02:00
|
|
|
class ChampPolicy < ApplicationPolicy
|
2020-06-24 15:27:41 +02:00
|
|
|
# Scope for WRITING to a champ.
|
|
|
|
#
|
|
|
|
# (If the need for a scope to READ a champ emerges, we can implement another scope
|
|
|
|
# in this file, following this example: https://github.com/varvet/pundit/issues/368#issuecomment-196111115)
|
2019-10-07 18:06:55 +02:00
|
|
|
class Scope < ApplicationScope
|
2019-06-27 16:26:07 +02:00
|
|
|
def resolve
|
2019-10-08 14:28:26 +02:00
|
|
|
if user.blank?
|
|
|
|
return scope.none
|
2019-06-27 16:26:07 +02:00
|
|
|
end
|
2019-10-08 14:28:26 +02:00
|
|
|
|
2020-04-01 16:05:54 +02:00
|
|
|
# The join must be the same for all elements of the WHERE clause.
|
2020-04-01 16:10:45 +02:00
|
|
|
#
|
|
|
|
# NB: here we want to do `.left_outer_joins(dossier: [:invites, { :groupe_instructeur: :instructeurs }]))`,
|
|
|
|
# but for some reasons ActiveRecord <= 5.2 generates bogus SQL. Hence the manual version of it below.
|
2020-04-01 16:05:54 +02:00
|
|
|
joined_scope = scope
|
2020-04-01 16:10:45 +02:00
|
|
|
.joins('LEFT OUTER JOIN dossiers ON dossiers.id = champs.dossier_id')
|
2023-05-24 16:53:51 +02:00
|
|
|
.joins('LEFT OUTER JOIN invites ON invites.dossier_id = dossiers.id OR invites.dossier_id = dossiers.editing_fork_origin_id')
|
2020-04-01 16:10:45 +02:00
|
|
|
.joins('LEFT OUTER JOIN groupe_instructeurs ON groupe_instructeurs.id = dossiers.groupe_instructeur_id')
|
|
|
|
.joins('LEFT OUTER JOIN assign_tos ON assign_tos.groupe_instructeur_id = groupe_instructeurs.id')
|
|
|
|
.joins('LEFT OUTER JOIN instructeurs ON instructeurs.id = assign_tos.instructeur_id')
|
2020-04-01 16:05:54 +02:00
|
|
|
|
|
|
|
# Users can access public champs on their own dossiers.
|
|
|
|
resolved_scope = joined_scope
|
2019-10-08 14:28:26 +02:00
|
|
|
.where('dossiers.user_id': user.id, private: false)
|
|
|
|
|
2020-04-01 16:10:45 +02:00
|
|
|
# Invited users can access public champs on dossiers they are invited to
|
|
|
|
invite_clause = joined_scope
|
|
|
|
.where('invites.user_id': user.id, private: false)
|
|
|
|
resolved_scope = resolved_scope.or(invite_clause)
|
|
|
|
|
2019-10-08 14:28:26 +02:00
|
|
|
if instructeur.present?
|
|
|
|
# Additionnaly, instructeurs can access private champs
|
|
|
|
# on dossiers they are allowed to instruct.
|
2020-04-01 16:05:54 +02:00
|
|
|
instructeur_clause = joined_scope
|
2019-10-08 14:28:26 +02:00
|
|
|
.where('instructeurs.id': instructeur.id, private: true)
|
|
|
|
resolved_scope = resolved_scope.or(instructeur_clause)
|
|
|
|
end
|
|
|
|
|
2022-05-07 17:49:27 +02:00
|
|
|
resolved_scope.or(joined_scope.where('dossiers.for_procedure_preview': true))
|
2019-06-27 16:26:07 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|