Make WordPress Core

Ticket #54481: 54481-v1.patch.txt

File 54481-v1.patch.txt, 3.3 KB (added by desmith, 3 years ago)

First draft version of the discussed patch, creating is_password_reset_allowed() and adding a few calls to same

Line 
1diff -ur wordpress-582-orig/wp-admin/includes/class-wp-users-list-table.php wordpress/wp-admin/includes/class-wp-users-list-table.php
2--- wordpress-582-orig/wp-admin/includes/class-wp-users-list-table.php  2021-06-08 17:21:57.000000000 -0500
3+++ wordpress/wp-admin/includes/class-wp-users-list-table.php   2021-11-23 11:25:41.881266000 -0600
4@@ -476,7 +476,7 @@
5                        }
6 
7                        // Add a link to send the user a reset password link by email.
8-                       if ( get_current_user_id() !== $user_object->ID && current_user_can( 'edit_user', $user_object->ID ) ) {
9+                       if ( get_current_user_id() !== $user_object->ID && current_user_can( 'edit_user', $user_object->ID ) && is_password_reset_allowed( $user_object->ID ) ) {
10                                $actions['resetpassword'] = "<a class='resetpassword' href='" . wp_nonce_url( "users.php?action=resetpassword&amp;users=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Send password reset' ) . '</a>';
11                        }
12 
13diff -ur wordpress-582-orig/wp-admin/user-edit.php wordpress/wp-admin/user-edit.php
14--- wordpress-582-orig/wp-admin/user-edit.php   2021-06-07 18:49:58.000000000 -0500
15+++ wordpress/wp-admin/user-edit.php    2021-11-23 11:28:22.566202900 -0600
16@@ -672,7 +672,7 @@
17 
18                <?php
19                // Allow admins to send reset password link.
20-               if ( ! IS_PROFILE_PAGE ) :
21+               if ( ( ! IS_PROFILE_PAGE ) && is_password_reset_allowed( $profileuser->ID ) ) :
22                        ?>
23        <tr class="user-generate-reset-link-wrap hide-if-no-js">
24                <th><?php _e( 'Password Reset' ); ?></th>
25diff -ur wordpress-582-orig/wp-includes/user.php wordpress/wp-includes/user.php
26--- wordpress-582-orig/wp-includes/user.php     2021-07-12 19:07:59.000000000 -0500
27+++ wordpress/wp-includes/user.php      2021-11-23 11:15:14.813439300 -0600
28@@ -2501,6 +2501,31 @@
29        return apply_filters( 'password_hint', $hint );
30 }
31 
32+/** Returns whether to allow a password to be reset.
33+ *
34+ * @since 6.0.0
35+ *
36+ * @param WP_User $user User to retrieve password reset key for.
37+ * @return bool True if password reset is allowed, false if not allowed.
38+ */
39+function is_password_reset_allowed( $user ) {
40+       $allow = true;
41+       if ( is_multisite() && is_user_spammy( $user ) ) {
42+               $allow = false;
43+       }
44+
45+       /**
46+        * Filters whether to allow a password to be reset.
47+        *
48+        * @since 2.7.0
49+        *
50+        * @param bool $allow Whether to allow the password to be reset. Default true.
51+        * @param int  $ID    The ID of the user attempting to reset a password.
52+        */
53+       $allow = apply_filters( 'allow_password_reset', $allow, $user->ID );
54+       return $allow;
55+}
56+
57 /**
58  * Creates, stores, then returns a password reset key for user.
59  *
60@@ -2539,25 +2564,8 @@
61         */
62        do_action( 'retrieve_password', $user->user_login );
63 
64-       $allow = true;
65-       if ( is_multisite() && is_user_spammy( $user ) ) {
66-               $allow = false;
67-       }
68-
69-       /**
70-        * Filters whether to allow a password to be reset.
71-        *
72-        * @since 2.7.0
73-        *
74-        * @param bool $allow Whether to allow the password to be reset. Default true.
75-        * @param int  $ID    The ID of the user attempting to reset a password.
76-        */
77-       $allow = apply_filters( 'allow_password_reset', $allow, $user->ID );
78-
79-       if ( ! $allow ) {
80+       if ( ! is_password_reset_allowed( $user ) ) {
81                return new WP_Error( 'no_password_reset', __( 'Password reset is not allowed for this user' ) );
82-       } elseif ( is_wp_error( $allow ) ) {
83-               return $allow;
84        }
85 
86        // Generate something random for a password reset key.