shareit/app/Http/Controllers/AlertController.php
Bruno Fontes e22f49bc6a Avoiding issues and refactoring code
I made the code more passive, avoiding issued at taking, returning,
storing alerts or removing alerts from an item.

Now they all check if it is with you before returning/deleting
alert etc. I am not sure if all cases are covered, but they are
better than before. I had one only issued on this on that time,
but I prefer to prioritize safety/security.

I took the opportunitie to move some code from Controllers to
the model itself, as they were changing with the DB.
2018-10-21 13:09:06 -03:00

60 lines
1.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use Auth;
use Mail;
use \App\User;
use \App\Mail\UserWaiting;
use Illuminate\Http\Request;
class AlertController extends Controller
{
/**
* Store the waiting_user_id on db
* so the user can be alerted when
* the item is free
*
* @param Request $request Form data
*
* @return redirect to home
*/
public function store(Request $request)
{
$item = User::loggedIn()->items()->find(request('item'));
if (!$item->used_by) {
session()->flash(
FlashMessage::PRIMARY,
__('Oh! This item has just being returned. Take it before anyone else!')
);
return redirect('home');
}
if ($item->used_by == Auth::id()) {
return redirect('home');
}
$item->storeAlert();
$loggedUser = Auth::user()->name;
$userWithItem = User::find($item->used_by);
Mail::to($userWithItem)
->locale($userWithItem->language)
->send(new UserWaiting($loggedUser, $userWithItem->name, $item));
return redirect('home');
}
public function delete(Request $request)
{
$item = User::loggedIn()->items()->find(request('item'));
if ($item->waiting_user_id != Auth::id()) {
return redirect('home');
}
$item->removeAlert();
return redirect('home');
}
}