Skip to content

Commit 7c9fd33

Browse files
committed
Api/DeckController: don't return sessions by other users
With 0af425c the `index` method was changed to include sessions **of the current user** in the response if all decks for a module get queried. But the added statement ``` ...->with(['sessions') => function (...) {...}])->with('sessions.answerChoices')->... ``` actually doesn't work as intended, as the second `with` statement loads all sessions and not only those for the subset of the first `with` selection. Instead, the right approach seems to be to add the with statement to the sessions subquery: ``` $query->where('user_id', '=', Auth::id())->with('answerChoices'); ```
1 parent b6d85f8 commit 7c9fd33

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

app/Http/Controllers/Api/DeckController.php

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,17 @@ public function index(Request $request)
2020

2121
if ($request->module) {
2222
// Return all public decks for the given module
23-
return response()->json(
24-
Deck::where([
25-
['module_id', '=', $request->module],
26-
['access', '=', 'public-rw-listed'],
27-
])
28-
->with('module', 'module.subject', 'questions:id,is_invalid', 'questions.images:id,question_id')
29-
->with(['sessions' => function ($query) {
30-
$query->where('user_id', '=', Auth::id());
31-
}])
32-
->with('sessions.answerChoices')
33-
->get()
34-
);
23+
$decks = Deck::where([
24+
['module_id', '=', $request->module],
25+
['access', '=', 'public-rw-listed'],
26+
])
27+
->with('module', 'module.subject', 'questions:id,is_invalid', 'questions.images:id,question_id')
28+
->with(['sessions' => function ($query) {
29+
$query->where('user_id', '=', Auth::id())->with('answerChoices');
30+
}])
31+
->get();
32+
33+
return response()->json($decks);
3534
}
3635
if ($request->decks) {
3736
// Return the decks with the given IDs; this endpoint

0 commit comments

Comments
 (0)