Skip to content

Commit

Permalink
Simplify consent option request and middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
cannycookie committed Feb 13, 2024
1 parent 72e3fd6 commit 6e1eefa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
36 changes: 14 additions & 22 deletions src/Http/Middleware/ForceRedirectToUnapprovedConsents.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,28 @@ class ForceRedirectToUnapprovedConsents
{
public function handle(Request $request, Closure $next)
{
if (Auth::guard('admin')->check()) {
$guard = 'admin';
} elseif (Auth::guard('enduser')->check()) {
$guard = 'enduser';
} elseif (Auth::guard('practitioner')->check()) {
$guard = 'practitioner';
} else {
// Get the currently authenticated user across all guards
$user = Auth::user();

// If no user is authenticated, proceed with the request
if (!$user) {
return $next($request);
}

$isConsentRoute = str_contains($request->route()->getName(), 'consent-options');
if (
//must be logged in
Auth::guard($guard)->user()
//have the trait installed
&& method_exists(Auth::guard($guard)->user(), 'hasRequiredConsents')
//Not be a consent route
&& ! $isConsentRoute
//Not an ajax call
&& ! $request->ajax()
//Not have required consents signed
&& ! Auth::guard($guard)->user()->hasRequiredConsents()
) {
//Save current request URL
// Determine if the current route is exempt (a consent route or ends with '.logout')
$route = $request->route()->getName();
$isExemptRoute = str_contains($route, 'consent-options') || str_ends_with($route, '.logout');

// Check for required consents if user is authenticated and not on an exempt or logout route
if (!$isExemptRoute && !$request->ajax() && method_exists($user, 'hasRequiredConsents') && !$user->hasRequiredConsents()) {
// Save the current request URL
$request->session()->put('url.saved', $request->fullUrl());

//Redirect user to ask for consent
// Redirect user to ask for consent
return redirect()->route('consent-option-request');
}

return $next($request);
}

}
21 changes: 5 additions & 16 deletions src/Livewire/ConsentOptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,34 +28,23 @@ class ConsentOptionRequest extends SimplePage
use InteractsWithFormActions;
use InteractsWithForms;

public Model $user;
public $user;

public Collection $collection;

public $acceptConsents = [];

public function mount(): void
{
if (Auth::guard('admin')->check()) {
$this->user = Auth::guard('admin')->user();
} elseif (Auth::guard('enduser')->check()) {
$this->user = Auth::guard('enduser')->user();
} elseif (Auth::guard('practitioner')->check()) {
$this->user = Auth::guard('practitioner')->user();
}

if (! $this->user) {
abort(403, 'Only authenticated users can set consent options');
}
$this->user = auth()->user();

$this->user->collections = $this->user->outstandingConsents();

if ($this->user->collections->count() < 1) {
abort(403, 'No required consent');
}
}



public static ?string $title = 'Your consent is required';

protected static string $view = 'vendor.user-consent.livewire.consent-option-request';
Expand Down Expand Up @@ -172,8 +161,8 @@ public function acceptConsent()
);
}
Notification::make()
->title('Welcome.!')
->body('Your submitted all consent options are saved.')
->title('Success')
->body('Your consent preferences have been saved.')
->icon('heroicon-o-check-circle')
->color('success')
->send();
Expand Down

0 comments on commit 6e1eefa

Please sign in to comment.