Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
Added #11 a new configuration variable "read_only" to prevent issues …
Browse files Browse the repository at this point in the history
…with permissions and ability to force an application into read-only mode. Added tests and modified readme.
  • Loading branch information
timothymarois committed Feb 9, 2018
1 parent 7ba1d6b commit 2e7f8e0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Change Log
==========

### 02/09/2018 - 1.0.14
* Added #11 a new configuration variable `read_only`. By default `false`, when set to `true` no modifications can be made to the database and if you attempt to make a `save()`, `delete()`, `truncate()` or `flush()` an exception will be thrown as those methods are not allowed to be used within read only mode. Also, the database will not attemp to create a new direction if one does not exist during read-only mode, this can become an issue if you don't have permission to do so, read only tries to solve that. When set to `false` the database functions as normal.

### 12/14/2017 - 1.0.13
* Added #10 a new configuration variable `safe_filename`. By default `true`, suppresses any file name errors and converts the file name to a valid name, if set to `false`, an exception will be thrown upon a invalid name. All users who update will notice no errors will appear upon a invalid name. Set `safe_filename` to `false` if you prefer the exception to be thrown.

Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Use `composer require tmarois/filebase`

If you want to modify the `composer.json` manually, add `"tmarois/filebase" : "^1.0"` to your `required`

You do not need to use composer, just download the files, and include it within your application, it does not have any dependencies, you will just need to keep it updated with any future releases.
If you do not want to use composer, download the files, and include it within your application, it does not have any dependencies, you will just need to keep it updated with any future releases.

## Usage

Expand Down Expand Up @@ -84,6 +84,7 @@ $db = new \Filebase\Database([
'cache_expires' => 1800,
'pretty' => true,
'safe_filename' => true,
'read_only' => false,
'validate' => [
'name' => [
'valid.type' => 'string',
Expand All @@ -102,7 +103,8 @@ $db = new \Filebase\Database([
|`cache` |bool |false |Stores [query](https://github.com/tmarois/Filebase#8-queries) results into cache for faster loading. |
|`cache_expire` |int |1800 |How long caching will last (in seconds) |
|`pretty` |bool |true |Store the data for human readability? Pretty Print |
|`safe_filename` |bool |true |Automatically converts the file name to a valid name |
|`safe_filename` |bool |true |Automatically converts the file name to a valid name (added: 1.0.13) |
|`read_only` |bool |false |Prevents the database from creating/modifying files or directories (added: 1.0.14) |


## (2) Formatting
Expand Down
10 changes: 10 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ class Config
public $safe_filename = true;


/**
* $read_only
* (if true) We will not attempt to create the database directory or allow the user to create anything
* (if false) Functions as normal
*
* default false
*/
public $read_only = false;


/**
* $backupLocation
* The location to store backups
Expand Down
20 changes: 19 additions & 1 deletion src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Database
* Stores the version of Filebase
* use $db->getVersion()
*/
const VERSION = '1.0.13';
const VERSION = '1.0.14';


//--------------------------------------------------------------------
Expand All @@ -35,6 +35,9 @@ public function __construct(array $config)
{
$this->config = new Config($config);

// if we are set to read only, don't care to look at the directory.
if ($this->config->read_only === true) return false;

// Check directory and create it if it doesn't exist
if (!is_dir($this->config->dir))
{
Expand Down Expand Up @@ -215,6 +218,11 @@ public function count()
*/
public function save(Document $document, $wdata = '')
{
if ($this->config->read_only === true)
{
throw new \Exception("This database is set to be read-only. No modifications can be made.");
}

$id = $document->getId();
$file_extension = $this->config->format::getFileExtension();
$file_location = $this->config->dir.'/'.Filesystem::validateName($id, $this->config->safe_filename).'.'.$file_extension;
Expand Down Expand Up @@ -289,6 +297,11 @@ protected function read($name)
*/
public function delete(Document $document)
{
if ($this->config->read_only === true)
{
throw new \Exception("This database is set to be read-only. No modifications can be made.");
}

return Filesystem::delete($this->config->dir.'/'.Filesystem::validateName($document->getId(), $this->config->safe_filename).'.'.$this->config->format::getFileExtension());
}

Expand Down Expand Up @@ -321,6 +334,11 @@ public function truncate()
*/
public function flush($confirm = false)
{
if ($this->config->read_only === true)
{
throw new \Exception("This database is set to be read-only. No modifications can be made.");
}

if ($confirm===true)
{
$documents = $this->findAll(false);
Expand Down
45 changes: 44 additions & 1 deletion tests/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,56 @@ class DatabaseTest extends \PHPUnit\Framework\TestCase
public function testVersion()
{
$db = new \Filebase\Database([
'dir' => __DIR__.'/databases'
'dir' => __DIR__.'/databases',
'read_only' => true
]);

$this->assertRegExp('/[0-9]+\.[0-9]+\.[0-9]+/', $db->version());
}



public function testReadonlyBadFlush()
{
$this->expectException(\Exception::class);

$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'read_only' => true
]);

$db->flush(true);
}


public function testReadonlyBadTurncate()
{
$this->expectException(\Exception::class);

$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'read_only' => true
]);

$db->truncate();
}



public function testDatabaseBadSave()
{
$this->expectException(\Exception::class);

$db = new \Filebase\Database([
'dir' => __DIR__.'/databases',
'read_only' => true
]);

$db->get('test1')->set(['key'=>'value'])->save();
}



public function testMissingFormatClass()
{
$this->expectException(\Exception::class);
Expand Down

0 comments on commit 2e7f8e0

Please sign in to comment.