Skip to content

Commit

Permalink
Add option to disable logs in Wordpress (#848)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamstraube authored Nov 12, 2022
1 parent 0ada204 commit 45d5a15
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/WP_Auth0_ErrorLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ private function update( array $log ) {
return update_option( self::OPTION_NAME, $log );
}

public static function check_is_disabled() {
$flag = wp_auth0_get_option( 'auth0_disable_logging' );
if ( is_bool( $flag ) ) {
return $flag;
}
return false;
}

/**
* Create a row in the error log.
*
Expand All @@ -127,6 +135,10 @@ private function update( array $log ) {
*/
public static function insert_error( $section, $error ) {

if ( self::check_is_disabled() ) {
return false;
}

$new_entry = [
'section' => $section,
'code' => 'unknown_code',
Expand Down
1 change: 1 addition & 0 deletions lib/WP_Auth0_Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ protected function defaults() {
'migration_ips' => '',
'valid_proxy_ip' => '',
'auth0_server_domain' => 'auth0.auth0.com',
'auth0_disable_logging' => false,
];
}
}
32 changes: 28 additions & 4 deletions lib/admin/WP_Auth0_Admin_Advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ public function init() {
'id' => 'wpa0_auth0_server_domain',
'function' => 'render_auth0_server_domain',
],
[
'name' => __( 'Disable Auth0 logging in Wordpress', 'wp-auth0' ),
'opt' => 'auth0_disable_logging',
'id' => 'wpa0_auth0_disable_logging',
'function' => 'render_auth0_disable_logging',
],
];

$this->init_option_section( '', 'advanced', $options );
Expand Down Expand Up @@ -221,8 +227,25 @@ public function render_force_https_callback( $args = [] ) {
public function render_auto_provisioning( $args = [] ) {
$this->render_switch( $args['label_for'], $args['opt_name'] );
$this->render_field_description(
__( 'Create new users in the WordPress database when signups are off. ', 'wp-auth0' ) .
__( 'Signups will not be allowed but successful Auth0 logins will add the user in WordPress', 'wp-auth0' )
__( 'Disables Auth0 logging within WordPress. ', 'wp-auth0' ) .
__( 'If enabled, Auth0 logging will be disabled in WordPress. Other Auth0 logs are unaffected by this switch', 'wp-auth0' )
);
}

/**
* Render form field and description for the `auth0_disable_logging` option.
* IMPORTANT: Internal callback use only, do not call this function directly!
*
* @param array $args - callback args passed in from add_settings_field().
*
* @see WP_Auth0_Admin_Generic::init_option_section()
* @see add_settings_field()
*/
public function render_auth0_disable_logging( $args = [] ) {
$this->render_switch( $args['label_for'], $args['opt_name'] );
$this->render_field_description(
__( 'A user session by default is kept for two days. ', 'wp-auth0' ) .
__( 'Enabling this setting will extend that and make the session be kept for 14 days', 'wp-auth0' )
);
}

Expand Down Expand Up @@ -361,8 +384,9 @@ public function basic_validation( array $input ) {
// `migration_token` is sanitized in $this->migration_ws_validation() below.
$input['migration_ips_filter'] = $this->sanitize_switch_val( $input['migration_ips_filter'] ?? null );
// `migration_ips` is sanitized in $this->migration_ips_validation() below.
$input['valid_proxy_ip'] = ( isset( $input['valid_proxy_ip'] ) ? $input['valid_proxy_ip'] : null );
$input['auth0_server_domain'] = $this->sanitize_text_val( $input['auth0_server_domain'] ?? null );
$input['valid_proxy_ip'] = ( isset( $input['valid_proxy_ip'] ) ? $input['valid_proxy_ip'] : null );
$input['auth0_server_domain'] = $this->sanitize_text_val( $input['auth0_server_domain'] ?? null );
$input['auth0_disable_logging'] = $this->sanitize_switch_val( $input['auth0_disable_logging'] ?? null );
return $input;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/Unit/ErrorLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,38 @@ public function testErrorLogEmpty() {
$this->assertEmpty( self::$error_log->get() );
}

/**
* Test that the error does not log when logging is disabled.
*/
public function testErrorDoesNotLogWhenDisabled() {
self::$opts->set( 'auth0_disable_logging', true );

$error_code = 999;
$error_msg = uniqid();
$wp_error = new WP_Error( $error_code, $error_msg );
WP_Auth0_ErrorLog::insert_error( __METHOD__, $wp_error );

$this->assertEmpty( self::$error_log->get() );
}

/**
* Test that the error does log when logging is enabled.
*/
public function testErrorDoesLogWhenLoggingEnabled() {
self::$opts->set( 'auth0_disable_logging', false );

$error_code = 999;
$error_msg = 'testmsg';
$wp_error = new WP_Error( $error_code, $error_msg );
WP_Auth0_ErrorLog::insert_error( __METHOD__, $wp_error );
$log = self::$error_log->get();

$this->assertEmpty( self::$error_log->get() );
$this->assertEquals( 1, $log[0]['count'] );
$this->assertEquals( $error_code, $log[0]['code'] );
$this->assertEquals( $error_msg, $log[0]['message'] );
}

/**
* Test that a basic added log entries are properly stored.
*/
Expand Down

0 comments on commit 45d5a15

Please sign in to comment.