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

GH-50 Update ee auth list command by adding support for listing all auths across all sites #51

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
45 changes: 45 additions & 0 deletions src/Auth_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -684,9 +685,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 ) {

// 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;
}

$global = $this->populate_info( $args, __FUNCTION__ );
$site_url = $global ? 'default' : $this->site_data->site_url;
$ip = \EE\Utils\get_flag_value( $assoc_args, 'ip' );
Expand Down Expand Up @@ -730,5 +740,40 @@ public function list( $args, $assoc_args ) {
}
}
}

/**
* This will display all the auths of all the sites
*
* @return void
*/
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' ) );

Choose a reason for hiding this comment

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

@kirtangajjar While working on other issue , I came across a condition where there was not auth on any sites , so I have added the condition to check if there are no auths at all on any sites in that case it will simply show a error message instead of showing empty table


// 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
);

// Sorts the $result array to make sure that same sites appear together.
usort(
$result,
function ( $item1, $item2 ) {
return strcmp( $item1->sitename, $item2->sitename );
PiyushKhurana marked this conversation as resolved.
Show resolved Hide resolved
}
);

$formatter->display_items( $result );
}

}