| @@ -7,8 +7,10 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace WCPOS\WooCommercePOS\Services; |
| 9 | 9 | |
| 10 | 10 | use WCPOS\WooCommercePOS\Abstracts\Store; |
| 11 | +use WCPOS\WooCommercePOS\Services\Settings\Access_Section; | |
| 12 | +use WCPOS\WooCommercePOS\Sync\Pos_Uuid; | |
| 11 | 13 | use WP_User; |
| 12 | 14 | |
| 13 | 15 | /** |
| 14 | 16 | * Cashier Service class. |
| @@ -43,29 +45,21 @@ | ||
| 43 | 45 | |
| 44 | 46 | /** |
| 45 | 47 | * Get cashier UUID. |
| 46 | 48 | * |
| 47 | - * Note: usermeta is shared across all sites in a network, this can cause issues in the POS. | |
| 48 | - * We need to make sure that the cashier uuid is unique per site. | |
| 49 | + * Delegates to Pos_Uuid — the sole authority for `_woocommerce_pos_uuid` — so | |
| 50 | + * the /cashier endpoint and auth payloads serve the SAME identity as the | |
| 51 | + * /customers endpoint. The POS client keys its RxDB documents on this uuid, so | |
| 52 | + * a divergent value makes one person appear as two. Legacy multisite per-blog | |
| 53 | + * uuids (minted by an old version of this method) are adopted network-wide by | |
| 54 | + * the authority. | |
| 49 | 55 | * |
| 50 | 56 | * @param WP_User $user User object. |
| 51 | 57 | * |
| 52 | - * @return string UUID for the cashier. | |
| 58 | + * @return string UUID for the cashier ('' only if WooCommerce customer data is unavailable). | |
| 53 | 59 | */ |
| 54 | 60 | public function get_cashier_uuid( WP_User $user ): string { |
| 55 | - $meta_key = '_woocommerce_pos_uuid'; | |
| 56 | - | |
| 57 | - if ( \function_exists( 'is_multisite' ) && is_multisite() ) { | |
| 58 | - $meta_key = $meta_key . '_' . get_current_blog_id(); | |
| 59 | - } | |
| 60 | - | |
| 61 | - $uuid = get_user_meta( $user->ID, $meta_key, true ); | |
| 62 | - if ( ! $uuid ) { | |
| 63 | - $uuid = wp_generate_uuid4(); | |
| 64 | - update_user_meta( $user->ID, $meta_key, $uuid ); | |
| 65 | - } | |
| 66 | - | |
| 67 | - return $uuid; | |
| 61 | + return Pos_Uuid::ensure_user_uuid( $user ); | |
| 68 | 62 | } |
| 69 | 63 | |
| 70 | 64 | /** |
| 71 | 65 | * Get cashier data for API responses. |
| @@ -88,8 +82,10 @@ | ||
| 88 | 82 | 'email' => $user->user_email, |
| 89 | 83 | 'display_name' => $user->display_name, |
| 90 | 84 | 'nice_name' => $user->user_nicename, |
| 91 | 85 | 'roles' => array_values( $user->roles ), |
| 86 | + // The helper reports effective grants, including role-editor denies. | |
| 87 | + 'capabilities' => Access_Section::effective_capabilities( $user ), | |
| 92 | 88 | 'last_access' => $last_access ? $last_access : '', |
| 93 | 89 | 'avatar_url' => get_avatar_url( $user->ID ), |
| 94 | 90 | ); |
| 95 | 91 | |
| @@ -199,8 +195,72 @@ | ||
| 199 | 195 | * @return bool True if user has cashier permissions. |
| 200 | 196 | */ |
| 201 | 197 | public function has_cashier_permissions( WP_User $user ): bool { |
| 202 | 198 | return user_can( $user, 'publish_shop_orders' ); |
| 199 | + } | |
| 200 | + | |
| 201 | + /** | |
| 202 | + * POS baseline capabilities the user lacks. | |
| 203 | + * | |
| 204 | + * The baseline is what the POS needs to open and take a sale: the access gate, | |
| 205 | + * the cashier gate (publish_shop_orders — see has_cashier_permissions()), and the | |
| 206 | + * three reads every screen makes. Missing entries are reported, in this order, | |
| 207 | + * so a merchant can see which role or role-editor deny is responsible. | |
| 208 | + * | |
| 209 | + * @param WP_User $user User to check. | |
| 210 | + * @return string[] Missing capability names. | |
| 211 | + */ | |
| 212 | + public function missing_pos_capabilities( WP_User $user ): array { | |
| 213 | + $baseline = array( 'access_woocommerce_pos', 'publish_shop_orders', 'read_private_products', 'read_private_shop_orders', 'list_users' ); | |
| 214 | + | |
| 215 | + return array_values( array_filter( $baseline, fn( $cap ) => ! user_can( $user, $cap ) ) ); | |
| 216 | + } | |
| 217 | + | |
| 218 | + /** | |
| 219 | + * Whether the user clears the two gates the server already enforces. | |
| 220 | + * | |
| 221 | + * Only access_woocommerce_pos (the REST gate) and publish_shop_orders (the | |
| 222 | + * cashier gate) block entry; a user missing only a read capability can still | |
| 223 | + * work partially, and the corrected capability payload tells the client what | |
| 224 | + * is missing. | |
| 225 | + * | |
| 226 | + * @param WP_User $user User to check. | |
| 227 | + * @return bool True when both entry capabilities are granted. | |
| 228 | + */ | |
| 229 | + public function can_open_pos( WP_User $user ): bool { | |
| 230 | + $blocking = array_intersect( array( 'access_woocommerce_pos', 'publish_shop_orders' ), $this->missing_pos_capabilities( $user ) ); | |
| 231 | + | |
| 232 | + return empty( $blocking ); | |
| 233 | + } | |
| 234 | + | |
| 235 | + /** | |
| 236 | + * Describe missing baseline capabilities and how to grant them. | |
| 237 | + * | |
| 238 | + * @param WP_User $user User to check. | |
| 239 | + * @return string Diagnostic message, or empty unless the user is blocked by can_open_pos(). | |
| 240 | + */ | |
| 241 | + public function missing_pos_capabilities_message( WP_User $user ): string { | |
| 242 | + if ( $this->can_open_pos( $user ) ) { | |
| 243 | + return ''; | |
| 244 | + } | |
| 245 | + $missing = $this->missing_pos_capabilities( $user ); | |
| 246 | + /* translators: %s: Comma-separated missing capability names. */ | |
| 247 | + $message = sprintf( __( 'This account cannot use the POS. Missing capabilities: %s.', 'woocommerce-pos' ), implode( ', ', $missing ) ); | |
| 248 | + if ( count( $user->roles ) >= 2 ) { | |
| 249 | + $role_names = array_map( | |
| 250 | + function ( $slug ) { | |
| 251 | + $roles = wp_roles()->roles; | |
| 252 | + | |
| 253 | + return isset( $roles[ $slug ]['name'] ) ? translate_user_role( $roles[ $slug ]['name'] ) : $slug; | |
| 254 | + }, | |
| 255 | + $user->roles | |
| 256 | + ); | |
| 257 | + /* translators: %s: Comma-separated role names. */ | |
| 258 | + return $message . ' ' . sprintf( __( 'It has the roles %s. A capability denied on one role can override a grant from another, and role-editor plugins such as Members apply that deny first. Remove the extra role or clear the deny in the role editor.', 'woocommerce-pos' ), implode( ', ', $role_names ) ); | |
| 259 | + } | |
| 260 | + | |
| 261 | + /* translators: Guidance for granting missing POS capabilities. */ | |
| 262 | + return $message . ' ' . __( 'Grant them under WCPOS Settings, Access, or assign a role that has them.', 'woocommerce-pos' ); | |
| 203 | 263 | } |
| 204 | 264 | |
| 205 | 265 | /** |
| 206 | 266 | * Validate cashier access for API endpoints. |