Skip to content

Commit

Permalink
Allow admins to configure U2F keys for other users (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparsd authored Apr 30, 2020
1 parent 121993c commit 2b4ce5c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 14 deletions.
24 changes: 19 additions & 5 deletions class-two-factor-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ public static function is_valid_user_action( $user_id, $action ) {
);
}

/**
* Get the ID of the user being edited.
*
* @return integer
*/
public static function current_user_being_edited() {
// Try to resolve the user ID from the request first.
if ( ! empty( $_REQUEST['user_id'] ) ) {
$user_id = intval( $_REQUEST['user_id'] );

if ( current_user_can( 'edit_user', $user_id ) ) {
return $user_id;
}
}

return get_current_user_id();
}

/**
* Trigger our custom update action if a valid
* action request is detected and passes the nonce check.
Expand All @@ -250,11 +268,7 @@ public static function is_valid_user_action( $user_id, $action ) {
*/
public static function trigger_user_settings_action() {
$action = filter_input( INPUT_GET, self::USER_SETTINGS_ACTION_QUERY_VAR, FILTER_SANITIZE_STRING );
$user_id = filter_input( INPUT_GET, 'user_id', FILTER_SANITIZE_NUMBER_INT );

if ( empty( $user_id ) && is_user_logged_in() ) {
$user_id = get_current_user_id();
}
$user_id = self::current_user_being_edited();

if ( ! empty( $action ) && self::is_valid_user_action( $user_id, $action ) ) {
/**
Expand Down
2 changes: 0 additions & 2 deletions providers/class.two-factor-fido-u2f-admin-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ public function inline_edit() {
<td colspan="<?php echo esc_attr( $this->get_column_count() ); ?>" class="colspanchange">
<fieldset>
<div class="inline-edit-col">
<h4><?php esc_html_e( 'Quick Edit', 'two-factor' ); ?></h4>

<label>
<span class="title"><?php esc_html_e( 'Name', 'two-factor' ); ?></span>
<span class="input-text-wrap"><input type="text" name="name" class="ptitle" value="" /></span>
Expand Down
16 changes: 11 additions & 5 deletions providers/class.two-factor-fido-u2f-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public static function enqueue_assets( $hook ) {
return;
}

$user_id = get_current_user_id();
$user_id = Two_Factor_Core::current_user_being_edited();
if ( ! $user_id ) {
return;
}

$security_keys = Two_Factor_FIDO_U2F::get_security_keys( $user_id );

// @todo Ensure that scripts don't fail because of missing u2fL10n.
Expand Down Expand Up @@ -81,6 +85,7 @@ public static function enqueue_assets( $hook ) {
*/

$translation_array = array(
'user_id' => $user_id,
'register' => array(
'request' => $req,
'sigs' => $sigs,
Expand Down Expand Up @@ -255,9 +260,11 @@ public static function catch_submission( $user_id ) {
* @static
*/
public static function catch_delete_security_key() {
$user_id = get_current_user_id();
if ( ! empty( $_REQUEST['delete_security_key'] ) ) {
$user_id = Two_Factor_Core::current_user_being_edited();

if ( ! empty( $user_id ) && ! empty( $_REQUEST['delete_security_key'] ) ) {
$slug = $_REQUEST['delete_security_key'];

check_admin_referer( "delete_security_key-{$slug}", '_nonce_delete_security_key' );

Two_Factor_FIDO_U2F::delete_security_key( $user_id, $slug );
Expand Down Expand Up @@ -316,8 +323,7 @@ public static function wp_ajax_inline_save() {
wp_die();
}

$user_id = get_current_user_id();

$user_id = Two_Factor_Core::current_user_being_edited();
$security_keys = Two_Factor_FIDO_U2F::get_security_keys( $user_id );
if ( ! $security_keys ) {
wp_die();
Expand Down
2 changes: 1 addition & 1 deletion providers/class.two-factor-fido-u2f.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Two_Factor_FIDO_U2F extends Two_Factor_Provider {
*
* @var string
*/
const U2F_ASSET_VERSION = '0.2.0';
const U2F_ASSET_VERSION = '0.2.1';

/**
* Ensures only one instance of this class exists in memory at any one time.
Expand Down
3 changes: 2 additions & 1 deletion providers/js/fido-u2f-admin-inline-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ var inlineEditKey;

params = {
action: 'inline-save-key',
keyHandle: id
keyHandle: id,
user_id: window.u2fL10n.user_id
};

fields = $( '#edit-' + id ).find( ':input' ).serialize();
Expand Down

0 comments on commit 2b4ce5c

Please sign in to comment.