ActivationPermissionsController.php
2 years ago
BalanceTransactionPermissionsController.php
3 years ago
ChargePermissionsController.php
3 years ago
CheckoutPermissionsController.php
1 year ago
CustomerPermissionsController.php
2 years ago
InvoicePermissionsController.php
3 years ago
LicensePermissionsController.php
3 years ago
MediaPermissionsController.php
3 years ago
ModelPermissionsController.php
1 year ago
OrderPermissionsController.php
3 years ago
PaymentMethodPermissionsController.php
3 years ago
PurchasePermissionsController.php
3 years ago
RefundPermissionsController.php
3 years ago
SubscriptionPermissionsController.php
2 years ago
ActivationPermissionsController.php
122 lines
| 1 | <?php |
| 2 | namespace SureCart\Permissions\Models; |
| 3 | |
| 4 | use SureCart\Models\Activation; |
| 5 | |
| 6 | /** |
| 7 | * Handle various charge permissions. |
| 8 | */ |
| 9 | class ActivationPermissionsController extends ModelPermissionsController { |
| 10 | /** |
| 11 | * Can user read multiple. |
| 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_activation( $user, $args, $allcaps ) { |
| 26 | if ( ! empty( $allcaps['read_sc_products'] ) ) { |
| 27 | return true; |
| 28 | } |
| 29 | return $this->belongsToUser( Activation::class, $args[2], $user ); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Can user read multiple. |
| 34 | * |
| 35 | * @param \SureCart\Models\User $user User model. |
| 36 | * @param array $args { |
| 37 | * Arguments that accompany the requested capability check. |
| 38 | * |
| 39 | * @type string $0 Requested capability. |
| 40 | * @type int $1 Concerned user ID. |
| 41 | * @type mixed ...$2 Optional second and further parameters, typically object ID. |
| 42 | * } |
| 43 | * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name |
| 44 | * and boolean values represent whether the user has that capability. |
| 45 | * @return boolean Does user have permission. |
| 46 | */ |
| 47 | public function read_sc_activations( $user, $args, $allcaps ) { |
| 48 | if ( ! empty( $allcaps['read_sc_products'] ) ) { |
| 49 | return true; |
| 50 | } |
| 51 | return $this->isListingOwnCustomerIds( $user, $args[2]['customer_ids'] ?? [] ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Can user read multiple. |
| 56 | * |
| 57 | * @param \SureCart\Models\User $user User model. |
| 58 | * @param array $args { |
| 59 | * Arguments that accompany the requested capability check. |
| 60 | * |
| 61 | * @type string $0 Requested capability. |
| 62 | * @type int $1 Concerned user ID. |
| 63 | * @type mixed ...$2 Optional second and further parameters, typically object ID. |
| 64 | * } |
| 65 | * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name |
| 66 | * and boolean values represent whether the user has that capability. |
| 67 | * @return boolean Does user have permission. |
| 68 | */ |
| 69 | public function edit_sc_activation( $user, $args, $allcaps ) { |
| 70 | if ( ! empty( $allcaps['edit_sc_products'] ) ) { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | // only allowed to update the fingerprint. |
| 75 | $params = $args[3]; |
| 76 | if ( ! $this->requestOnlyHasKeys( $params, array( 'fingerprint' ) ) ) { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | // only allowed to update if it belongs to the user. |
| 81 | return $this->belongsToUser( Activation::class, $args[2], $user ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Publish |
| 86 | * |
| 87 | * @param \SureCart\Models\User $user User model. |
| 88 | * @param array $args { |
| 89 | * Arguments that accompany the requested capability check. |
| 90 | * |
| 91 | * @type string $0 Requested capability. |
| 92 | * @type int $1 Concerned user ID. |
| 93 | * @type mixed ...$2 Optional second and further parameters, typically object ID. |
| 94 | * } |
| 95 | * @param bool[] $allcaps Array of key/value pairs where keys represent a capability name |
| 96 | * and boolean values represent whether the user has that capability. |
| 97 | * @return boolean Does user have permission. |
| 98 | */ |
| 99 | public function publish_sc_activations( $user, $args, $allcaps ) { |
| 100 | return ! empty( $allcaps['publish_sc_products'] ); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Does the model belong to the user? |
| 105 | * |
| 106 | * @param string $model Model name. |
| 107 | * @param string $id Model ID. |
| 108 | * @param \SureCart\Models\User $user User model. |
| 109 | * @return boolean |
| 110 | */ |
| 111 | public function belongsToUser( $model, $id, $user ) { |
| 112 | $model = $model::with( [ 'license' ] )->find( $id ); |
| 113 | if ( is_wp_error( $model ) ) { |
| 114 | return $model; |
| 115 | } |
| 116 | if ( ! $model->license ) { |
| 117 | return false; |
| 118 | } |
| 119 | return $model->license->belongsToUser( $user ); |
| 120 | } |
| 121 | } |
| 122 |