mardi 23 avril 2019

There's a way to pass more Models instance in the authorize method (policy) with the 'create' action (usually is not not required a model instance)

I'm stuck in a policy problem. Actually i have a OrderEventHistoryPolicy and a

class OrderEventHistoryPolicy { use HandlesAuthorization;

/**
 * Determine whether the user can create the order event history.
 *
 * @param  \App\User  $user
 * @param  \App\Models\Order\Order  $order
 */
public function create(User $user, OrderEventHistory $orderEventHistory, Order $order)
}

and a OrderEventHistoryController

class OrderEventHistoryController extends Controller
{

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \App\Models\Order\Order $order
     */
    public function store(Request $request, Order $order)
    {
        $this->authorize('create',OrderEventHistory::class, $order);
    }
}

My aim is to pass the Order model to the policy class (OrderEventHistoryPolicy), but according to Laravel 5.8 documentation, you can just pass the class name with action like 'OrderEventHistoryPolicy'. By passing the class name i am not able to pass the Order model (of course). Here's the Laravel documentation:

As previously discussed, some actions like create may not require a model instance. In these situations, you may pass a class name to the authorize method. The class name will be used to determine which policy to use when authorizing the action:

public function create(Request $request) {
   $this->authorize('create', Post::class);
   // The current user can create blog posts... 
}

What i was thinking is to pass a empty OrderEventHistory model like this:

$this->authorize('create',new OrderEventHistory(), $order);

But i am not sure this is the "right" and "clean" way for doing it.

Anyone face in this situation?

Thanks in advance.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2IQJvEy
via IFTTT

Aucun commentaire:

Enregistrer un commentaire