Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve WP_Auth0_Options #418

Merged
merged 2 commits into from
Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/WP_Auth0_Lock10_Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ protected function build_settings( $settings ) {
}
}
if ( $this->_is_valid( $settings, 'lock_connections' ) ) {
$options_obj['allowedConnections'] = explode( ",", $settings['lock_connections'] );
$options_obj['allowedConnections'] = $this->wp_options->get_lock_connections();
}
if ( isset( $settings['extra_conf'] ) && trim( $settings['extra_conf'] ) !== '' ) {
$extra_conf_arr = json_decode( $settings['extra_conf'], true );
Expand Down
2 changes: 1 addition & 1 deletion lib/WP_Auth0_Lock_Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ protected function build_settings( $settings ) {
$options_obj['icon'] = $settings['icon_url'];
}
if ( $this->_is_valid( $settings, 'lock_connections' ) ) {
$options_obj['connections'] = explode( ",", $settings['lock_connections'] );
$options_obj['connections'] = $this->wp_options->get_lock_connections();
}
if ( isset( $settings['extra_conf'] ) && trim( $settings['extra_conf'] ) !== '' ) {
$extra_conf_arr = json_decode( $settings['extra_conf'], true );
Expand Down
163 changes: 100 additions & 63 deletions lib/WP_Auth0_Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

class WP_Auth0_Options extends WP_Auth0_Options_Generic {

protected static $instance = null;
protected static $_instance = null;
protected $_options_name = 'wp_auth0_settings';

public static function Instance() {
if ( self::$instance === null ) {
self::$instance = new WP_Auth0_Options;
if ( null=== self::$_instance ) {
self::$_instance = new WP_Auth0_Options;
}
return self::$instance;
return self::$_instance;
}

protected $options_name = 'wp_auth0_settings';

public function is_wp_registration_enabled() {
if ( is_multisite() ) {
return users_can_register_signup_filter();
}
return get_site_option( 'users_can_register', 0 ) == 1;
}

// TODO: Deprecate, not used
public function get_enabled_connections() {
return array( 'facebook', 'twitter', 'google-oauth2' );
}
Expand All @@ -43,9 +44,13 @@ public function get_default($key) {
return $defaults[$key];
}

/**
* Get the stored token signing algorithm
*
* @return string
*/
public function get_client_signing_algorithm() {
$client_signing_algorithm = $this->get('client_signing_algorithm', WP_Auth0_Api_Client::DEFAULT_CLIENT_ALG);
return $client_signing_algorithm;
return $this->get( 'client_signing_algorithm', WP_Auth0_Api_Client::DEFAULT_CLIENT_ALG );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this like this get(key, valueIfMissing)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

}

/**
Expand Down Expand Up @@ -129,92 +134,124 @@ public function get_logout_url() {
return add_query_arg( 'action', 'logout', site_url( 'wp-login.php', 'login' ) );
}

/**
* Get lock_connections as an array of strings
*
* @return array
*/
public function get_lock_connections() {
$connections = $this->get( 'lock_connections' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is the lock_connections a "string,of,connections"? It seems you are always manipulating arrays. Can it be defined as an array to avoid all this coming and going from one structure to the other?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The field in on the settings page is just a text field so comma-separated (or line break or pipe or whatever) is about the best we can do without a UI to force multiple, separate values. This is basically to handle user input

$connections = empty( $connections ) ? array() : explode( ',', $connections );
return array_map( 'trim', $connections );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the trim needed because it's a user input or because you don't trust the source? (i don't know where it comes from)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User input

}

/**
* Add a new connection to the lock_connections setting
*
* @param string $connection - connection name to add
*/
public function add_lock_connection( $connection ) {
$connections = $this->get_lock_connections();

// Add if it doesn't exist already
if ( ! array_key_exists( $connection, $connections ) ) {
$connections[] = $connection;
$connections = implode( ',', $connections );
$this->set( 'lock_connections', $connections );
}
}

/**
* Default settings when plugin is installed or reset
*
* @return array
*/
protected function defaults() {
return array(

// System
'version' => 1,
'metrics' => 1,
'last_step' => 1,
'auto_login' => 0,
'auto_login_method' => '',
'migration_token_id' => null,
'use_lock_10' => true,
'jwt_auth_integration' => false,
'amplificator_title' => '',
'amplificator_subtitle' => '',
'connections' => array(),
'auth0js-cdn' => 'https://cdn.auth0.com/js/auth0/9.1/auth0.min.js',

// Basic
'domain' => '',
'client_id' => '',
'client_secret' => '',
'client_secret_b64_encoded' => null,
'client_signing_algorithm' => WP_Auth0_Api_Client::DEFAULT_CLIENT_ALG,
'cache_expiration' => 1440,
'client_secret_b64_encoded' => null,
'domain' => '',
'form_title' => '',
'icon_url' => '',
'ip_range_check' => 0,
'ip_ranges' => '',
'lock_connections' => '',
'passwordless_enabled' => false,
'passwordless_method' => 'magiclink',
'passwordless_cdn_url' => '//cdn.auth0.com/js/lock-passwordless-2.2.min.js',
'use_lock_10' => true,
'cdn_url' => '//cdn.auth0.com/js/lock/11.1/lock.min.js',
'cdn_url_legacy' => '//cdn.auth0.com/js/lock-9.2.min.js',
'requires_verified_email' => true,
'auth0_app_token' => null,
'wordpress_login_enabled' => true,
'primary_color' => '',

'language' => '',
'language_dictionary' => '',

'custom_signup_fields' => '',

'social_big_buttons' => false,
'username_style' => '',
'extra_conf' => '',
'custom_css' => '',
'custom_js' => '',
'auth0_implicit_workflow' => false,
// Features
'password_policy' => 'fair',
'sso' => false,
'singlelogout' => false,
'gravatar' => true,
'jwt_auth_integration' => false,
'auth0_app_token' => null,
'api_audience' => null,
'mfa' => null,
'fullcontact' => null,
'fullcontact_rule' => null,
'fullcontact_apikey' => null,
'geo_rule' => null,
'income_rule' => null,
'link_auth0_users' => null,
'remember_users_session' => false,

'override_wp_avatars' => true,

// Appearance
'icon_url' => '',
'form_title' => '',
'social_big_buttons' => false,
'gravatar' => true,
'custom_css' => '',
'custom_js' => '',
'username_style' => '',
'primary_color' => '',
'language' => '',
'language_dictionary' => '',

// Advanced
'requires_verified_email' => true,
'remember_users_session' => false,
'default_login_redirection' => home_url(),
'passwordless_enabled' => false,
'passwordless_method' => 'magiclink',
'force_https_callback' => false,
'cdn_url' => 'https://cdn.auth0.com/js/lock/11.1/lock.min.js',
'cdn_url_legacy' => 'https://cdn.auth0.com/js/lock-9.2.min.js',
'passwordless_cdn_url' => 'https://cdn.auth0.com/js/lock-passwordless-2.2.min.js',
'lock_connections' => '',
'link_auth0_users' => null,
'auto_provisioning' => false,
'migration_ws' => false,
'migration_token' => null,
'migration_token_id' => null,
'migration_ips_filter' => false,
'migration_ips' => null,
'auto_login' => 0,
'auto_login_method' => '',
'auth0_implicit_workflow' => false,
'ip_range_check' => 0,
'ip_ranges' => '',
'valid_proxy_ip' => null,

'amplificator_title' => '',
'amplificator_subtitle' => '',

'connections' => array(),

'password_policy' => 'fair',

'force_https_callback' => false,

'auto_provisioning' => false,
'default_login_redirection' => home_url(),

'custom_signup_fields' => '',
'extra_conf' => '',
'social_twitter_key' => '',
'social_twitter_secret' => '',
'social_facebook_key' => '',
'social_facebook_secret' => '',
'auth0_server_domain' => 'auth0.auth0.com',
'auth0js-cdn' => '//cdn.auth0.com/js/auth0/9.1/auth0.min.js',
'metrics' => 1,

//DASHBOARD
// Dashboard
'chart_idp_type' => 'donut',
'chart_gender_type' => 'donut',
'chart_age_type' => 'donut',

'chart_age_from' => '10',
'chart_age_to' => '70',
'chart_age_step' => '5',
);
}
}
}
16 changes: 8 additions & 8 deletions lib/WP_Auth0_Options_Generic.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php

class WP_Auth0_Options_Generic {
protected $options_name = '';
protected $_options_name = '';
private $_opt = null;

public function get_options_name() {
return $this->options_name;
return $this->_options_name;
}

public function get_options() {
if ( empty( $this->_opt ) ) {
$options = get_option( $this->options_name, array() );
$options = get_option( $this->_options_name, array() );

if ( !is_array( $options ) )
$options = $this->defaults();
Expand All @@ -36,23 +36,23 @@ public function set( $key, $value, $should_update = true ) {
$this->_opt = $options;

if ( $should_update ) {
update_option( $this->options_name, $options );
update_option( $this->_options_name, $options );
}
}

public function update_all() {
update_option( $this->options_name, $this->_opt );
update_option( $this->_options_name, $this->_opt );
}

public function save() {
$options = $this->get_options();
update_option( $this->options_name, $options );
update_option( $this->_options_name, $options );
}
public function delete() {
delete_option( $this->options_name );
delete_option( $this->_options_name );
}

protected function defaults() {
return array();
}
}
}
11 changes: 1 addition & 10 deletions lib/admin/WP_Auth0_Admin_Advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,6 @@ public function connections_validation( $old_options, $input ) {

if ($input['passwordless_enabled'] && $input['passwordless_enabled'] != $old_options['passwordless_enabled']) {

// $check_if_enabled = explode(',', $input['lock_connections']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened to all these comments that are now removed? was it implemented somewhere?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They've been commented for a while

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but is it something you want to do someday? are you tracking it somewhere else or are they just gone for good? 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what it's for, it's obviously not doing anything, and it's related to the settings I'm working with so 👋

:)


foreach ($passwordless_connections as $alias => $name) {
if (strpos($input['passwordless_method'], $alias) !== false) {
$check_if_enabled[] = $name;
Expand All @@ -566,19 +564,12 @@ public function connections_validation( $old_options, $input ) {

} elseif ($input['passwordless_method'] != $old_options['passwordless_method']) {

// $check_if_enabled = explode(',', $input['lock_connections']);

foreach ($passwordless_connections as $name) {
if (strpos($input['passwordless_method'], $name) !== false) {
$check_if_enabled[] = $name;
}
}

} // elseif ($input['lock_connections'] != $old_options['lock_connections']) {

// $check_if_enabled = explode(',', $input['lock_connections']);

// }
}

if (!empty($check_if_enabled)) {

Expand Down