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