HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Orders/OrderPolicy.php
<?php

namespace App\Orders;

use Komma\KMS\Base\Policy;
use App\Orders\Models\Order;
use Komma\KMS\Users\Models\KmsUserRole;
use Komma\KMS\Users\Models\KmsUser;
use Illuminate\Auth\Access\HandlesAuthorization;

class OrderPolicy extends Policy
{
    use HandlesAuthorization;

    protected $modelClassName = Order::class;

    /**
     * The before method will be executed before any other methods on the policy,
     * giving you an opportunity to authorize the action before the
     * intended policy method is actually called
     *
     * @param $user
     * @param $ability
     * @return bool
     */
    public function before(KmsUser $user, $ability)
    {
        $result = $user->isAtLeast(KmsUserRole::SuperAdmin);

        $this->debug($ability, $result);
        if($result) {
            if($ability === 'destroy') return false; //See the destroy method for an explanation

            return true;
        }

        return null; //Fallback to the intended ability
    }

    public function create(KmsUser $user): bool
    {
        $this->debug('create', false);
        return false;
    }

    /**
     * For whom it is allowed to search orders
     *
     * @param KmsUser $user
     * @return bool
     */
    public function search(KmsUser $user) {
        $result = $user->isAtLeast(KmsUserRole::Admin);
        return $result;
    }

    /**
     * @param KmsUser $user
     * @param Order $order
     * @return bool
     */
    public function changeOrderStatus(KmsUser $user, Order $order)
    {
        return true;
    }

    /**
     * Check if it is allowed to create shipments for the order
     *
     * @param KmsUser $user
     * @param Order $order
     * @return bool
     */
    public function createShipmentForOrder(KmsUser $user, Order $order)
    {
        if(!OrderStatus::isValidItem($order->status, true)) return false;

        $allowedStatuses = [
//            OrderStatus::NEW,
//            OrderStatus::PENDING,
//            OrderStatus::AWAITING_PAYMENT,
            OrderStatus::AWAITING_FULFILLMENT,
            OrderStatus::AWAITING_SHIPMENT,
            OrderStatus::AWAITING_PICKUP,
            OrderStatus::PARTIALLY_SHIPPED,
//            OrderStatus::COMPLETED,
//            OrderStatus::SHIPPED,
//            OrderStatus::CANCELED,
//            OrderStatus::DECLINED,
//            OrderStatus::REFUNDED,
//            OrderStatus::PARTIALLY_REFUNDED,
            OrderStatus::DISPUTED,
//            OrderStatus::VERIFICATION,
        ];

        $result = (in_array($order->status, $allowedStatuses, true));
//        $this->debug('createShipmentForOrder', $result);
        return $result;
    }

    public function destroy(KmsUser $user, $modelToDestroy): bool
    {
        //Order deletion is not supported because of the many variables in play. Think about the payments and shipments attached to it.
        //Deleting an order would mean we need to delete them to if they reference the order. And payment and shipment providers may still try to reference them
        return false;
    }
}