From 0a9c1135d73625a5c59059f47b49abed11119fcc Mon Sep 17 00:00:00 2001 From: PiyushKhurana Date: Thu, 16 Sep 2021 19:52:13 +0530 Subject: [PATCH 1/8] v2 : listing all auths across all sites Add support for listing all auths across all sites using a hashmap approach --- src/Auth_Command.php | 75 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/Auth_Command.php b/src/Auth_Command.php index 1e36e9f..0436d22 100644 --- a/src/Auth_Command.php +++ b/src/Auth_Command.php @@ -684,9 +684,18 @@ public function delete( $args, $assoc_args ) { * # List all global auth * $ ee auth list global * + * # List all auths across all sites + * $ ee auth list + * */ public function list( $args, $assoc_args ) { + // handle only 'ee auth list' separately. + if ( empty( $args ) ) { + $this->display_all_auths(); + return; + } + $global = $this->populate_info( $args, __FUNCTION__ ); $site_url = $global ? 'default' : $this->site_data->site_url; $ip = \EE\Utils\get_flag_value( $assoc_args, 'ip' ); @@ -730,5 +739,71 @@ public function list( $args, $assoc_args ) { } } } + + /** + * This will display all the auths of all the sites + * + * @return void + * @throws Exception + */ + private function display_all_auths() { + + $start_time = microtime( true ); + + $some = array(); + $sites = Auth::all(); + $formatter = new EE\Formatter( $assoc_args, [ 'site', 'username', 'password' ] ); + + foreach ( $sites as $site ) { + + if ( $site->site_url !== 'default' ) { + + if ( ! isset( $some[ $site->site_url ] ) ) { + + $some[ $site->site_url ] = array( + array( + 'username' => $site->username, + 'password' => $site->password, + ), + ); + + } else { + + $new_arr = array( + 'username' => $site->username, + 'password' => $site->password, + ); + + array_push( $some[ $site->site_url ], $new_arr ); + } + } + } + $main = array(); + + foreach ( $some as $key => $value ) { + + $complete = array(); + $complete['site'] = $key; + + foreach ( $value as $v ) { + + $complete['username'] = $v['username']; + $complete['password'] = $v['password']; + + array_push( $main, $complete ); + + } + } + + $formatter->display_items( $main ); + + $end_time = microtime( true ); + $execution_time = ( $end_time - $start_time ); + + echo ' Execution time of script = ' . $execution_time . ' sec'; + + + } + } From 478081c60e44054cc22e121e696d7bc7f1b3e0cc Mon Sep 17 00:00:00 2001 From: PiyushKhurana Date: Fri, 17 Sep 2021 13:40:07 +0530 Subject: [PATCH 2/8] Add query logic to fetch site list --- src/Auth_Command.php | 31 ++++++++++++++----------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/src/Auth_Command.php b/src/Auth_Command.php index 0436d22..d7c27c0 100644 --- a/src/Auth_Command.php +++ b/src/Auth_Command.php @@ -751,31 +751,28 @@ private function display_all_auths() { $start_time = microtime( true ); $some = array(); - $sites = Auth::all(); + $sites = Auth::where( [ [ 'site_url', '!=', 'default' ] ] ); $formatter = new EE\Formatter( $assoc_args, [ 'site', 'username', 'password' ] ); foreach ( $sites as $site ) { - if ( $site->site_url !== 'default' ) { + if ( ! isset( $some[ $site->site_url ] ) ) { - if ( ! isset( $some[ $site->site_url ] ) ) { - - $some[ $site->site_url ] = array( - array( - 'username' => $site->username, - 'password' => $site->password, - ), - ); - - } else { - - $new_arr = array( + $some[ $site->site_url ] = array( + array( 'username' => $site->username, 'password' => $site->password, - ); + ), + ); - array_push( $some[ $site->site_url ], $new_arr ); - } + } else { + + $new_arr = array( + 'username' => $site->username, + 'password' => $site->password, + ); + + array_push( $some[ $site->site_url ], $new_arr ); } } $main = array(); From ccf9531729298f5aa6c5619cad1274277a9f4d8c Mon Sep 17 00:00:00 2001 From: PiyushKhurana Date: Fri, 17 Sep 2021 14:02:53 +0530 Subject: [PATCH 3/8] Rename variables and remove extra code --- src/Auth_Command.php | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/src/Auth_Command.php b/src/Auth_Command.php index d7c27c0..f53678e 100644 --- a/src/Auth_Command.php +++ b/src/Auth_Command.php @@ -747,18 +747,16 @@ public function list( $args, $assoc_args ) { * @throws Exception */ private function display_all_auths() { - - $start_time = microtime( true ); - - $some = array(); - $sites = Auth::where( [ [ 'site_url', '!=', 'default' ] ] ); - $formatter = new EE\Formatter( $assoc_args, [ 'site', 'username', 'password' ] ); + + $hash_table = array(); + $sites = Auth::where( [ [ 'site_url', '!=', 'default' ] ] ); + $formatter = new EE\Formatter( $assoc_args, [ 'site', 'username', 'password' ] ); foreach ( $sites as $site ) { - if ( ! isset( $some[ $site->site_url ] ) ) { + if ( ! isset( $hash_table[ $site->site_url ] ) ) { - $some[ $site->site_url ] = array( + $hash_table[ $site->site_url ] = array( array( 'username' => $site->username, 'password' => $site->password, @@ -767,39 +765,33 @@ private function display_all_auths() { } else { - $new_arr = array( + $new_entry = array( 'username' => $site->username, 'password' => $site->password, ); - array_push( $some[ $site->site_url ], $new_arr ); + array_push( $hash_table[ $site->site_url ], $new_entry ); } } - $main = array(); + $data = array(); - foreach ( $some as $key => $value ) { + foreach ( $hash_table as $key => $credentials ) { - $complete = array(); - $complete['site'] = $key; + $row = array(); - foreach ( $value as $v ) { + $row['site'] = $key; - $complete['username'] = $v['username']; - $complete['password'] = $v['password']; + foreach ( $credentials as $credential ) { - array_push( $main, $complete ); + $row['username'] = $credential['username']; + $row['password'] = $credential['password']; + + array_push( $data, $row ); } } - $formatter->display_items( $main ); - - $end_time = microtime( true ); - $execution_time = ( $end_time - $start_time ); - - echo ' Execution time of script = ' . $execution_time . ' sec'; - - + $formatter->display_items( $data ); } } From dc29314dcf7c58988d5db7febf50a4258556e3e1 Mon Sep 17 00:00:00 2001 From: PiyushKhurana Date: Fri, 17 Sep 2021 16:06:08 +0530 Subject: [PATCH 4/8] Add required comments --- src/Auth_Command.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Auth_Command.php b/src/Auth_Command.php index f53678e..fde9462 100644 --- a/src/Auth_Command.php +++ b/src/Auth_Command.php @@ -747,15 +747,24 @@ public function list( $args, $assoc_args ) { * @throws Exception */ private function display_all_auths() { - + + $data = array(); + + // To store auth data in hash table form ('key' as 'site url' and 'value' as 'array of credentials'). + // It will group all the auths for a particular site together. $hash_table = array(); - $sites = Auth::where( [ [ 'site_url', '!=', 'default' ] ] ); - $formatter = new EE\Formatter( $assoc_args, [ 'site', 'username', 'password' ] ); + + // Fetch all the auths across all the sites other than global auth. + $sites = Auth::where( [ [ 'site_url', '!=', 'default' ] ] ); + + $formatter = new EE\Formatter( $assoc_args, [ 'site', 'username', 'password' ] ); foreach ( $sites as $site ) { + // Check if this is first occurrence of site. if ( ! isset( $hash_table[ $site->site_url ] ) ) { + // If yes then it will create a new 'key' named 'site_url'. $hash_table[ $site->site_url ] = array( array( 'username' => $site->username, @@ -773,8 +782,8 @@ private function display_all_auths() { array_push( $hash_table[ $site->site_url ], $new_entry ); } } - $data = array(); + // Fetch auth data from hash table and transform it into a simple array. foreach ( $hash_table as $key => $credentials ) { $row = array(); From 52590289d1733b89f349cacdac2a4ee8a69d6d3a Mon Sep 17 00:00:00 2001 From: PiyushKhurana Date: Fri, 17 Sep 2021 17:21:35 +0530 Subject: [PATCH 5/8] Add support for listing global auths too --- src/Auth_Command.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Auth_Command.php b/src/Auth_Command.php index fde9462..ae3bc69 100644 --- a/src/Auth_Command.php +++ b/src/Auth_Command.php @@ -754,8 +754,8 @@ private function display_all_auths() { // It will group all the auths for a particular site together. $hash_table = array(); - // Fetch all the auths across all the sites other than global auth. - $sites = Auth::where( [ [ 'site_url', '!=', 'default' ] ] ); + // Fetch all the auths across all the sites. + $sites = Auth::all(); $formatter = new EE\Formatter( $assoc_args, [ 'site', 'username', 'password' ] ); From da1b04c85c1562db45bd10d7d646ac69ff23b2d7 Mon Sep 17 00:00:00 2001 From: PiyushKhurana Date: Tue, 21 Sep 2021 14:00:28 +0530 Subject: [PATCH 6/8] Refactor display_all_auths function - Add condition to check whether command is run from inside site directory or not - Use usort function to sort the array - Use array_map to add new field 'sitename' to each array element --- src/Auth_Command.php | 65 +++++++++++++------------------------------- 1 file changed, 19 insertions(+), 46 deletions(-) diff --git a/src/Auth_Command.php b/src/Auth_Command.php index ae3bc69..e44f84c 100644 --- a/src/Auth_Command.php +++ b/src/Auth_Command.php @@ -20,6 +20,7 @@ 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\get_site_name; use function EE\Site\Utils\reload_global_nginx_proxy; class Auth_Command extends EE_Command { @@ -690,8 +691,8 @@ public function delete( $args, $assoc_args ) { */ public function list( $args, $assoc_args ) { - // handle only 'ee auth list' separately. - if ( empty( $args ) ) { + // Display all the auths if `ee auth list' is run from outside of site directory. + if ( empty( $args ) && ! get_site_name() ) { $this->display_all_auths(); return; } @@ -748,59 +749,31 @@ public function list( $args, $assoc_args ) { */ private function display_all_auths() { - $data = array(); - - // To store auth data in hash table form ('key' as 'site url' and 'value' as 'array of credentials'). - // It will group all the auths for a particular site together. - $hash_table = array(); - // Fetch all the auths across all the sites. $sites = Auth::all(); - $formatter = new EE\Formatter( $assoc_args, [ 'site', 'username', 'password' ] ); - - foreach ( $sites as $site ) { - - // Check if this is first occurrence of site. - if ( ! isset( $hash_table[ $site->site_url ] ) ) { - - // If yes then it will create a new 'key' named 'site_url'. - $hash_table[ $site->site_url ] = array( - array( - 'username' => $site->username, - 'password' => $site->password, - ), - ); - - } else { - - $new_entry = array( - 'username' => $site->username, - 'password' => $site->password, - ); + $formatter = new EE\Formatter( $assoc_args, [ 'sitename', 'username', 'password' ] ); - array_push( $hash_table[ $site->site_url ], $new_entry ); - } - } - - // Fetch auth data from hash table and transform it into a simple array. - foreach ( $hash_table as $key => $credentials ) { - - $row = array(); - - $row['site'] = $key; - - foreach ( $credentials as $credential ) { + // Add a field named 'sitename' to the array , so that it can be displayed as heading in output. + $result = array_map( + function ( $site ) { + $site->sitename = $site->site_url; + return $site; + }, + $sites + ); - $row['username'] = $credential['username']; - $row['password'] = $credential['password']; + // Sorts the $result array to make sure that same sites appear together. + usort( + $result, + function ( $item1, $item2 ) { - array_push( $data, $row ); + return strcmp( $item1->sitename, $item2->sitename ); } - } + ); - $formatter->display_items( $data ); + $formatter->display_items( $result ); } } From 19c7e9a33d05cd2a8a7566a442efd1348e42b6d0 Mon Sep 17 00:00:00 2001 From: PiyushKhurana Date: Wed, 22 Sep 2021 14:02:39 +0530 Subject: [PATCH 7/8] Remove extra spaces --- src/Auth_Command.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Auth_Command.php b/src/Auth_Command.php index e44f84c..f6f7f76 100644 --- a/src/Auth_Command.php +++ b/src/Auth_Command.php @@ -745,15 +745,13 @@ public function list( $args, $assoc_args ) { * This will display all the auths of all the sites * * @return void - * @throws Exception */ private function display_all_auths() { // Fetch all the auths across all the sites. $sites = Auth::all(); - - $formatter = new EE\Formatter( $assoc_args, [ 'sitename', 'username', 'password' ] ); - + $formatter = new EE\Formatter( $assoc_args, array( 'sitename', 'username', 'password' ) ); + // Add a field named 'sitename' to the array , so that it can be displayed as heading in output. $result = array_map( function ( $site ) { @@ -762,17 +760,15 @@ function ( $site ) { }, $sites ); - + // Sorts the $result array to make sure that same sites appear together. usort( $result, function ( $item1, $item2 ) { - return strcmp( $item1->sitename, $item2->sitename ); - } ); - + $formatter->display_items( $result ); } From 6369281ce0ae36004a3c6dbbe6fbc660397a06f4 Mon Sep 17 00:00:00 2001 From: PiyushKhurana Date: Thu, 23 Sep 2021 10:19:20 +0530 Subject: [PATCH 8/8] Add error for no auths --- src/Auth_Command.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Auth_Command.php b/src/Auth_Command.php index f6f7f76..d384068 100644 --- a/src/Auth_Command.php +++ b/src/Auth_Command.php @@ -750,8 +750,11 @@ private function display_all_auths() { // Fetch all the auths across all the sites. $sites = Auth::all(); + if ( empty( $sites ) ) { + EE::error( 'No auths exits on any sites' ); + } $formatter = new EE\Formatter( $assoc_args, array( 'sitename', 'username', 'password' ) ); - + // Add a field named 'sitename' to the array , so that it can be displayed as heading in output. $result = array_map( function ( $site ) { @@ -760,7 +763,7 @@ function ( $site ) { }, $sites ); - + // Sorts the $result array to make sure that same sites appear together. usort( $result, @@ -768,7 +771,7 @@ function ( $item1, $item2 ) { return strcmp( $item1->sitename, $item2->sitename ); } ); - + $formatter->display_items( $result ); }