Skip to content

Commit

Permalink
fix: extra constraint that does not apply to subselect query
Browse files Browse the repository at this point in the history
  • Loading branch information
tszulc committed Mar 11, 2022
1 parent da6842f commit 11995a4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/BouncerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function register()
public function boot()
{
$this->registerMorphs();
$this->setTablePrefix();
$this->setUserModel();

$this->registerAtGate();
Expand Down Expand Up @@ -78,6 +79,32 @@ protected function registerMorphs()
Models::updateMorphMap();
}

/**
* Set the table prefix for Bouncer's tables.
*
* @return void
*/
protected function setTablePrefix()
{
if ($prefix = $this->getTablePrefix()) {
Models::setPrefix($prefix);
}
}

/**
* Get the configured table prefix.
*
* @return string|null
*/
protected function getTablePrefix()
{
$config = $this->app->config['database'];

$connection = Arr::get($config, 'default');

return Arr::get($config, "connections.{$connection}.prefix");
}

/**
* Publish the package's middleware.
*
Expand Down
28 changes: 28 additions & 0 deletions src/Database/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@

class Models
{
/**
* The prefix for all tables.
*
* @var string
*/
protected static $prefix = '';

/**
* Map of bouncer's models.
*
Expand Down Expand Up @@ -93,6 +100,17 @@ public static function setTables(array $map)
static::updateMorphMap();
}

/**
* Set the prefix for the tables.
*
* @param string $prefix
* @return void
*/
public static function setPrefix($prefix)
{
static::$prefix = $prefix;
}

/**
* Get a custom table name mapping for the given table.
*
Expand All @@ -108,6 +126,16 @@ public static function table($table)
return $table;
}

/**
* Get the prefix for the tables.
*
* @return string
*/
public static function prefix()
{
return static::$prefix;
}

/**
* Get or set the model scoping instance.
*
Expand Down
3 changes: 3 additions & 0 deletions src/Database/Queries/Abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ protected static function getRoleConstraint(Model $authority, $allowed)
return function ($query) use ($authority, $allowed) {
$permissions = Models::table('permissions');
$roles = Models::table('roles');
$prefix = Models::prefix();

$query->from($roles)
->select("{$prefix}{$permissions}.ability_id")
Expand Down Expand Up @@ -77,6 +78,7 @@ protected static function getAuthorityRoleConstraint(Model $authority)
$pivot = Models::table('assigned_roles');
$roles = Models::table('roles');
$table = $authority->getTable();
$prefix = Models::prefix();

$query->from($table)
->select("{$prefix}{$pivot}.role_id")
Expand All @@ -102,6 +104,7 @@ protected static function getAuthorityConstraint(Model $authority, $allowed)
$permissions = Models::table('permissions');
$abilities = Models::table('abilities');
$table = $authority->getTable();
$prefix = Models::prefix();

$query->from($table)
->select("{$prefix}{$permissions}.ability_id")
Expand Down

0 comments on commit 11995a4

Please sign in to comment.