feat: add loa requests
This commit adds a feature that allows users to request periods of inactivity from their managers. This is effectively known as a leave of absence. The commit also introduces new permissions and migrations, therefore, you'll need to adapt your database according to these changes.
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Exceptions\AbsenceNotActionableException;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
@@ -25,4 +27,99 @@ class Absence extends Model
|
||||
{
|
||||
return $this->belongsTo('App\User', 'requesterID', 'id');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determines whether this model can be setApproved(), setDeclined() or setCancelled()
|
||||
*
|
||||
* @param bool $toCancel Switch the check to cancellability check
|
||||
* @return bool
|
||||
*/
|
||||
public function isActionable(bool $toCancel = false): bool
|
||||
{
|
||||
if ($toCancel)
|
||||
{
|
||||
return in_array($this->getRawOriginal('status'), ['PENDING', 'APPROVED']);
|
||||
}
|
||||
|
||||
return $this->getRawOriginal('status') == 'PENDING';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the Absence's status as approved
|
||||
*
|
||||
* @return Absence
|
||||
* @throws AbsenceNotActionableException
|
||||
*/
|
||||
public function setApproved(): Absence
|
||||
{
|
||||
if ($this->isActionable())
|
||||
{
|
||||
return tap($this)->update([
|
||||
'status' => 'APPROVED'
|
||||
]);
|
||||
}
|
||||
|
||||
throw new AbsenceNotActionableException('This absence is not actionable!');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the absence's status as declined
|
||||
*
|
||||
* @return Absence
|
||||
* @throws AbsenceNotActionableException
|
||||
*/
|
||||
public function setDeclined(): Absence
|
||||
{
|
||||
if ($this->isActionable()) {
|
||||
return tap($this)->update([
|
||||
'status' => 'DECLINED'
|
||||
]);
|
||||
}
|
||||
|
||||
throw new AbsenceNotActionableException('This absence is not actionable!');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Sets the absence's status as cancelled
|
||||
*
|
||||
* @return Absence
|
||||
* @throws AbsenceNotActionableException Thrown when the switch to this status would be invalid
|
||||
*/
|
||||
public function setCancelled(): Absence
|
||||
{
|
||||
if ($this->isActionable(true)) {
|
||||
return tap($this)->update([
|
||||
'status' => 'CANCELLED'
|
||||
]);
|
||||
}
|
||||
|
||||
throw new AbsenceNotActionableException('This absence is not actionable!');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the absence's status as ended
|
||||
*
|
||||
* @return Absence
|
||||
*/
|
||||
public function setEnded(): Absence
|
||||
{
|
||||
return tap($this)->update([
|
||||
'status' => 'ENDED'
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
// Look out when retrieving this value;
|
||||
//If you need the unaltered version of it, either adapt to its formatting or call getRawOriginal()
|
||||
protected function status(): Attribute {
|
||||
return Attribute::make(
|
||||
get: fn($value) => ucfirst(strtolower($value))
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
10
app/Exceptions/AbsenceNotActionableException.php
Normal file
10
app/Exceptions/AbsenceNotActionableException.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class AbsenceNotActionableException extends Exception
|
||||
{
|
||||
//
|
||||
}
|
@@ -3,10 +3,12 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Absence;
|
||||
use App\Exceptions\AbsenceNotActionableException;
|
||||
use App\Http\Requests\StoreAbsenceRequest;
|
||||
use App\Http\Requests\UpdateAbsenceRequest;
|
||||
use App\User;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
@@ -28,7 +30,7 @@ class AbsenceController extends Controller
|
||||
// Or we could adjust the query (using a model scope) to only return valid absences;
|
||||
// If there are any, refuse to store more, but this approach also works
|
||||
// A model scope that only returns cancelled, declined and ended absences could also be implemented for future use
|
||||
if (in_array($absence->status, ['PENDING', 'APPROVED']))
|
||||
if (in_array($absence->getRawOriginal('status'), ['PENDING', 'APPROVED']))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -38,31 +40,51 @@ class AbsenceController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
* Display a listing of absences.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->authorize('viewAny', Absence::class);
|
||||
|
||||
return view('dashboard.absences.index')
|
||||
->with('absences', Absence::all());
|
||||
// display for admin users
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
* Display a listing of absences belonging to the current user.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function showUserAbsences()
|
||||
{
|
||||
$this->authorize('viewOwn', Absence::class);
|
||||
|
||||
return view('dashboard.absences.own')
|
||||
->with('absences', Auth::user()->absences);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Show the form for creating a new absence request.
|
||||
*
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
$this->authorize('create', Absence::class);
|
||||
|
||||
return view('dashboard.absences.create')
|
||||
->with('activeRequest', $this->hasActiveRequest(Auth::user()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
* Store a newly created request in storage.
|
||||
*
|
||||
* @param \App\Http\Requests\StoreAbsenceRequest $request
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
@@ -77,62 +99,133 @@ class AbsenceController extends Controller
|
||||
->with('error', __('You already have an active request. Cancel it or let it expire first.'));
|
||||
}
|
||||
|
||||
Absence::create([
|
||||
|
||||
$absence = Absence::create([
|
||||
'requesterID' => Auth::user()->id,
|
||||
'start' => $request->start_date,
|
||||
'predicted_end' => $request->predicted_end,
|
||||
'available_assist' => $request->invalidAbsenceAgreement == 'on',
|
||||
'available_assist' => $request->available_assist == "on",
|
||||
'reason' => $request->reason,
|
||||
'status' => 'PENDING',
|
||||
]);
|
||||
|
||||
return redirect()
|
||||
->back()
|
||||
->to(route('absences.show', ['absence' => $absence->id]))
|
||||
->with('success', 'Absence request submitted for approval. You will receive email confirmation shortly.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
* Display the specified absence request.
|
||||
*
|
||||
* @param \App\Absence $absence
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show(Absence $absence)
|
||||
{
|
||||
//
|
||||
$this->authorize('view', $absence);
|
||||
|
||||
return view('dashboard.absences.view')
|
||||
->with([
|
||||
'absence' => $absence,
|
||||
'totalDays' => Carbon::parse($absence->start)->diffInDays($absence->predicted_end)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
* Approve the specified absence.
|
||||
*
|
||||
* @param \App\Absence $absence
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param Absence $absence
|
||||
* @return RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function edit(Absence $absence)
|
||||
public function approveAbsence(Absence $absence): RedirectResponse
|
||||
{
|
||||
//
|
||||
$this->authorize('approve', $absence);
|
||||
|
||||
try
|
||||
{
|
||||
$absence->setApproved();
|
||||
}
|
||||
catch (AbsenceNotActionableException $notActionableException)
|
||||
{
|
||||
return redirect()
|
||||
->back()
|
||||
->with('error', $notActionableException->getMessage());
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->back()
|
||||
->with('success', __('Absence request successfully approved. It will automatically transition to "Ended" on its predicted end date.'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
* Decline the specified absence.
|
||||
*
|
||||
* @param \App\Http\Requests\UpdateAbsenceRequest $request
|
||||
* @param \App\Absence $absence
|
||||
* @return \Illuminate\Http\Response
|
||||
* @param Absence $absence
|
||||
* @return RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function update(UpdateAbsenceRequest $request, Absence $absence)
|
||||
public function declineAbsence(Absence $absence): RedirectResponse
|
||||
{
|
||||
//
|
||||
$this->authorize('decline', $absence);
|
||||
|
||||
try
|
||||
{
|
||||
$absence->setDeclined();
|
||||
} catch (AbsenceNotActionableException $notActionableException)
|
||||
{
|
||||
return redirect()
|
||||
->back()
|
||||
->with('error', $notActionableException->getMessage());
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->back()
|
||||
->with('success', __('Absence request successfully declined.'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Cancel the specified absence.
|
||||
*
|
||||
* @param Absence $absence
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
*/
|
||||
public function cancelAbsence(Absence $absence): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$this->authorize('cancel', $absence);
|
||||
|
||||
try
|
||||
{
|
||||
$absence->setCancelled();
|
||||
}
|
||||
catch (AbsenceNotActionableException $notActionableException)
|
||||
{
|
||||
return redirect()
|
||||
->back()
|
||||
->with('error', $notActionableException->getMessage());
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->back()
|
||||
->with('success', __('Absence request successfully cancelled.'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param \App\Absence $absence
|
||||
* @return \Illuminate\Http\Response
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*/
|
||||
public function destroy(Absence $absence)
|
||||
{
|
||||
//
|
||||
$this->authorize('delete', $absence);
|
||||
|
||||
if ($absence->delete()) {
|
||||
return redirect()
|
||||
->to(route('absences.index'))
|
||||
->with('success', __('Absence request deleted.'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -21,10 +21,20 @@ class AbsencePolicy
|
||||
{
|
||||
if ($user->hasPermissionTo('admin.viewAllAbsences'))
|
||||
{
|
||||
return \Illuminate\Auth\Access\Response::allow();
|
||||
return true;
|
||||
}
|
||||
|
||||
return \Illuminate\Auth\Access\Response::deny('Forbidden');
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function viewOwn(User $user): bool
|
||||
{
|
||||
if ($user->hasPermissionTo('reviewer.viewAbsence')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +46,7 @@ class AbsencePolicy
|
||||
*/
|
||||
public function view(User $user, Absence $absence)
|
||||
{
|
||||
if ($user->hasPermissionTo('reviewer.viewAbsence') && $user->id == $absence->requesterID)
|
||||
if ($user->hasPermissionTo('reviewer.viewAbsence') && $user->is($absence->requester) || $user->hasPermissionTo('admin.manageAbsences'))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
@@ -55,16 +65,49 @@ class AbsencePolicy
|
||||
return $user->hasPermissionTo('reviewer.requestAbsence');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Determine whether the user can update the model.
|
||||
* Determine whether the user can approve the absence request
|
||||
*
|
||||
* @param \App\User $user
|
||||
* @param \App\Absence $absence
|
||||
* @return \Illuminate\Auth\Access\Response|bool
|
||||
* @param User $user
|
||||
* @param Absence $absence
|
||||
* @return bool
|
||||
*/
|
||||
public function update(User $user, Absence $absence)
|
||||
public function approve(User $user, Absence $absence): bool
|
||||
{
|
||||
return $user->hasPermissionTo('admin.manageAbsences');
|
||||
if ($user->can('admin.manageAbsences') && $user->isNot($absence->requester))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public function decline(User $user, Absence $absence): bool
|
||||
{
|
||||
if ($user->can('admin.manageAbsences') && $user->isNot($absence->requester))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can cancel the absence request
|
||||
*
|
||||
* @param User $user
|
||||
* @param Absence $absence
|
||||
* @return bool
|
||||
*/
|
||||
public function cancel(User $user, Absence $absence): bool {
|
||||
|
||||
if($user->is($absence->requester) && $user->can('reviewer.withdrawAbsence')) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,7 +119,8 @@ class AbsencePolicy
|
||||
*/
|
||||
public function delete(User $user, Absence $absence)
|
||||
{
|
||||
return $user->hasPermissionTo('admin.manageAbsences') || $user->hasPermissionTo('reviewer.withdrawAbsence') && $user->id == $absence->requesterID;
|
||||
return $user->hasPermissionTo('admin.manageAbsences');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@@ -21,11 +21,13 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Absence;
|
||||
use App\ApiKey;
|
||||
use App\Application;
|
||||
use App\Appointment;
|
||||
use App\Ban;
|
||||
use App\Form;
|
||||
use App\Policies\AbsencePolicy;
|
||||
use App\Policies\ApiKeyPolicy;
|
||||
use App\Policies\ApplicationPolicy;
|
||||
use App\Policies\AppointmentPolicy;
|
||||
@@ -64,7 +66,7 @@ class AuthServiceProvider extends ServiceProvider
|
||||
Appointment::class => AppointmentPolicy::class,
|
||||
Team::class => TeamPolicy::class,
|
||||
TeamFile::class => TeamFilePolicy::class,
|
||||
ApiKey::class => ApiKeyPolicy::class
|
||||
Absence::class => AbsencePolicy::class
|
||||
];
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user