WordPress.org

Make WordPress Core

Ticket #51941: 51941.patch

File 51941.patch, 1.6 KB (added by engahmeds3ed, 9 months ago)

check if application name exists before for this user

  • src/wp-includes/class-wp-application-passwords.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/src/wp-includes/class-wp-application-passwords.php b/src/wp-includes/class-wp-application-passwords.php
    a b  
    6969                        return new WP_Error( 'application_password_empty_name', __( 'An application name is required to create an application password.' ) );
    7070                }
    7171
     72                if ( self::user_application_name_exists( $user_id, $args['name'] ) ) {
     73                        return new WP_Error( 'application_password_unique_name', __( 'An application name should be unique to create an application password.' ) );
     74                }
     75
    7276                $new_password    = wp_generate_password( static::PW_LENGTH, false );
    7377                $hashed_password = wp_hash_password( $new_password );
    7478
     
    162166                return null;
    163167        }
    164168
     169        /**
     170         * Check if application name exists before for this user.
     171         *
     172         * @param int    $user_id User ID.
     173         * @param string $name    Application name.
     174         *
     175         * @return bool Provided application name exists or not.
     176         */
     177        public static function user_application_name_exists( $user_id, $name ) {
     178                $passwords = static::get_user_application_passwords( $user_id );
     179
     180                foreach ( $passwords as $password ) {
     181                        if ( strtolower( $password['name'] ) === strtolower( $name ) ) {
     182                                return true;
     183                        }
     184                }
     185
     186                return false;
     187        }
     188
    165189        /**
    166190         * Updates an application password.
    167191         *