Z3d0X / filament-logger

Extensible activity logger for filament that works out-of-the-box.
MIT License
287 stars 42 forks source link

Restrict access #10

Closed EmilioBravo closed 2 years ago

Z3d0X commented 2 years ago

I don't think i want to do authorization like this, because not everyone may have permissions set like this..

Since filament resources respects model policy, you can restrict access by using a policy for Activity Model. Here is an example:

//App/Policies/ActivityLogPolicy.php
<?php

namespace App\Policies;

use App\Models\User;

class ActivityLogPolicy
{
    public function viewAny(User $user): bool
    {
        return $user->can('view_activity');
    }

    public function view(User $user): bool
    {
        return $user->can('view_activity');
    }
}

then in your AuthServiceProvider you can hook up Activity Model & ActivityLogPolicy

<?php

namespace App\Providers;

use App\Policies\ActivityLogPolicy;
use Spatie\Activitylog\Models\Activity;

class AuthServiceProvider extends ServiceProvider
{

    protected $policies = [
        Activity::class => ActivityLogPolicy::class,
    ];

    //other stuff
}