-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Check user ownership of groups parties cats #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -261,6 +261,32 @@ public function store(Request $request): JsonResponse | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| unset($data['categories']); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //ownership checks for authenticated user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $user = auth()->user(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //check for group | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isset($data['group_id']) && $data['group_id']){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!$user->groups()->where('id', $data['group_id'])->exists()){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new HttpException(400,'The selected group does not belong to user'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //check for party | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isset($data['party_id']) && $data['party_id']) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!$user->parties()->where('id', $data['party_id'])->exists()){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new HttpException(400,'The selected party does not belong to user'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //check for categories | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!empty($categories)){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $user_category_ids = $user->categories()->pluck('id')->toArray(); //get user category ids | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $invalid_categories = array_diff($categories, $user_category_ids); //find any ids not owned by user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!empty($invalid_categories)){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new HttpException(400,'Some of the selected categories do not belong to user. Invalid category IDs: '.implode(',', $invalid_categories)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+265
to
+288
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ownership checks for groups, parties, and categories are duplicated across the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $transaction = DB::transaction(function () use ($request, $data, $categories, $recurring_transaction_data) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** @var Transaction $transaction */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -311,6 +337,10 @@ public function store(Request $request): JsonResponse | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'exception' => $e, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // If an HttpException was thrown earlier (e.g., from ownership check), re-throw it with the correct status code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ($e instanceof HttpException) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $this->failure($e->getMessage(), $e->getStatusCode()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $this->failure('Failed to create transaction', 500, [$e->getMessage()]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -546,6 +576,33 @@ public function update(Request $request, $id): JsonResponse | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (HttpException $e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $this->failure($e->getMessage(), $e->getStatusCode()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //check ownership of fields passed in request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $user = auth()->user(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //check for group | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isset($validatedData['group_id']) && $validatedData['group_id']){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!$user->groups()->where('id', $validatedData['group_id'])->exists()){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new HttpException(400,'The selected group does not belong to user'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //check for party | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (isset($validatedData['party_id']) && $validatedData['party_id']) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!$user->parties()->where('id', $validatedData['party_id'])->exists()){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new HttpException(400,'The selected party does not belong to user'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| //check for categories | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!empty($categories)){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $user_category_ids = $user->categories()->pluck('id')->toArray(); //get user category ids | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $invalid_categories = array_diff($categories, $user_category_ids); //find any ids not owned by user | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!empty($invalid_categories)){ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new HttpException(400,'Some of the selected categories do not belong to user. Invalid category IDs: '.implode(',', $invalid_categories)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+581
to
+604
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $transaction = DB::transaction(function () use ($validatedData, $transaction, $recurring_transaction_data, $request) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduce a new private helper method
authorizeTransactionOwnershipto centralize and reuse the ownership validation logic for groups, parties, and categories. This method will take the validated data and categories array as arguments, improving modularity and preventing code duplication across controller actions.