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

Fix code structure #33

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
57 changes: 29 additions & 28 deletions src/Auth_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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 ) {
Expand All @@ -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,
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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;
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );

Expand All @@ -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 );
Expand Down Expand Up @@ -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();

}

Expand Down Expand Up @@ -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 ) {
Expand All @@ -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 ) {
Expand Down Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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 );
Expand All @@ -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.' : '';
}
Expand Down
6 changes: 4 additions & 2 deletions src/auth-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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',
Expand All @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/helper/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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' );