insenseanalytics / laravel-nova-permission

A Laravel Nova tool for the Spatie Permission package
MIT License
78 stars 33 forks source link

Attach will not trigger forgetCachedPermissions() #5

Closed jianminLee closed 5 years ago

jianminLee commented 5 years ago
class ForgetCachedPermissions
{
    /**
     * Handle the incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return \Illuminate\Http\Response
     */
    public function handle($request, $next)
    {
        $response = $next($request);

        if ($request->is('nova-api/*/detach') ||
            $request->is('nova-api/*/*/attach/*')) {
            ##This is my attach url: http://127.0.0.1:8000/nova-api/admins/1/attach-morphed/permissions
            $permissionKey = (Nova::resourceForModel(app(PermissionRegistrar::class)->getPermissionClass()))::uriKey();

            if ($request->viaRelationship === $permissionKey) {
                app(PermissionRegistrar::class)->forgetCachedPermissions();
            }
        }

        return $response;
    }
}
paras-malhotra commented 5 years ago

Thanks for reporting @jianminLee. Can you make a PR for this? I think $request->is('nova-api/*/*/attach*') may work.

jianminLee commented 5 years ago

@paras-malhotra try:

$request->is('nova-api/*/*/attach*/*')

is work.