Skip to content
This repository has been archived by the owner on Jan 13, 2021. It is now read-only.

Commit

Permalink
Prevent self-returning of borrowings (v0.11.0)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenophie committed Oct 2, 2018
1 parent 63168ab commit 96d5784
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/Http/Requests/EndBorrowingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function rules()
{
return [
'selectedBorrowings' => 'required|array',
'selectedBorrowings.*' => 'integer|distinct', // TODO: Prevent self-return of borrowing
'selectedBorrowings.*' => 'integer|distinct|no_self_return',
'newInventoryItemsStatus' => [
'required',
'integer',
Expand Down
1 change: 1 addition & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function boot()

Validator::extend('inventory_item_available', 'App\Validators\InventoryItemAvailable@validate');
Validator::extend('inventory_item_not_borrowed', 'App\Validators\InventoryItemNotBorrowed@validate');
Validator::extend('no_self_return', 'App\Validators\NoSelfReturn@validate');
Validator::extend('password_for', 'App\Validators\PasswordFor@validate');
Validator::extend('unchanged_during_borrowing', 'App\Validators\UnchangedDuringBorrowing@validate');
Validator::extend('not_changed_to_borrowed', 'App\Validators\NotChangedToBorrowed@validate');
Expand Down
19 changes: 19 additions & 0 deletions app/Validators/NoSelfReturn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Validators;

use App\Borrowing;
use Illuminate\Support\Facades\Auth;

class NoSelfReturn
{
public function validate($attribute, $value, $parameters, $validator)
{
$borrowerId = Borrowing::find($value)->select('borrower_id')->first();
if ($borrowerId) {
$borrowerId = $borrowerId->borrower_id;
return $borrowerId !== Auth::user()->id;
}

}
}
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added

* End borrowing validation
* A user can't confirm the return of its own borrowings.
* Edit inventory view
* Minimal width of tables for better responsiveness.
* Edit users view
Expand All @@ -18,7 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
* Users updating and deletion.
* Edit users validation
* Check for admin self-modification.

### Removed

* Edit inventory view
Expand Down
3 changes: 2 additions & 1 deletion resources/lang/en/validation/endBorrowing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

return [
'selectedBorrowings.required' => 'Select borrowings to end.',
'selectedBorrowings.*.distinct' => 'The borrowing of :item by :borrower was selected in multiple times.'
'selectedBorrowings.*.distinct' => 'The borrowing of :item by :borrower was selected in multiple times.',
'selectedBorrowings.*.no_self_return' => 'A user can\'t confirm the return of one of its own borrowings.'
];
3 changes: 2 additions & 1 deletion resources/lang/fr/validation/endBorrowing.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

return [
'selectedBorrowings.required' => 'Sélectionnez des emprunts à terminer.',
'selectedBorrowings.*.distinct' => 'L\'emprunt du jeu :item par :borrower a été choisi plusieurs fois.'
'selectedBorrowings.*.distinct' => 'L\'emprunt du jeu :item par :borrower a été choisi plusieurs fois.',
'selectedBorrowings.*.no_self_return' => 'Un utilisateur ne peut pas valider le retour de son propre emprunt.'
];

0 comments on commit 96d5784

Please sign in to comment.