Add AuthenticationToken to UserMailer.exercise_anomaly_detected

This commit is contained in:
Sebastian Serth
2024-04-18 16:03:40 +02:00
committed by Sebastian Serth
parent 0a379721a1
commit 8c506cd47c
4 changed files with 26 additions and 19 deletions

View File

@ -54,8 +54,25 @@ class UserMailer < ApplicationMailer
end end
def exercise_anomaly_detected(exercise_collection, anomalies) def exercise_anomaly_detected(exercise_collection, anomalies)
# First, we try to find the best matching study group for the user being notified.
# The study group is relevant, since it determines the access rights to the exercise within the collection.
# The best matching study group is the one that grants access to the most exercises in the collection.
# This approach might look complicated, but since it is called from a Rake task and no active user session, we need it.
@relevant_exercises = Exercise.where(id: anomalies.keys)
potential_study_groups = exercise_collection.user.study_groups.where(study_group_memberships: {role: StudyGroupMembership.roles[:teacher]})
potential_study_groups_with_expected_access = potential_study_groups.to_h do |study_group|
exercises_granting_access = @relevant_exercises.count do |exercise|
author_study_groups = exercise.author.study_groups.where(study_group_memberships: {role: StudyGroupMembership.roles[:teacher]})
author_study_groups.include?(study_group)
end
[study_group, exercises_granting_access]
end
best_matching_study_group = potential_study_groups_with_expected_access.max_by {|_study_group, exercises_granting_access| exercises_granting_access }.first
# Second, all relevant values are passed to the view
@user = exercise_collection.user @user = exercise_collection.user
@receiver_displayname = exercise_collection.user.displayname @receiver_displayname = @user.displayname
@token = AuthenticationToken.generate!(@user, best_matching_study_group).shared_secret
@collection = exercise_collection @collection = exercise_collection
@anomalies = anomalies @anomalies = anomalies
mail(subject: t('mailers.user_mailer.exercise_anomaly_detected.subject'), to: exercise_collection.user.email) mail(subject: t('mailers.user_mailer.exercise_anomaly_detected.subject'), to: exercise_collection.user.email)

View File

@ -9,12 +9,11 @@ table(border=1)
td = t('exercises.statistics.average_worktime', locale: :de) td = t('exercises.statistics.average_worktime', locale: :de)
td = t('shared.actions', locale: :de) td = t('shared.actions', locale: :de)
tbody tbody
- @anomalies.each_key do |id| - @relevant_exercises.each do |exercise|
- exercise = Exercise.find(id)
tr tr
td = link_to_if(ExercisePolicy.new(@user, exercise).show?, exercise.title, exercise_url(exercise)) td = link_to_if(ExercisePolicy.new(@user, exercise).show?, exercise.title, exercise_url(exercise, token: @token))
td = @anomalies[id] td = @anomalies[exercise.id]
td = link_to_if(ExercisePolicy.new(@user, exercise).statistics?, t('shared.statistics', locale: :de), statistics_exercise_url(exercise)) td = link_to_if(ExercisePolicy.new(@user, exercise).statistics?, t('shared.statistics', locale: :de), statistics_exercise_url(exercise, token: @token))
== t('mailers.user_mailer.exercise_anomaly_detected.body2', == t('mailers.user_mailer.exercise_anomaly_detected.body2',
receiver_displayname: @receiver_displayname, receiver_displayname: @receiver_displayname,
@ -28,11 +27,10 @@ table(border=1)
td = t('exercises.statistics.average_worktime', locale: :en) td = t('exercises.statistics.average_worktime', locale: :en)
td = t('shared.actions', locale: :en) td = t('shared.actions', locale: :en)
tbody tbody
- @anomalies.each_key do |id| - @relevant_exercises.each do |exercise|
- exercise = Exercise.find(id)
tr tr
td = link_to_if(ExercisePolicy.new(@user, exercise).show?, exercise.title, exercise_url(exercise)) td = link_to_if(ExercisePolicy.new(@user, exercise).show?, exercise.title, exercise_url(exercise, token: @token))
td = @anomalies[id] td = @anomalies[exercise.id]
td = link_to_if(ExercisePolicy.new(@user, exercise).statistics?, t('shared.statistics', locale: :en), statistics_exercise_url(exercise)) td = link_to_if(ExercisePolicy.new(@user, exercise).statistics?, t('shared.statistics', locale: :en), statistics_exercise_url(exercise, token: @token))
== t('mailers.user_mailer.exercise_anomaly_detected.body3') == t('mailers.user_mailer.exercise_anomaly_detected.body3')

View File

@ -17,8 +17,6 @@ de:
Die Aufgaben sind: Die Aufgaben sind:
<br> <br>
body2: | body2: |
<br>
Falls Sie beim Klick auf einen Link eine Fehlermeldung erhalten, dass Sie nicht berechtigt wären diese Aktion auszuführen, öffnen Sie bitte eine beliebige Programmieraufgabe aus einem Kurs heraus und klicken den Link danach noch einmal.<br>
<br> <br>
Diese Mail wurde automatisch von CodeOcean verschickt.<br> Diese Mail wurde automatisch von CodeOcean verschickt.<br>
<br> <br>
@ -31,8 +29,6 @@ de:
The exercises are: The exercises are:
<br> <br>
body3: | body3: |
<br>
If you receive an error that you are not authorized to perform this action when clicking a link, please log-in through any course exercise beforehand and click the link again. <br>
<br> <br>
This mail was automatically sent by CodeOcean. <br> This mail was automatically sent by CodeOcean. <br>
subject: Unregelmäßigkeiten in Aufgaben Ihrer Aufgabensammlung subject: Unregelmäßigkeiten in Aufgaben Ihrer Aufgabensammlung

View File

@ -17,8 +17,6 @@ en:
Die Aufgaben sind: Die Aufgaben sind:
<br> <br>
body2: | body2: |
<br>
Falls Sie beim Klick auf einen Link eine Fehlermeldung erhalten, dass Sie nicht berechtigt wären diese Aktion auszuführen, öffnen Sie bitte eine beliebige Programmieraufgabe aus einem Kurs heraus und klicken den Link danach noch einmal.<br>
<br> <br>
Diese Mail wurde automatisch von CodeOcean verschickt.<br> Diese Mail wurde automatisch von CodeOcean verschickt.<br>
<br> <br>
@ -31,8 +29,6 @@ en:
The exercises are: The exercises are:
<br> <br>
body3: | body3: |
<br>
If you receive an error that you are not authorized to perform this action when clicking a link, please log-in through any course exercise beforehand and click the link again. <br>
<br> <br>
This mail was automatically sent by CodeOcean. <br> This mail was automatically sent by CodeOcean. <br>
subject: Anomalies in exercises of your exercise collection subject: Anomalies in exercises of your exercise collection