Skip to content

Commit

Permalink
Remove null checks and allow it to be queried (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
grantholle authored Feb 10, 2023
1 parent 658a6e2 commit 502221b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/Database/Scope/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function applyToModel(Model $model)
*/
public function applyToModelQuery($query, $table = null)
{
if (is_null($this->scope) || $this->onlyScopeRelations) {
if ($this->onlyScopeRelations) {
return $query;
}

Expand All @@ -129,10 +129,6 @@ public function applyToModelQuery($query, $table = null)
*/
public function applyToRelationQuery($query, $table)
{
if (is_null($this->scope)) {
return $query;
}

return $this->applyToQuery($query, $table);
}

Expand Down Expand Up @@ -166,8 +162,11 @@ public function applyToRelation(BelongsToMany $relation)
protected function applyToQuery($query, $table)
{
return $query->where(function ($query) use ($table) {
$query->where("{$table}.scope", $this->scope)
->orWhereNull("{$table}.scope");
$query->whereNull("{$table}.scope");

if (! is_null($this->scope)) {
$query->orWhere("{$table}.scope", $this->scope);
}
});
}

Expand Down
23 changes: 23 additions & 0 deletions tests/MultiTenancyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,26 @@ function syncing_abilities_is_properly_scoped($provider)
$this->assertEquals(1, $user->abilities()->count());
}

/**
* @test
* @dataProvider bouncerProvider
*/
function scoped_abilities_do_not_work_when_unscoped($provider)
{
list($bouncer, $user) = $provider();

$bouncer->scope()->to(1);
$bouncer->allow($user)->to(['write', 'read']);

$this->assertTrue($bouncer->can('write'));
$this->assertTrue($bouncer->can('read'));
$this->assertEquals(2, $user->abilities()->count());

$bouncer->scope()->to(null);
$this->assertFalse($bouncer->can('write'));
$this->assertFalse($bouncer->can('read'));
}

/**
* @test
* @dataProvider bouncerProvider
Expand Down Expand Up @@ -326,6 +346,9 @@ function assigning_and_retracting_roles_scopes_them_properly($provider)

$bouncer->scope()->to(2);
$this->assertFalse($bouncer->is($user)->an('admin'));

$bouncer->scope()->to(null);
$this->assertFalse($bouncer->is($user)->an('admin'));
}

/**
Expand Down

0 comments on commit 502221b

Please sign in to comment.