Skip to content

[3.x] Fix guarded to return always true #2082

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

Merged
merged 5 commits into from
Aug 20, 2020
Merged
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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
- [Extending the base model](#extending-the-base-model)
- [Soft Deletes](#soft-deletes)
- [Dates](#dates)
- [Guarding attributes](#guarding-attributes)
- [Basic Usage](#basic-usage)
- [MongoDB-specific operators](#mongodb-specific-operators)
- [MongoDB-specific Geo operations](#mongodb-specific-geo-operations)
Expand Down Expand Up @@ -240,7 +241,7 @@ use Jenssegers\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable
{

}
```

Expand All @@ -263,6 +264,13 @@ class User extends Model

For more information check [Laravel Docs about Soft Deleting](http://laravel.com/docs/eloquent#soft-deleting).

### Guarding attributes

When choosing between guarding attributes or marking some as fillable, Taylor Otwell prefers the fillable route.
This is in light of [recent security issues described here](https://blog.laravel.com/security-release-laravel-61835-7240).

Keep in mind guarding still works, but you may experience unexpected behavior.

### Dates

Eloquent allows you to work with Carbon or DateTime objects instead of MongoDate objects. Internally, these dates will be converted to MongoDate objects when saved to the database.
Expand Down
11 changes: 11 additions & 0 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,17 @@ protected function getRelationsWithoutParent()
return $relations;
}

/**
* Checks if column exists on a table. As this is a document model, just return true. This also
* prevents calls to non-existent function Grammar::compileColumnListing()
* @param string $key
* @return bool
*/
protected function isGuardableColumn($key)
{
return true;
}

/**
* @inheritdoc
*/
Expand Down
8 changes: 0 additions & 8 deletions src/Jenssegers/Mongodb/Schema/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@

class Builder extends \Illuminate\Database\Schema\Builder
{
/**
* @inheritdoc
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* @inheritdoc
*/
Expand Down
24 changes: 24 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public function tearDown(): void
Soft::truncate();
Book::truncate();
Item::truncate();
Guarded::truncate();
}

public function testNewModel(): void
Expand Down Expand Up @@ -722,4 +723,27 @@ public function testTruncateModel()

$this->assertEquals(0, User::count());
}

public function testGuardedModel()
{
$model = new Guarded();

// foobar is properly guarded
$model->fill(['foobar' => 'ignored', 'name' => 'John Doe']);
$this->assertFalse(isset($model->foobar));
$this->assertSame('John Doe', $model->name);

// foobar is guarded to any level
$model->fill(['foobar->level2' => 'v2']);
$this->assertNull($model->getAttribute('foobar->level2'));

// multi level statement also guarded
$model->fill(['level1->level2' => 'v1']);
$this->assertNull($model->getAttribute('level1->level2'));

// level1 is still writable
$dataValues = ['array', 'of', 'values'];
$model->fill(['level1' => $dataValues]);
$this->assertEquals($dataValues, $model->getAttribute('level1'));
}
}
11 changes: 11 additions & 0 deletions tests/models/Guarded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
declare(strict_types=1);

use Jenssegers\Mongodb\Eloquent\Model as Eloquent;

class Guarded extends Eloquent
{
protected $connection = 'mongodb';
protected $collection = 'guarded';
protected $guarded = ['foobar', 'level1->level2'];
}