0 ) { $found = get_user_by( 'id', $user ); return $found ? $found : null; } return null; } /** * Block password authentication for agent accounts. * * Runs after core password verification (priority 20) and after * the AgentID callback has already set its own cookie — this only * refuses authentications that presented a password. * * @param WP_User|WP_Error|null $result Auth result so far. * @param string $username Supplied username. * @param string $password Supplied password. * @return WP_User|WP_Error|null */ public static function block_password_auth( $result, $username, $password ) { if ( '' === (string) $password || is_wp_error( $result ) || ! $result instanceof WP_User ) { return $result; } if ( AgentID_Auth::is_agent_user( $result->ID ) ) { return new WP_Error( 'agentid_password_disabled', __( 'This is an agent account. It can only sign in with AgentID.', 'agentkey-sign-in-agentid' ) ); } return $result; } /** * No password resets for agent accounts. * * @param bool $allow Whether reset is allowed. * @param int $user_id User ID. * @return bool */ public static function block_password_reset( $allow, $user_id ) { if ( AgentID_Auth::is_agent_user( (int) $user_id ) ) { return false; } return $allow; } /** * No application passwords for agent accounts. * * @param bool $available Whether app passwords are available. * @param WP_User|int $user The user. * @return bool */ public static function block_app_passwords( $available, $user ) { $resolved = self::as_user( $user ); if ( $resolved && AgentID_Auth::is_agent_user( $resolved->ID ) ) { return false; } return $available; } /** * Capabilities an agent account must never hold, under any role * and regardless of its owner: the site-administration set, the * data export/import pair, and WooCommerce store management. * * Denying only manage_options is not enough — an account that * keeps install_plugins, promote_users or unfiltered_html can * still take over a site between sign-ins. The same set decides * which roles an agent may be provisioned with (see * AgentID_Settings::provisionable_roles()) and which roles get * demoted at sign-in (see demote_admin_agents()). * * The WooCommerce entries mirror WC_Install::get_core_capabilities() * (manage_woocommerce, reports, and the product / shop_order / * shop_coupon management families), so a store-management role * such as Shop manager is refused for agents exactly like an * administrator role. The participation-level caps (read_product * and the assign_*_terms caps) are deliberately not listed: * without the management caps they grant nothing on their own, * and the WooCommerce Customer role — read only — is unaffected. * * @return string[] */ public static function hard_denied_caps() { return array( 'manage_options', 'edit_theme_options', 'edit_users', 'create_users', 'delete_users', 'promote_users', 'promote_user', 'remove_users', 'install_plugins', 'activate_plugins', 'delete_plugins', 'edit_plugins', 'install_themes', 'delete_themes', 'edit_themes', 'switch_themes', 'update_core', 'update_plugins', 'update_themes', 'update_languages', 'edit_files', 'edit_css', 'unfiltered_html', 'unfiltered_upload', 'manage_network', 'manage_network_users', 'manage_network_plugins', 'manage_network_themes', 'manage_network_options', 'upgrade_network', 'setup_network', 'delete_site', // Data movement: held by administrators and Shop manager; // an exporter run exfiltrates orders and customer data. 'export', 'import', // WooCommerce store management (core pair). 'manage_woocommerce', 'view_woocommerce_reports', // WooCommerce store management (product). 'edit_product', 'delete_product', 'edit_products', 'edit_others_products', 'publish_products', 'read_private_products', 'delete_products', 'delete_private_products', 'delete_published_products', 'delete_others_products', 'edit_private_products', 'edit_published_products', 'manage_product_terms', 'edit_product_terms', 'delete_product_terms', // WooCommerce store management (shop_order). 'edit_shop_order', 'delete_shop_order', 'edit_shop_orders', 'edit_others_shop_orders', 'publish_shop_orders', 'read_private_shop_orders', 'delete_shop_orders', 'delete_private_shop_orders', 'delete_published_shop_orders', 'delete_others_shop_orders', 'edit_private_shop_orders', 'edit_published_shop_orders', 'manage_shop_order_terms', 'edit_shop_order_terms', 'delete_shop_order_terms', // WooCommerce store management (shop_coupon). 'edit_shop_coupon', 'delete_shop_coupon', 'edit_shop_coupons', 'edit_others_shop_coupons', 'publish_shop_coupons', 'read_private_shop_coupons', 'delete_shop_coupons', 'delete_private_shop_coupons', 'delete_published_shop_coupons', 'delete_others_shop_coupons', 'edit_private_shop_coupons', 'edit_published_shop_coupons', 'manage_shop_coupon_terms', 'edit_shop_coupon_terms', 'delete_shop_coupon_terms', ); } /** * Ceiling an agent's capabilities at its owner's. * * @param array $allcaps All capabilities granted to the user. * @param array $caps Capabilities being checked. * @param array $args Filter args. * @param WP_User $user The user. * @return array */ public static function capability_ceiling( $allcaps, $caps, $args, $user ) { if ( ! $user instanceof WP_User || ! AgentID_Auth::is_agent_user( $user->ID ) ) { return $allcaps; } // An agent account must never administer the site — no matter // what its role says, and no matter what its owner can do. // This deny is unconditional; the owner ceiling below only // ever removes further capabilities. foreach ( self::hard_denied_caps() as $denied_cap ) { $allcaps[ $denied_cap ] = false; } $owner_id = (int) get_user_meta( $user->ID, 'agentid_owner_user_id', true ); if ( $owner_id <= 0 || $owner_id === (int) $user->ID ) { // No recorded owner: keep the account's role capabilities // (site administration already denied above). return $allcaps; } $owner = get_user_by( 'id', $owner_id ); if ( ! $owner ) { // Owner is gone: fall back to read-only. return array( 'read' => true ); } $owner_caps = is_array( $owner->allcaps ) ? $owner->allcaps : array(); foreach ( $allcaps as $cap => $granted ) { if ( $granted && empty( $owner_caps[ $cap ] ) && 'read' !== $cap ) { $allcaps[ $cap ] = false; } } return $allcaps; } /** * Short session lifetime for agent accounts. * * @param int $length Default cookie length in seconds. * @param int $user_id User ID. * @param bool $remember Whether "remember me" was requested. * @return int */ public static function session_length( $length, $user_id, $remember ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- $remember is part of the auth_cookie_expiration filter signature; agent sessions intentionally ignore it. if ( AgentID_Auth::is_agent_user( (int) $user_id ) ) { $hours = (int) agentid_sso_get_option( 'session_hours', 12 ); $hours = max( 1, min( 72, $hours ) ); return $hours * HOUR_IN_SECONDS; } return $length; } /** * Demote an agent account that somehow holds an administering role. * * @param string $user_login Username. * @param WP_User $user The user who signed in. */ public static function demote_admin_agents( $user_login, $user ) { if ( ! $user instanceof WP_User || ! AgentID_Auth::is_agent_user( $user->ID ) ) { return; } // Any role the plugin would refuse to provision — because it // grants a hard-denied capability — is demoted on sight, not // just roles holding manage_options. $provisionable = AgentID_Settings::provisionable_roles(); foreach ( (array) $user->roles as $role_slug ) { if ( ! isset( $provisionable[ $role_slug ] ) ) { $role = AgentID_Auth::safe_default_role(); if ( null !== $role ) { $user->set_role( $role ); } // No safe role exists: leave the role untouched — // the capability ceiling still hard-denies // site administration for this account. return; } } } /** * Show AgentID identity details on the user profile. * * @param WP_User $profile_user The profile being viewed. */ public static function profile_section( $profile_user ) { $is_agent = AgentID_Auth::is_agent_user( $profile_user->ID ); $linked_sub = (string) get_user_meta( $profile_user->ID, 'agentid_linked_sub', true ); if ( ! $is_agent && '' === $linked_sub && (int) get_current_user_id() !== (int) $profile_user->ID ) { return; } ?>

ID && agentid_sso_is_configured() ) : ?>