F
Filament3w ago
Vp

Free and Paid Resources

I want to restrict navigation resource like below use-case, where free/paid can have options Use-cases: 1. When any user login, they should see all resources in navigation 2. If not subscribe, auto redirect to free resource when logging in 3. If not subscribe, they can browse free resource. If they click "paid" resource, it should ask "Confirmation" or "redirect to payment page" 3. If subscribed, they can access all How can I can restrict like this, or is there a plugins which can handle this? Or any advice on how to implement like this for Advance Navigation, thanks in advance. For simplicity, imagine this "free/paid" logic will be check by is_subscribe boolean column.
No description
Solution:
I tried to quickly create something, and ended up with this: use resources as regular resources, but then add global middleware to redirect to locked page
Jump to solution
9 Replies
Oscar Carvajal Mora
I think that the "Fillament/Laravel" way to do that could be using Policies. Have you tried "viewAny" method? This method run before any other policy method in order to know if the resource is available to be shown in navigation, access by a direct URL and so on
Vp
VpOP3w ago
viewAny hide both navigation and list page (without permission), but what if I put custom url, and display correct navigation... let me try this
Solution
Povilas Korop
Povilas Korop3w ago
I tried to quickly create something, and ended up with this: use resources as regular resources, but then add global middleware to redirect to locked page
Povilas Korop
Povilas Korop3w ago
No description
No description
Povilas Korop
Povilas Korop3w ago
This is AI-generated and I would change a lot but you will probably get the main idea:
class SubscriptionMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$user = Auth::user();

if (!$user) {
return $next($request);
}

if ($this->isResourceRequest($request)) {
$resourceClass = $this->getResourceClassFromRequest($request);

if ($resourceClass && ResourceAccessService::isResourcePaid($resourceClass) && !$user->canAccessPaidResources()) {
$resourceName = ResourceAccessService::getResourceName($resourceClass);
return redirect()->route('filament.admin.pages.locked-resource', ['resource' => $resourceName]);
}
}
class SubscriptionMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$user = Auth::user();

if (!$user) {
return $next($request);
}

if ($this->isResourceRequest($request)) {
$resourceClass = $this->getResourceClassFromRequest($request);

if ($resourceClass && ResourceAccessService::isResourcePaid($resourceClass) && !$user->canAccessPaidResources()) {
$resourceName = ResourceAccessService::getResourceName($resourceClass);
return redirect()->route('filament.admin.pages.locked-resource', ['resource' => $resourceName]);
}
}
Povilas Korop
Povilas Korop3w ago
and then globally register that middleware in AdminPanelProvider
No description
Povilas Korop
Povilas Korop3w ago
I will probably shoot a video next week when I tidy up this project, but wanted to give you the idea
Oscar Carvajal Mora
Yup. What @PovilasKorop says could be a better way. I'm not sure if you only need to hide the resources or also want to give a "unlock resource" page to your users No! I really sorry to miss your entire message. I only saw the image, now I notice all the use cases. Sorry for that!
Vp
VpOP3w ago
Yes, I will go like this solution.. thank you

Did you find this page helpful?