Created
July 8, 2014 14:28
-
-
Save Kevinlearynet/4c90901eaf7f92fb1dae to your computer and use it in GitHub Desktop.
WP Admin page to assist with a migration of users to Auth0 authentication and login
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Auth0 Migration: Admin Page | |
| * | |
| * Provide a UI in the WP admin for migrating users to Auth0 | |
| */ | |
| public function auth0_admin_page() { | |
| // requires admin privledges | |
| if ( current_user_can('manage_options') ) { | |
| global $wpdb; | |
| $wpdb->show_errors(); | |
| ?> | |
| <h2>Auth0 Migration</h2> | |
| <p>This process will reset all user passwords in <code>users</code> table for this WordPress install, sending the username, email and new password to Auth0 for a smooth transition.</p> | |
| <?php if ( ! empty( $_POST['emergency_accept'] ) && check_admin_referer( 'emergency_reset', 'emergency_reset' ) ) : ?> | |
| <p>We've begun the process of migrating all WordPress users to Auth0</p> | |
| <?php | |
| $results = $wpdb->get_results( 'SELECT ID FROM '.$wpdb->prefix.'users' ); | |
| if ( $results ) { | |
| foreach ( $results as $row ) { | |
| // gather user and site data | |
| $user_id = $row->ID; | |
| $user = get_userdata( $user_id ); | |
| $current_site = get_current_site(); | |
| $domain = ucfirst( $current_site->domain ); | |
| // generate new password | |
| $password = wp_generate_password(); | |
| // send data to Auth0 API | |
| // | |
| // PHP API calls go here, this may be slow though | |
| // perhaps storing in a new DB table for importing | |
| // into Auth0 database would be better? | |
| $args = array( | |
| 'user' => esc_attr( $user->user_login ), | |
| 'email' => sanitize_email( $user->user_email ), | |
| 'password' => $password, | |
| ); | |
| // set new password in WP | |
| wp_set_password( $password, $user_id ); | |
| // format email message | |
| $message = '<p>To keep your information safe and secure your <a href="' . wp_login_url() . '">' . $domain . '</a> password has been reset.</p>'; | |
| $message .= '<p>You can now login with:</p>'; | |
| $message .= '<ul>'; | |
| $message .= '<li><strong>Username</strong>: ' . $user->user_login . '</li>'; | |
| $message .= '<li><strong>Password</strong>: ' . $password . '</li>'; | |
| $message .= '</ul>'; | |
| $message .= '<p>We apologize for any inconvenience this causes,</p>'; | |
| $message .= '<p>Thank you</p>'; | |
| $message .= '<p>The ' . get_bloginfo('name') . 'Team</p>'; | |
| // notify user via email | |
| add_filter( 'wp_mail_content_type', create_function('', 'return "text/html";' ) ); | |
| wp_mail( $user->user_email, __( "$domain Security Update", 'rapidminer' ), $message ); | |
| // output status | |
| ?> | |
| <p>Password changed for <strong><?php echo $user->user_login; ?></strong></p> | |
| <?php | |
| } | |
| } | |
| ?> | |
| <h2>All done</h2> | |
| <?php else: ?> | |
| <p> | |
| <form action="" method="post">'; | |
| <?php echo wp_nonce_field('emergency_reset','emergency_reset'); ?> | |
| <input type="hidden" name="emergency_accept" value="yes"/><input type="submit" value="Reset all passwords"/></form> | |
| </p> | |
| <?php endif; // end POST emergency_accept | |
| } | |
| else { | |
| wp_die( "You don't have permission to use this password reset" ); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment