Skip to content

Commit

Permalink
Add consistent naming, linting and a unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
angusmcleod committed Apr 3, 2024
1 parent 85ae319 commit 1625d87
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
26 changes: 13 additions & 13 deletions admin/sso-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,16 +261,16 @@ public function register_sso_settings() {
'discourse_sso_client_settings_section'
);

add_settings_field(
'discourse_disable_sso_user_creation',
__( 'Disable creation of users', 'wp-discourse' ),
add_settings_field(
'discourse_sso_disable_create_user',
__( 'Disable user creation', 'wp-discourse' ),
array(
$this,
'sso_client_user_creation_checkbox',
'sso_client_disable_create_user_checkbox',
),
'discourse_sso_client',
'discourse_sso_client_settings_section'
);
'discourse_sso_client',
'discourse_sso_client_settings_section'
);

add_settings_field(
'discourse_sso_client_sync_logout',
Expand Down Expand Up @@ -596,20 +596,20 @@ public function discourse_sso_login_form_redirect_url_input() {
}

/**
* Outputs markup for sso-client-no-user-creation checkbox.
* Outputs markup for sso-client-disable-create-user checkbox.
*/
public function sso_client_user_creation_checkbox() {
public function sso_client_disable_create_user_checkbox() {
$this->form_helper->checkbox_input(
'sso-client-no-user-creation',
'sso-client-disable-create-user',
'discourse_sso_client',
__( 'If a user is not matched by email in wordpress, don\'t create a new user', 'wp-discourse' ),
__( 'Disable creation of new WordPress users', 'wp-discourse' ),
__(
"If a user is not found to be existing in wordpress, don't create one. This can be useful if you limit wordpress login to a few admins/editors but still want to use the sso client.",
'Only Discourse users with an email or id matching an existing WordPress user will be allowed to log in with Discourse.',
'wp-discourse'
)
);
}

/**
* Outputs markup for sso-client-sync-by-email checkbox.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/discourse.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ class Discourse {
'sso-client-login-form-change' => 0,
'sso-client-login-form-redirect' => '',
'sso-client-sync-by-email' => 0,
'sso-client-no-user-creation' => 0,
'sso-client-disable-create-user' => 0,
'sso-client-sync-logout' => 0,
);

Expand Down
37 changes: 21 additions & 16 deletions lib/sso-client/client.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,22 +231,22 @@ private function get_user_id() {
}
}

if ( empty( $user_query_results ) && ! empty( $this->options['sso-client-disable-create-user'] ) ) {
return new \WP_Error( 'no_matching_user' );
}

if ( empty( $user_query_results ) ) {
if ( empty( $this->options['sso-client-no-user-creation'] ) ) {
$user_password = wp_generate_password( 12, true );

$user_id = wp_create_user(
$this->get_sso_response( 'username' ),
$user_password,
$this->get_sso_response( 'email' )
);

do_action( 'wpdc_sso_client_after_create_user', $user_id );

return $user_id;
} else {
return new \WP_Error( 'no_such_user' );
}
$user_password = wp_generate_password( 12, true );

$user_id = wp_create_user(
$this->get_sso_response( 'username' ),
$user_password,
$this->get_sso_response( 'email' )
);

do_action( 'wpdc_sso_client_after_create_user', $user_id );

return $user_id;
}

return $user_query_results[0]->ID;
Expand Down Expand Up @@ -368,7 +368,12 @@ public function handle_login_errors( $errors ) {
case 'existing_user_login':
$message = __( 'There is already an account registered with the username supplied by Discourse. If this is you, login through WordPress and visit your profile page to sync your account with Discourse', 'wp-discourse' );
$errors->add( 'existing_user_login', $message );
break;
break;

case 'no_matching_user':
$message = __( 'No WordPress user matches your Discourse user.', 'wp-discourse' );
$errors->add( 'discourse_sso_no_matching_user', $message );
break;

default:
$message = __( 'Unhandled Error', 'wp-discourse' );
Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/test-sso-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ public function test_parse_request_update_user_failed() {
remove_filter( 'wpdc_sso_client_updated_user', array( $this, 'invalid_update_user_filter' ), 10 );
}

/**
* parse_request does not create new users if user creation is disabled.
*/
public function test_parse_request_disable_create_user() {
self::$plugin_options['sso-client-disable-create-user'] = 1;
$this->sso_client->setup_options( self::$plugin_options );

$parse_result = $this->sso_client->parse_request();

$log = $this->get_last_log();
$this->assertMatchesRegularExpression( '/sso_client.ERROR: parse_request.get_user_id/', $log );
$this->assertMatchesRegularExpression( '/"code":"no_matching_user"/', $log );
}

public function invalid_update_user_filter( $updated_user, $query ) {
$updated_user['ID'] = 23;
return $updated_user;
Expand Down

0 comments on commit 1625d87

Please sign in to comment.