Repass on policies
This commit is contained in:
parent
0055add48f
commit
6cfc3e381e
6 changed files with 88 additions and 100 deletions
|
|
@ -4,25 +4,21 @@ namespace App\Policies;
|
|||
|
||||
use App\Enums\UserRole;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\Response;
|
||||
|
||||
class DeliveryModePolicy extends Policy
|
||||
{
|
||||
/**
|
||||
* User can update delivery Mode ?
|
||||
*
|
||||
* @param \App\Models\User|null $user
|
||||
* @param \App\Models\User $user
|
||||
*
|
||||
* @return \Illuminate\Auth\Access\Response
|
||||
*/
|
||||
public function update(User $user = null)
|
||||
public function update(User $user)
|
||||
{
|
||||
if (!$user) {
|
||||
return Response::deny();
|
||||
if (!$user->role->equals(UserRole::admin())) {
|
||||
return $this->deny('Seuls les administrateurs peuvent accéder à ce paramètre.');
|
||||
}
|
||||
if (!$user or !$user->role->equals(UserRole::admin())) {
|
||||
return Response::deny('Seuls les administrateurs peuvent accéder à ce paramètre.');
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,16 +22,16 @@ class OrderPolicy extends Policy
|
|||
* @return \Illuminate\Auth\Access\Response
|
||||
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
||||
*/
|
||||
public function orderThings(Customer $customer = null): Response
|
||||
public function orderThings(?Customer $customer = null): Response
|
||||
{
|
||||
// phpcs:enable
|
||||
if (!$this->anyDeliveryModeAvalable()) {
|
||||
return Response::deny(trans('Aucun mode de livraison n\'est disponible'));
|
||||
return $this->deny(trans('Aucun mode de livraison n\'est disponible'));
|
||||
}
|
||||
if (!$this->anyPaymentModeAvalable()) {
|
||||
return Response::deny(trans('Aucun mode de paiement n\'est disponible'));
|
||||
return $this->deny(trans('Aucun mode de paiement n\'est disponible'));
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -44,18 +44,18 @@ class OrderPolicy extends Policy
|
|||
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
||||
*/
|
||||
public function orderUsingDeliveryMode(
|
||||
Customer $customer = null,
|
||||
?Customer $customer = null,
|
||||
Order $order
|
||||
): Response {
|
||||
// phpcs:enable
|
||||
if (!$this->anyDeliveryModeAvalable()) {
|
||||
return Response::deny(trans('Aucun mode de livraison n\'est disponible'));
|
||||
return $this->deny(trans('Aucun mode de livraison n\'est disponible'));
|
||||
}
|
||||
if (!$this->anyPaymentModeAvalable()) {
|
||||
return Response::deny(trans('Aucun mode de paiement n\'est disponible'));
|
||||
return $this->deny(trans('Aucun mode de paiement n\'est disponible'));
|
||||
}
|
||||
if (!$this->deliveryModeIsAvalaible($order->deliveryMode->mode)) {
|
||||
return Response::deny(trans("Le mode de livraison {$order->deliveryMode->mode->label} n'est disponible"));
|
||||
return $this->deny(trans("Le mode de livraison {$order->deliveryMode->mode->label} n'est disponible"));
|
||||
}
|
||||
// * Selection d'un mode de livraison autre que 'inShop'seulement
|
||||
// * si un mode de paiement autre que 'inShop'est disponible.
|
||||
|
|
@ -66,9 +66,9 @@ class OrderPolicy extends Policy
|
|||
!$this->deliveryModeIsAvalaible(DeliveryModeEnum::inShop()) and
|
||||
$deliveryModes->count() < 2
|
||||
) {
|
||||
return Response::deny(trans("Le mode de livraison {$order->deliveryMode->mode->label} n'est disponible"));
|
||||
return $this->deny(trans("Le mode de livraison {$order->deliveryMode->mode->label} n'est disponible"));
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -83,27 +83,27 @@ class OrderPolicy extends Policy
|
|||
{
|
||||
// phpcs:enable
|
||||
if ($order->delivery_status->value >= DeliveryStatus::ready()->value) {
|
||||
return Response::deny('The order has already been prepared');
|
||||
return $this->deny('The order has already been prepared');
|
||||
}
|
||||
if (
|
||||
$order->payment_status->equals(PaymentStatus::canceled()) or
|
||||
$order->payment_status->equals(PaymentStatus::failed())
|
||||
) {
|
||||
return Response::deny('The order status is :status and cannot be prepared.', [
|
||||
return $this->deny('The order status is :status and cannot be prepared.', [
|
||||
'status' => $order->payment_status->label
|
||||
]);
|
||||
}
|
||||
// ! Limit to only order where mail has been sent (order-tunnel has completed).
|
||||
if (!$order->mail_sent) {
|
||||
return Response::deny('The order was not completed (mail sent to client).');
|
||||
return $this->deny('The order was not completed (mail sent to client).');
|
||||
}
|
||||
if ($order->has_warning) {
|
||||
return Response::deny('The order has warnings.');
|
||||
return $this->deny('The order has warnings.');
|
||||
}
|
||||
if (!$order->validation_status->equals(ValidationStatus::validated())) {
|
||||
return Response::deny('The order has to be validated to be prepared.');
|
||||
return $this->deny('The order has to be validated to be prepared.');
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -118,21 +118,21 @@ class OrderPolicy extends Policy
|
|||
{
|
||||
$transaction = $order->getTransactionUsedForPayment();
|
||||
if (!$transaction) {
|
||||
return Response::deny('The order does not have a transaction associated.');
|
||||
return $this->deny('The order does not have a transaction associated.');
|
||||
}
|
||||
// ! Limit to only order where mail has been sent (order-tunnel has completed).
|
||||
if (!$order->mail_sent) {
|
||||
return Response::deny('The order was not completed (mail sent to client).');
|
||||
return $this->deny('The order was not completed (mail sent to client).');
|
||||
}
|
||||
if ($order->has_warning) {
|
||||
return Response::deny('The order has warnings.');
|
||||
return $this->deny('The order has warnings.');
|
||||
}
|
||||
if ($order->validation_status->equals(ValidationStatus::canceled())) {
|
||||
return Response::deny('The order is already canceled.');
|
||||
return $this->deny('The order is already canceled.');
|
||||
}
|
||||
|
||||
// phpcs:enable
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -148,15 +148,15 @@ class OrderPolicy extends Policy
|
|||
// phpcs:enable
|
||||
// ! Limit to only order where mail has been sent (order-tunnel has completed).
|
||||
if (!$order->mail_sent) {
|
||||
return Response::deny('The order was not completed (mail sent to client).');
|
||||
return $this->deny('The order was not completed (mail sent to client).');
|
||||
}
|
||||
if ($order->has_warning) {
|
||||
return Response::deny('The order has warnings.');
|
||||
return $this->deny('The order has warnings.');
|
||||
}
|
||||
if ($order->validation_status->equals(ValidationStatus::pending())) {
|
||||
return Response::deny('The order still needs validation.');
|
||||
return $this->deny('The order still needs validation.');
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -175,27 +175,27 @@ class OrderPolicy extends Policy
|
|||
$order->payment_status->equals(PaymentStatus::canceled()) or
|
||||
$order->payment_status->equals(PaymentStatus::failed())
|
||||
) {
|
||||
return Response::deny('The order payment status is alread :status.', [
|
||||
return $this->deny('The order payment status is alread :status.', [
|
||||
'status' => $order->payment_status->label
|
||||
]);
|
||||
}
|
||||
if (!$transaction) {
|
||||
return Response::deny('The order does not have a transaction associated.');
|
||||
return $this->deny('The order does not have a transaction associated.');
|
||||
}
|
||||
if ($transaction->payment_mode->equals(PaymentModeEnum::inShop())) {
|
||||
return Response::deny('The order transaction payment mode is in shop, and cannot be refunded.');
|
||||
return $this->deny('The order transaction payment mode is in shop, and cannot be refunded.');
|
||||
}
|
||||
// ! Limit to only order where mail has been sent (order-tunnel has completed).
|
||||
if (!$order->mail_sent) {
|
||||
return Response::deny('The order was not completed (mail sent to client).');
|
||||
return $this->deny('The order was not completed (mail sent to client).');
|
||||
}
|
||||
if ($order->has_warning) {
|
||||
return Response::deny('The order has warnings.');
|
||||
return $this->deny('The order has warnings.');
|
||||
}
|
||||
if (!$order->can_be_refund) {
|
||||
return Response::deny('The order cannot be refunded (from model).');
|
||||
return $this->deny('The order cannot be refunded (from model).');
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -210,36 +210,30 @@ class OrderPolicy extends Policy
|
|||
{
|
||||
// phpcs:enable
|
||||
if (!$order->has_warning) {
|
||||
return Response::deny('The order has warnings.');
|
||||
return $this->deny('The order has warnings.');
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
|
||||
/**
|
||||
* Can a user order download final order invoice
|
||||
*
|
||||
* @param \App\Models\Customer\Customer|\App\Models\User|null $user
|
||||
* @param \App\Models\Shop\Order|null $order
|
||||
* @param \App\Models\User $user
|
||||
* @param \App\Models\Shop\Order $order
|
||||
*
|
||||
* @return \Illuminate\Auth\Access\Response
|
||||
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
||||
*/
|
||||
public function download(
|
||||
$user = null,
|
||||
Order $order = null
|
||||
User $user,
|
||||
Order $order
|
||||
): Response {
|
||||
if (!$order) {
|
||||
return Response::deny(\trans('No order provided'));
|
||||
}
|
||||
if (!$order->transactions()->count()) {
|
||||
return Response::deny('No transactions are available on this order');
|
||||
return $this->deny('No transactions are available on this order');
|
||||
}
|
||||
if ($order->transactions()->where('needs_validation', true)->count()) {
|
||||
return Response::deny('There are transaction that needs to be resolved');
|
||||
return $this->deny('There are transaction that needs to be resolved');
|
||||
}
|
||||
if (!\is_object($user) or !($user instanceof User)) {
|
||||
return Response::deny(trans('Only authenticated users can do this'));
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,44 +4,41 @@ namespace App\Policies;
|
|||
|
||||
use App\Enums\Order\PaymentModeEnum;
|
||||
use App\Enums\UserRole;
|
||||
use App\Models\Customer\Customer;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\Response;
|
||||
|
||||
class PaymentModePolicy extends Policy
|
||||
{
|
||||
/**
|
||||
* User can update delivery Mode ?
|
||||
*
|
||||
* @param \App\Models\User|null $user
|
||||
* @param \App\Models\User $user
|
||||
*
|
||||
* @return \Illuminate\Auth\Access\Response
|
||||
*/
|
||||
public function update(User $user = null)
|
||||
public function update(User $user)
|
||||
{
|
||||
if (!$user) {
|
||||
return Response::deny();
|
||||
}
|
||||
if (!$user or !$user->role->equals(UserRole::admin())) {
|
||||
return Response::deny('Seuls les administrateurs peuvent accéder à ce paramètre.');
|
||||
return $this->deny('Seuls les administrateurs peuvent accéder à ce paramètre.');
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
|
||||
/**
|
||||
* Payment mode is available ?
|
||||
*
|
||||
* @param \App\Models\User|\App\Models\Customer|null $user
|
||||
* @param \App\Enums\Order\PaymentModeEnum $paymentModeEnum
|
||||
* @param \App\Models\Customer $user
|
||||
* @param \App\Enums\Order\PaymentModeEnum $paymentModeEnum
|
||||
*
|
||||
* @return \Illuminate\Auth\Access\Response
|
||||
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
||||
*/
|
||||
public function isAvailable($user, PaymentModeEnum $paymentModeEnum)
|
||||
public function isAvailable(Customer $user, PaymentModeEnum $paymentModeEnum)
|
||||
{
|
||||
// phpcs:enable
|
||||
if (!$this->paymentModeIsAvailable($paymentModeEnum)) {
|
||||
return Response::deny('Ce mode de paiement n\'est pas disponible');
|
||||
return $this->deny('Ce mode de paiement n\'est pas disponible');
|
||||
}
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,10 @@
|
|||
namespace App\Policies;
|
||||
|
||||
use App\Enums\Product\DefaultProductType;
|
||||
use App\Models\Shop\Order;
|
||||
use App\Models\Customer\Customer;
|
||||
use App\Models\Merchandise\Product;
|
||||
use App\Models\Shop\Order;
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Access\Response;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
|
|
@ -15,19 +17,19 @@ class ProductPolicy extends Policy
|
|||
*
|
||||
* Customer is nullable to allow guests.
|
||||
*
|
||||
* @param \App\Models\User|\App\Models\Customer|null $user
|
||||
* @param \App\Models\Customer|null $user
|
||||
*
|
||||
* @return \Illuminate\Auth\Access\Response
|
||||
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
|
||||
*/
|
||||
public function useBasketSystem($user = null): Response
|
||||
public function useBasketSystem(?Customer $user = null): Response
|
||||
{
|
||||
// phpcs:enable
|
||||
if (!Gate::check('orderThings', Order::class)) {
|
||||
return Response::deny('Il n\'est pas possible de passer des commandes pour \'instant');
|
||||
return $this->deny(\trans('Il n\'est pas possible de passer des commandes pour \'instant'));
|
||||
}
|
||||
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -35,27 +37,26 @@ class ProductPolicy extends Policy
|
|||
*
|
||||
* Customer is nullable to allow guests.
|
||||
*
|
||||
* @param \App\Models\User|\App\Models\Customer|null $user
|
||||
* @param \App\Models\Merchandise\Product $product
|
||||
* @param \App\Models\Customer|null $user
|
||||
* @param \App\Models\Merchandise\Product $product
|
||||
*
|
||||
* @return \Illuminate\Auth\Access\Response
|
||||
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
||||
* @phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd
|
||||
*/
|
||||
public function addToCart($user = null, Product $product): Response
|
||||
public function addToCart(?Customer $user = null, Product $product): Response
|
||||
{
|
||||
// phpcs:enable
|
||||
if (
|
||||
$product->orderable and
|
||||
$product->status
|
||||
) {
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
if (
|
||||
$product->orderable_at !== null and
|
||||
($product->orderable_at->gt(\now()))
|
||||
) {
|
||||
return Response::deny(trans('Commandable à partir du :date', [
|
||||
return $this->deny(\trans('Commandable à partir du :date', [
|
||||
'date' => $product->orderable_at->isoFormat('LLLL')
|
||||
]));
|
||||
}
|
||||
|
|
@ -63,9 +64,9 @@ class ProductPolicy extends Policy
|
|||
$product->unorderable_at !== null and
|
||||
($product->unorderable_at->lt(\now()))
|
||||
) {
|
||||
return Response::deny(trans('Ce produit n\'est plus disponible'));
|
||||
return $this->deny(\trans('Ce produit n\'est plus disponible'));
|
||||
}
|
||||
return Response::deny('Ce produit n\'est pas disponible pour le moment');
|
||||
return $this->deny(\trans('Ce produit n\'est pas disponible pour le moment'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -73,15 +74,15 @@ class ProductPolicy extends Policy
|
|||
*
|
||||
* Customer is nullable to allow guests.
|
||||
*
|
||||
* @param \App\Models\User|\App\Models\Customer|null $user
|
||||
* @param \App\Models\Merchandise\Product $product
|
||||
* @param \App\Models\User $user
|
||||
* @param \App\Models\Merchandise\Product $product
|
||||
* @return \Illuminate\Auth\Access\Response
|
||||
* @phpcs:disable PEAR.Functions.ValidDefaultValue.NotAtEnd
|
||||
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
||||
*/
|
||||
public function associateToInco($user = null, Product $product): Response
|
||||
public function associateToInco(User $user, Product $product): Response
|
||||
{
|
||||
// phpcs:enable
|
||||
return ($user and $product->type->value === DefaultProductType::inco()->value) ?
|
||||
Response::allow() : Response::deny('This product type is not ' . DefaultProductType::inco()->label);
|
||||
return ($product->type->value === DefaultProductType::inco()->value) ?
|
||||
$this->allow() : $this->deny('This product type is not ' . DefaultProductType::inco()->label);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,36 +17,36 @@ class TransactionPolicy extends Policy
|
|||
* ! + a validated transaction.
|
||||
* ! + a !needs_validation transaction.
|
||||
*
|
||||
* @param \App\Models\Customer\Customer|\App\Models\User|null $user
|
||||
* @param \App\Models\Payment\Transaction|null $transaction
|
||||
* @param \App\Models\Customer\Customer|\App\Models\User $user
|
||||
* @param \App\Models\Payment\Transaction|null $transaction
|
||||
*
|
||||
* @return \Illuminate\Auth\Access\Response
|
||||
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
||||
*/
|
||||
public function download(
|
||||
$user = null,
|
||||
$user,
|
||||
Transaction $transaction = null
|
||||
): Response {
|
||||
if (!$transaction) {
|
||||
return Response::deny(\trans('No transaction provided'));
|
||||
return $this->deny(\trans('No transaction provided'));
|
||||
}
|
||||
if (!$transaction->status->equals(TransactionStatus::validated())) {
|
||||
return Response::deny(trans('Transaction is not validated'));
|
||||
return $this->deny(trans('Transaction is not validated'));
|
||||
}
|
||||
if ($transaction->needs_validation) {
|
||||
return Response::deny(\trans('Transaction needs to be validated first'));
|
||||
return $this->deny(\trans('Transaction needs to be validated first'));
|
||||
}
|
||||
if ($user and $user instanceof User) {
|
||||
return Response::allow();
|
||||
if ($user instanceof User) {
|
||||
return $this->allow();
|
||||
}
|
||||
if (
|
||||
$user and $user instanceof Customer and
|
||||
$user instanceof Customer and
|
||||
$transaction->order->customer and
|
||||
!\is_null($user->hashcode) and
|
||||
$transaction->order->customer->hashcode === $user->hashcode
|
||||
) {
|
||||
return Response::allow();
|
||||
return $this->allow();
|
||||
}
|
||||
return Response::deny(\trans('Could not satisfy any conditions'));
|
||||
return $this->deny(\trans('Could not satisfy any conditions'));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Subproject commit 1298c1bd85b03e53b3c535f939ddfafde9c0b646
|
||||
Subproject commit b791524bf0b748b393af698b693bd019c77397a0
|
||||
Loading…
Add table
Reference in a new issue