From 98380a965aed8c94598e0bf8db178a6e298547b0 Mon Sep 17 00:00:00 2001 From: remyfrd Date: Tue, 20 Nov 2018 10:25:23 +0100 Subject: [PATCH] File permission option for file consumer --- lib/Segment/Consumer/File.php | 6 +++++- test/ConsumerFileTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/Segment/Consumer/File.php b/lib/Segment/Consumer/File.php index d59d71c..a0d385e 100644 --- a/lib/Segment/Consumer/File.php +++ b/lib/Segment/Consumer/File.php @@ -20,7 +20,11 @@ public function __construct($secret, $options = array()) { try { $this->file_handle = fopen($options["filename"], "a"); - chmod($options["filename"], 0777); + if (isset($options["filepermissions"])) { + chmod($options["filename"], $options["filepermissions"]); + } else { + chmod($options["filename"], 0777); + } } catch (Exception $e) { $this->handleError($e->getCode(), $e->getMessage()); } diff --git a/test/ConsumerFileTest.php b/test/ConsumerFileTest.php index 510ffaf..82ab848 100644 --- a/test/ConsumerFileTest.php +++ b/test/ConsumerFileTest.php @@ -123,6 +123,31 @@ public function testProductionProblems() $this->assertFalse($tracked); } + public function testFileSecurityCustom() { + $client = new Segment_Client( + "testsecret", + array( + "consumer" => "file", + "filename" => $this->filename, + "filepermissions" => 0700 + ) + ); + $tracked = $client->track(array("userId" => "some_user", "event" => "File PHP Event")); + $this->assertEquals(0700, (fileperms($this->filename) & 0777)); + } + + public function testFileSecurityDefaults() { + $client = new Segment_Client( + "testsecret", + array( + "consumer" => "file", + "filename" => $this->filename + ) + ); + $tracked = $client->track(array("userId" => "some_user", "event" => "File PHP Event")); + $this->assertEquals(0777, (fileperms($this->filename) & 0777)); + } + public function checkWritten($type) { exec("wc -l " . $this->filename, $output);