BalanceTransactionPermissionsController.php
3 years ago
ChargePermissionsController.php
3 years ago
CheckoutPermissionsController.php
3 years ago
CustomerPermissionsController.php
2 years ago
InvoicePermissionsController.php
3 years ago
LicensePermissionsController.php
3 years ago
MediaPermissionsController.php
3 years ago
ModelPermissionsController.php
3 years ago
OrderPermissionsController.php
3 years ago
PaymentMethodPermissionsController.php
3 years ago
PurchasePermissionsController.php
3 years ago
RefundPermissionsController.php
3 years ago
SubscriptionPermissionsController.php
3 years ago
OrderPermissionsController.php
70 lines
| 1 | <?php |
| 2 | namespace SureCart\Permissions\Models; |
| 3 | |
| 4 | use SureCart\Models\Order; |
| 5 | |
| 6 | /** |
| 7 | * Handle various charge permissions. |
| 8 | */ |
| 9 | class OrderPermissionsController extends ModelPermissionsController { |
| 10 | /** |
| 11 | * Can user read. |
| 12 | * |
| 13 | * @param \SureCart\Models\User $user User model. |
| 14 | * @param array $args { |
| 15 | * Arguments that accompany the requested capability check. |
| 16 | * |
| 17 | * @type string $0 Requested capability. |
| 18 | * @type int $1 Concerned user ID. |
| 19 | * @type mixed ...$2 Optional second and further parameters, typically object ID. |
| 20 | * } |
| 21 | * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name |
| 22 | * and boolean values represent whether the user has that capability. |
| 23 | * @return boolean Does user have permission. |
| 24 | */ |
| 25 | public function read_sc_order( $user, $args, $allcaps ) { |
| 26 | if ( ! empty( $allcaps['read_sc_orders'] ) ) { |
| 27 | return true; |
| 28 | } |
| 29 | return $this->belongsToUser( Order::class, $args[2], $user ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Does the model belong to the user? |
| 34 | * |
| 35 | * @param string $model Model name. |
| 36 | * @param string $id Model ID. |
| 37 | * @param \SureCart\Models\User $user User model. |
| 38 | * @return boolean |
| 39 | */ |
| 40 | public function belongsToUser( $model, $id, $user ) { |
| 41 | $order = Order::with( [ 'checkout' ] )->find( $id ); |
| 42 | if ( is_wp_error( $order ) ) { |
| 43 | return $order; |
| 44 | } |
| 45 | return $order->checkout->belongsToUser( $user ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Can user read. |
| 50 | * |
| 51 | * @param \SureCart\Models\User $user User model. |
| 52 | * @param array $args { |
| 53 | * Arguments that accompany the requested capability check. |
| 54 | * |
| 55 | * @type string $0 Requested capability. |
| 56 | * @type int $1 Concerned user ID. |
| 57 | * @type mixed ...$2 Optional second and further parameters, typically object ID. |
| 58 | * } |
| 59 | * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name |
| 60 | * and boolean values represent whether the user has that capability. |
| 61 | * @return boolean Does user have permission. |
| 62 | */ |
| 63 | public function read_sc_orders( $user, $args, $allcaps ) { |
| 64 | if ( ! empty( $allcaps['read_sc_orders'] ) ) { |
| 65 | return true; |
| 66 | } |
| 67 | return $this->isListingOwnCustomerIds( $user, $args[2]['customer_ids'] ?? [] ); |
| 68 | } |
| 69 | } |
| 70 |