I'm trying to modify the Policies function which is currently this:
public function pass(User $user, Product $product)
{
return $user->id == $product->user_id;
}
Let's see if I explain myself correctly... I have some products that, when trying to access them, check that the id
user 's is the same as the user_id
product that they are trying to access up to here, everything is perfect, but what I also need is to create some products that are accessible to them by all users without restrictions that all users regardless of their id have access to them. For example, I have thought about using user_id 1
it in all products without restriction, but I don't know how to make the function of free access to all products, do you have user_id 1
any ideas?
I understand that your use case is the following:
There are products that can be accessed by all users and another group of products that have restricted access based on
user_id
the product.Based on this I propose two ways.
mode 1
That you add a value in the product table that indicates if the product is restrictive for the indicated user.
1 - Create a migration and add
is_restricted
as a boolean (or modify the initial one):then in
AddIsRestrictedColumnToProductsTable.php
:2 - Run your migration.
3 - Modify your policy:
mode 2
That you set the value of
user_id
in the product table tonull
in case this product is accessed by everyone. so, you can compare in your policy:You can include an attribute in your product table that determines if that product is accessible by everyone or only by a certain group of users.