From 474715bd1b0f769461f7f509f99d5cead34ba5c0 Mon Sep 17 00:00:00 2001 From: Abhijit Rakas Date: Tue, 27 Nov 2018 18:39:51 +0530 Subject: [PATCH] Fix code structure --- src/Auth_Command.php | 57 ++++++++++++++++++++++---------------------- src/auth-utils.php | 6 +++-- src/helper/hooks.php | 3 ++- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/Auth_Command.php b/src/Auth_Command.php index c2e17dc..8ea344d 100644 --- a/src/Auth_Command.php +++ b/src/Auth_Command.php @@ -17,10 +17,11 @@ use EE\Model\Auth; use EE\Model\Whitelist; use Symfony\Component\Filesystem\Filesystem; -use function EE\Auth\Utils\verify_htpasswd_is_present; -use function EE\Site\Utils\auto_site_name; -use function EE\Site\Utils\get_site_info; -use function EE\Site\Utils\reload_global_nginx_proxy; +use EE\Model\Site; +use EE\Utils as EE_Utils; +use EE\Auth\Utils as Auth_Utils; +use EE\Site\Utils as Site_Utils; + class Auth_Command extends EE_Command { @@ -73,10 +74,10 @@ public function __construct() { */ public function create( $args, $assoc_args ) { - verify_htpasswd_is_present(); + Auth_Utils\verify_htpasswd_is_present(); $global = $this->populate_info( $args, __FUNCTION__ ); - $ips = \EE\Utils\get_flag_value( $assoc_args, 'ip' ); + $ips = EE_Utils\get_flag_value( $assoc_args, 'ip' ); $site_url = $global ? 'default' : $this->site_data->site_url; if ( $ips ) { @@ -96,8 +97,8 @@ public function create( $args, $assoc_args ) { * @throws Exception */ private function create_auth( array $assoc_args, bool $global, string $site_url ) { - $user = \EE\Utils\get_flag_value( $assoc_args, 'user', 'ee-' . EE\Utils\random_password( 6 ) ); - $pass = \EE\Utils\get_flag_value( $assoc_args, 'pass', EE\Utils\random_password() ); + $user = EE_Utils\get_flag_value( $assoc_args, 'user', 'ee-' . EE_Utils\random_password( 6 ) ); + $pass = EE_Utils\get_flag_value( $assoc_args, 'pass', EE_Utils\random_password() ); $auth_data = [ 'site_url' => $site_url, 'username' => $user, @@ -133,7 +134,7 @@ private function create_auth( array $assoc_args, bool $global, string $site_url } EE::log( 'Reloading global reverse proxy.' ); - reload_global_nginx_proxy(); + Site_Utils\reload_global_nginx_proxy(); EE::success( sprintf( 'Auth successfully updated for `%s` scope. New values added:', $this->site_data->site_url ) ); EE::line( 'User: ' . $user ); @@ -172,7 +173,7 @@ private function create_whitelist( string $site_url, string $ips ) { $this->generate_site_whitelist( $site_url ); } - reload_global_nginx_proxy(); + Site_Utils\reload_global_nginx_proxy(); } /** @@ -190,8 +191,8 @@ private function populate_info( $args, $command ) { $this->site_data = (object) [ 'site_url' => $args[0] ]; $global = true; } else { - $args = auto_site_name( $args, 'auth', $command ); - $this->site_data = get_site_info( $args, true, true, false ); + $args = Site_Utils\auto_site_name( $args, 'auth', $command ); + $this->site_data = Site_Utils\get_site_info( $args, true, true, false ); } return $global; @@ -203,13 +204,13 @@ private function populate_info( $args, $command ) { * @throws \EE\ExitException */ private function regen_admin_tools_auth() { - $admin_tools = \EE\Model\Site::where( 'admin_tools', '1' ); - $mailhog = \EE\Model\Site::where( 'mailhog_enabled', '1' ); + $admin_tools = Site::where( 'admin_tools', '1' ); + $mailhog = Site::where( 'mailhog_enabled', '1' ); if ( empty( $admin_tools ) && empty( $mailhog ) ) { return; } EE::log( 'Creating new auth for admin-tools only.' ); - \EE\Auth\Utils\init_global_admin_tools_auth(); + Auth_Utils\init_global_admin_tools_auth(); } /** @@ -376,11 +377,11 @@ private function put_ips_to_file( string $file, array $ips ) { */ public function update( $args, $assoc_args ) { - verify_htpasswd_is_present(); + Auth_Utils\verify_htpasswd_is_present(); $global = $this->populate_info( $args, __FUNCTION__ ); $site_url = $global ? 'default' : $this->site_data->site_url; - $ips = EE\Utils\get_flag_value( $assoc_args, 'ip' ); + $ips = EE_Utils\get_flag_value( $assoc_args, 'ip' ); if ( $ips ) { $this->update_whitelist( $site_url, $ips ); @@ -396,13 +397,13 @@ public function update( $args, $assoc_args ) { * @param string $site_url */ private function update_auth( array $assoc_args, string $site_url ) { - $user = EE\Utils\get_flag_value( $assoc_args, 'user' ); + $user = EE_Utils\get_flag_value( $assoc_args, 'user' ); if ( ! $user ) { EE::error( 'Please provide auth user with --user flag' ); } - $pass = EE\Utils\get_flag_value( $assoc_args, 'pass', EE\Utils\random_password() ); + $pass = EE_Utils\get_flag_value( $assoc_args, 'pass', EE_Utils\random_password() ); $auths = $this->get_auths( $site_url, $user ); @@ -418,7 +419,7 @@ private function update_auth( array $assoc_args, string $site_url ) { } EE::log( 'Reloading global reverse proxy.' ); - reload_global_nginx_proxy(); + Site_Utils\reload_global_nginx_proxy(); EE::success( sprintf( 'Auth successfully updated for `%s` scope. New values added:', $this->site_data->site_url ) ); EE::line( 'User: ' . $user ); @@ -464,7 +465,7 @@ private function update_whitelist( string $site_url, string $ips ) { $this->generate_site_whitelist( $site_url ); } - reload_global_nginx_proxy(); + Site_Utils\reload_global_nginx_proxy(); } @@ -526,14 +527,14 @@ private function get_auths( $site_url, $user, $error_if_empty = true ) { */ public function delete( $args, $assoc_args ) { - verify_htpasswd_is_present(); + Auth_Utils\verify_htpasswd_is_present(); $global = $this->populate_info( $args, __FUNCTION__ ); $site_url = $global ? 'default' : $this->site_data->site_url; - $ip = EE\Utils\get_flag_value( $assoc_args, 'ip' ); + $ip = EE_Utils\get_flag_value( $assoc_args, 'ip' ); if ( ! $ip ) { - $user = EE\Utils\get_flag_value( $assoc_args, 'user' ); + $user = EE_Utils\get_flag_value( $assoc_args, 'user' ); $auths = $this->get_auths( $site_url, $user ); foreach ( $auths as $auth ) { @@ -554,7 +555,7 @@ public function delete( $args, $assoc_args ) { EE::success( $success_message ); EE::log( 'Reloading global reverse proxy.' ); - reload_global_nginx_proxy(); + Site_Utils\reload_global_nginx_proxy(); } else { if ( 'all' === $ip ) { @@ -597,7 +598,7 @@ public function delete( $args, $assoc_args ) { $this->generate_site_whitelist( $site_url ); } - reload_global_nginx_proxy(); + Site_Utils\reload_global_nginx_proxy(); } } @@ -637,7 +638,7 @@ public function list( $args, $assoc_args ) { $global = $this->populate_info( $args, __FUNCTION__ ); $site_url = $global ? 'default' : $this->site_data->site_url; - $ip = \EE\Utils\get_flag_value( $assoc_args, 'ip' ); + $ip = EE_Utils\get_flag_value( $assoc_args, 'ip' ); if ( $ip ) { $whitelists = Whitelist::where( 'site_url', $site_url ); @@ -657,7 +658,7 @@ public function list( $args, $assoc_args ) { if ( empty( $auths ) ) { EE::error( 'Auth does not exists on global.' ); } - $format = \EE\Utils\get_flag_value( $assoc_args, 'format' ); + $format = EE_Utils\get_flag_value( $assoc_args, 'format' ); if ( 'table' === $format ) { $log_msg = $admin_tools_auth ? 'This auth is applied only on admin-tools.' : ''; } diff --git a/src/auth-utils.php b/src/auth-utils.php index 8752e1e..ff6ed70 100644 --- a/src/auth-utils.php +++ b/src/auth-utils.php @@ -5,6 +5,8 @@ use EE; use EE\Model\Auth; +use EE\Utils as EE_Utils; +use EE\Service\Utils as Service_Utils; /** * Initialize global admin tools auth if it's not present. @@ -26,7 +28,7 @@ function init_global_admin_tools_auth( $display_log = true ) { verify_htpasswd_is_present(); - $pass = \EE\Utils\random_password(); + $pass = EE_Utils\random_password(); $auth_data = [ 'site_url' => 'default_admin_tools', 'username' => 'easyengine', @@ -51,7 +53,7 @@ function init_global_admin_tools_auth( $display_log = true ) { */ function verify_htpasswd_is_present() { - EE\Service\Utils\nginx_proxy_check(); + Service_Utils\nginx_proxy_check(); EE::debug( 'Verifying htpasswd is present.' ); if ( EE::exec( sprintf( 'docker exec %s sh -c \'command -v htpasswd\'', EE_PROXY_TYPE ) ) ) { return; diff --git a/src/helper/hooks.php b/src/helper/hooks.php index fe494f5..4319402 100644 --- a/src/helper/hooks.php +++ b/src/helper/hooks.php @@ -8,6 +8,7 @@ use EE\Model\Site; use EE\Model\Whitelist; use Symfony\Component\Filesystem\Filesystem; +use EE\Site\Utils as Site_Utils; /** * Hook to cleanup auth entries and whitelisted ips if any. @@ -44,7 +45,7 @@ function cleanup_auth_and_whitelist( $site_url ) { $fs->remove( $site_whitelist_file ); } - \EE\Site\Utils\reload_global_nginx_proxy(); + Site_Utils\reload_global_nginx_proxy(); } EE::add_hook( 'site_cleanup', 'cleanup_auth_and_whitelist' );