Skip to content

Commit 828e751

Browse files
authored
Merge pull request mongodb#2082 from fish3046/issue-2078
[3.x] Fix guarded to return always true
2 parents 828982f + 2a8c2fd commit 828e751

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
2222
- [Extending the base model](#extending-the-base-model)
2323
- [Soft Deletes](#soft-deletes)
2424
- [Dates](#dates)
25+
- [Guarding attributes](#guarding-attributes)
2526
- [Basic Usage](#basic-usage)
2627
- [MongoDB-specific operators](#mongodb-specific-operators)
2728
- [MongoDB-specific Geo operations](#mongodb-specific-geo-operations)
@@ -240,7 +241,7 @@ use Jenssegers\Mongodb\Auth\User as Authenticatable;
240241

241242
class User extends Authenticatable
242243
{
243-
244+
244245
}
245246
```
246247

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

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

267+
### Guarding attributes
268+
269+
When choosing between guarding attributes or marking some as fillable, Taylor Otwell prefers the fillable route.
270+
This is in light of [recent security issues described here](https://blog.laravel.com/security-release-laravel-61835-7240).
271+
272+
Keep in mind guarding still works, but you may experience unexpected behavior.
273+
266274
### Dates
267275

268276
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.

src/Jenssegers/Mongodb/Eloquent/Model.php

+11
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,17 @@ protected function getRelationsWithoutParent()
473473
return $relations;
474474
}
475475

476+
/**
477+
* Checks if column exists on a table. As this is a document model, just return true. This also
478+
* prevents calls to non-existent function Grammar::compileColumnListing()
479+
* @param string $key
480+
* @return bool
481+
*/
482+
protected function isGuardableColumn($key)
483+
{
484+
return true;
485+
}
486+
476487
/**
477488
* @inheritdoc
478489
*/

src/Jenssegers/Mongodb/Schema/Builder.php

-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,6 @@
77

88
class Builder extends \Illuminate\Database\Schema\Builder
99
{
10-
/**
11-
* @inheritdoc
12-
*/
13-
public function __construct(Connection $connection)
14-
{
15-
$this->connection = $connection;
16-
}
17-
1810
/**
1911
* @inheritdoc
2012
*/

tests/ModelTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function tearDown(): void
1919
Soft::truncate();
2020
Book::truncate();
2121
Item::truncate();
22+
Guarded::truncate();
2223
}
2324

2425
public function testNewModel(): void
@@ -722,4 +723,27 @@ public function testTruncateModel()
722723

723724
$this->assertEquals(0, User::count());
724725
}
726+
727+
public function testGuardedModel()
728+
{
729+
$model = new Guarded();
730+
731+
// foobar is properly guarded
732+
$model->fill(['foobar' => 'ignored', 'name' => 'John Doe']);
733+
$this->assertFalse(isset($model->foobar));
734+
$this->assertSame('John Doe', $model->name);
735+
736+
// foobar is guarded to any level
737+
$model->fill(['foobar->level2' => 'v2']);
738+
$this->assertNull($model->getAttribute('foobar->level2'));
739+
740+
// multi level statement also guarded
741+
$model->fill(['level1->level2' => 'v1']);
742+
$this->assertNull($model->getAttribute('level1->level2'));
743+
744+
// level1 is still writable
745+
$dataValues = ['array', 'of', 'values'];
746+
$model->fill(['level1' => $dataValues]);
747+
$this->assertEquals($dataValues, $model->getAttribute('level1'));
748+
}
725749
}

tests/models/Guarded.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
5+
6+
class Guarded extends Eloquent
7+
{
8+
protected $connection = 'mongodb';
9+
protected $collection = 'guarded';
10+
protected $guarded = ['foobar', 'level1->level2'];
11+
}

0 commit comments

Comments
 (0)