| @@ -16,9 +16,9 @@ | ||
| 16 | 16 | use Exception; |
| 17 | 17 | use WC_Customer; |
| 18 | 18 | use WC_REST_Customers_Controller; |
| 19 | 19 | use WCPOS\WooCommercePOS\Logger; |
| 20 | -use WCPOS\WooCommercePOS\Services\Customer_Account_Guard; | |
| 20 | +use WCPOS\WooCommercePOS\Services\Permission_Rules; | |
| 21 | 21 | use WCPOS\WooCommercePOS\Services\Settings as SettingsService; |
| 22 | 22 | use WCPOS\WooCommercePOS\Services\Tax_Id_Reader; |
| 23 | 23 | use WCPOS\WooCommercePOS\Services\Tax_Id_Types; |
| 24 | 24 | use WCPOS\WooCommercePOS\Services\Tax_Id_Writer; |
| @@ -129,102 +129,30 @@ | ||
| 129 | 129 | |
| 130 | 130 | return $schema; |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | - /** | |
| 134 | - * Check if a given request has access to create a customer. | |
| 133 | + /** Delegate the create decision, preserving WooCommerce's request-dependent checks. | |
| 135 | 134 | * |
| 136 | - * WC checks promote_users (< 9.9) or create_customers (9.9+). The POS | |
| 137 | - * fallback checks only the version-appropriate capability so it matches | |
| 138 | - * the toggle shown on the Access settings page. | |
| 139 | - * | |
| 140 | - * @param WP_REST_Request $request Full details about the request. | |
| 141 | - * | |
| 142 | - * @return WP_Error|bool | |
| 135 | + * @param \WP_REST_Request $request Full request details. | |
| 143 | 136 | */ |
| 144 | 137 | public function create_item_permissions_check( $request ) { |
| 145 | - $permission = parent::create_item_permissions_check( $request ); | |
| 146 | - | |
| 147 | - if ( is_wp_error( $permission ) ) { | |
| 148 | - $customer_create_cap = version_compare( WC()->version, '9.9', '>=' ) | |
| 149 | - ? 'create_customers' | |
| 150 | - : 'promote_users'; | |
| 151 | - | |
| 152 | - if ( current_user_can( $customer_create_cap ) ) { | |
| 153 | - return true; | |
| 154 | - } | |
| 155 | - } | |
| 156 | - | |
| 157 | - return $permission; | |
| 138 | + return Permission_Rules::verdict( 'customers', 'create', (int) $request['id'], 0, 'v1', $request->get_params() ); | |
| 158 | 139 | } |
| 159 | 140 | |
| 160 | - /** | |
| 161 | - * Check if a given request has access to update a customer. | |
| 141 | + /** Delegate the edit decision, preserving WooCommerce's request-dependent checks. | |
| 162 | 142 | * |
| 163 | - * WCPOS never widens WooCommerce's credential fence, which refuses | |
| 164 | - * email/password changes on non-customer roles. The guard additionally | |
| 165 | - * keeps non-admins off staff accounts, testing capabilities rather than | |
| 166 | - * WooCommerce's first-role-only test. | |
| 167 | - * | |
| 168 | - * @param WP_REST_Request $request Full details about the request. | |
| 169 | - * | |
| 170 | - * @return WP_Error|bool | |
| 143 | + * @param \WP_REST_Request $request Full request details. | |
| 171 | 144 | */ |
| 172 | 145 | public function update_item_permissions_check( $request ) { |
| 173 | - return $this->wcpos_guarded_permissions_check( | |
| 174 | - (int) $request['id'], | |
| 175 | - function () use ( $request ) { | |
| 176 | - return parent::update_item_permissions_check( $request ); | |
| 177 | - } | |
| 178 | - ); | |
| 146 | + return Permission_Rules::verdict( 'customers', 'edit', (int) $request['id'], 0, 'v1', $request->get_params() ); | |
| 179 | 147 | } |
| 180 | 148 | |
| 181 | - /** | |
| 182 | - * Check if a given request has access to delete a customer. | |
| 149 | + /** Delegate the delete decision, preserving WooCommerce's request-dependent checks. | |
| 183 | 150 | * |
| 184 | - * WooCommerce refuses deleting a user whose role is outside its allowed | |
| 185 | - * list, but it reads only the FIRST role, so an administrator who also | |
| 186 | - * holds the customer role is deleted by any POS user with delete_users. | |
| 187 | - * The guard closes that by capability. | |
| 188 | - * | |
| 189 | - * @param WP_REST_Request $request Full details about the request. | |
| 190 | - * | |
| 191 | - * @return WP_Error|bool | |
| 151 | + * @param \WP_REST_Request $request Full request details. | |
| 192 | 152 | */ |
| 193 | 153 | public function delete_item_permissions_check( $request ) { |
| 194 | - return $this->wcpos_guarded_permissions_check( | |
| 195 | - (int) $request['id'], | |
| 196 | - function () use ( $request ) { | |
| 197 | - return parent::delete_item_permissions_check( $request ); | |
| 198 | - } | |
| 199 | - ); | |
| 200 | - } | |
| 201 | - | |
| 202 | - /** | |
| 203 | - * Run WooCommerce's own check behind the staff account guard. | |
| 204 | - * | |
| 205 | - * The guard runs first and can only refuse. When it clears the target, | |
| 206 | - * that target's roles are allowed through WooCommerce's shop_manager | |
| 207 | - * role-name restriction for the duration of the check, so a cleared | |
| 208 | - * subscriber or membership-plugin role is judged by capability. | |
| 209 | - * | |
| 210 | - * @param int $target_id Target user ID. | |
| 211 | - * @param callable $check Returns WooCommerce's verdict. | |
| 212 | - * | |
| 213 | - * @return WP_Error|bool | |
| 214 | - */ | |
| 215 | - private function wcpos_guarded_permissions_check( int $target_id, callable $check ) { | |
| 216 | - if ( ! Customer_Account_Guard::can_modify( get_current_user_id(), $target_id ) ) { | |
| 217 | - return Customer_Account_Guard::denial(); | |
| 218 | - } | |
| 219 | - | |
| 220 | - $restore = Customer_Account_Guard::allow_target_roles( $target_id ); | |
| 221 | - | |
| 222 | - try { | |
| 223 | - return $check(); | |
| 224 | - } finally { | |
| 225 | - $restore(); | |
| 226 | - } | |
| 154 | + return Permission_Rules::verdict( 'customers', 'delete', (int) $request['id'], 0, 'v1', $request->get_params() ); | |
| 227 | 155 | } |
| 228 | 156 | |
| 229 | 157 | /** |
| 230 | 158 | * Add extra fields to WP_REST_Controller::get_collection_params(). |