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 whitelist file #6

Merged
merged 1 commit into from
Aug 24, 2018
Merged
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
32 changes: 20 additions & 12 deletions src/Auth_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,7 @@ public function whitelist( $args, $assoc_args ) {
*/
private function whitelist_create( $file, $user_ips, $existing_ips ) {

$file_content = '';
foreach ( $user_ips as $ip ) {
$file_content .= "allow $ip;" . PHP_EOL;
}
$this->fs->dumpFile( $file, $file_content );
$this->put_ips_to_file( $file, $user_ips );
EE::success( sprintf( 'Created whitelist for `%s` scope with %s IP\'s.', $this->site_data->site_url, implode( ',', $user_ips ) ) );
}

Expand All @@ -225,12 +221,8 @@ private function whitelist_create( $file, $user_ips, $existing_ips ) {
*/
private function whitelist_append( $file, $user_ips, $existing_ips ) {

$all_ips = array_unique( array_merge( $user_ips, $existing_ips ) );
$file_content = '';
foreach ( $all_ips as $individual_ip ) {
$file_content .= "allow $individual_ip;" . PHP_EOL;
}
$this->fs->dumpFile( $file, $file_content );
$all_ips = array_unique( array_merge( $user_ips, $existing_ips ) );
$this->put_ips_to_file( $file, $all_ips );
EE::success( sprintf( 'Appended %s IP\'s to whitelist of `%s` scope', implode( ',', $user_ips ), $this->site_data->site_url ) );
}

Expand Down Expand Up @@ -302,7 +294,7 @@ private function get_ips_from_file( $global ) {
$file .= $global ? 'default_acl' : $this->site_data->site_url . '_acl';
$existing_ips = [];
if ( $this->fs->exists( $file ) ) {
$existing_ips_in_file = array_filter( explode( PHP_EOL, trim( file_get_contents( $file ) ) ), 'strlen' );
$existing_ips_in_file = array_slice( array_filter( explode( PHP_EOL, file_get_contents( $file ) ), 'trim' ), 1, - 1 );
foreach ( $existing_ips_in_file as $ip_in_file ) {
$existing_ips[] = str_replace( [ 'allow ', ';' ], '', trim( $ip_in_file ) );
}
Expand All @@ -311,6 +303,22 @@ private function get_ips_from_file( $global ) {
return $existing_ips;
}

/**
* Function to put list of ip's into a file.
*
* @param string $file Path of file to write ip's in.
* @param array $ips List of ip's.
*/
private function put_ips_to_file( $file, $ips ) {

$file_content = 'satisfy any;' . PHP_EOL;
foreach ( $ips as $ip ) {
$file_content .= "allow $ip;" . PHP_EOL;
}
$file_content .= 'deny all;';
$this->fs->dumpFile( $file, $file_content );
}

/**
* Function to populate basic info from args
*
Expand Down