Skip to content

Commit

Permalink
More consent cache updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cannycookie committed Dec 16, 2024
1 parent 9ba8e15 commit 98fef72
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Livewire/ConsentOptionFormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function mount(): void
$this->user->collection = $this->user->outstandingConsents();

Check failure on line 62 in src/Livewire/ConsentOptionFormBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Illuminate\Foundation\Auth\User::$collection.

Check failure on line 62 in src/Livewire/ConsentOptionFormBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Foundation\Auth\User::outstandingConsents().

Check failure on line 62 in src/Livewire/ConsentOptionFormBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Illuminate\Foundation\Auth\User::$collection.

Check failure on line 62 in src/Livewire/ConsentOptionFormBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Illuminate\Foundation\Auth\User::outstandingConsents().

if ($this->user->collection->count() < 1) {
$this->redirect(request()->session()->get('url.saved'));
abort(403, 'No required consent');
}

$this->setDefaultValues();
Expand Down
27 changes: 17 additions & 10 deletions src/Traits/HasConsent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Visualbuilder\FilamentUserConsent\Traits;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Visualbuilder\FilamentUserConsent\Models\ConsentOption;
use Visualbuilder\FilamentUserConsent\Models\ConsentOptionUser;
Expand All @@ -22,7 +23,7 @@ public function requiredConsentKeys(): array
{
return ConsentOption::activeKeysForUser($this);
}

public function outstandingConsentValidators()
{
$consents = $this->outstandingConsents();
Expand Down Expand Up @@ -123,14 +124,20 @@ public function activeConsents()
*/
public function hasRequiredConsents()
{
$requiredConsents = ConsentOption::findbykeys($this->requiredConsentKeys())
->where('force_user_update', true)
->pluck('id')
->toArray();
$givenConsents = $this->consents()
->pluck('consent_options.id')
->toArray();

return ! array_diff($requiredConsents, $givenConsents);
// Define a unique cache key based on identifiable attributes including the model class
$cacheKey = 'user_consent_'.class_basename($this).'-'.$this->getKey();

// Retrieve from cache or calculate if not cached
return Cache::tags(['user-consents'])->rememberForever($cacheKey, function () {
$requiredConsents = ConsentOption::findbykeys($this->requiredConsentKeys())
->where('force_user_update', true)
->pluck('id')
->toArray();
$givenConsents = $this->consents()
->pluck('consent_options.id')
->toArray();

return !array_diff($requiredConsents, $givenConsents);
});
}
}

0 comments on commit 98fef72

Please sign in to comment.