Skip to content

Commit

Permalink
Fix #372 since whereColumn automatically prepend table prefix (#508)
Browse files Browse the repository at this point in the history
  • Loading branch information
Omranic authored Mar 20, 2020
1 parent c624eda commit 9f54c9d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 65 deletions.
27 changes: 0 additions & 27 deletions src/BouncerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function register()
public function boot()
{
$this->registerMorphs();
$this->setTablePrefix();
$this->setUserModel();

$this->registerAtGate();
Expand Down Expand Up @@ -78,32 +77,6 @@ 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: 0 additions & 28 deletions src/Database/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@

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

/**
* Map of bouncer's models.
*
Expand Down Expand Up @@ -100,17 +93,6 @@ 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 @@ -126,16 +108,6 @@ 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
12 changes: 4 additions & 8 deletions src/Database/Queries/Abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ protected static function getRoleConstraint(Model $authority, $allowed)
$permissions = Models::table('permissions');
$abilities = Models::table('abilities');
$roles = Models::table('roles');
$prefix = Models::prefix();

$query->from($roles)
->join($permissions, $roles.'.id', '=', $permissions.'.entity_id')
->whereColumn("{$prefix}{$permissions}.ability_id", "{$prefix}{$abilities}.id")
->whereColumn("{$permissions}.ability_id", "{$abilities}.id")
->where($permissions.".forbidden", ! $allowed)
->where($permissions.".entity_type", Models::role()->getMorphClass());

Expand Down Expand Up @@ -101,11 +100,10 @@ protected static function getAuthorityRoleConstraint(Model $authority)
$pivot = Models::table('assigned_roles');
$roles = Models::table('roles');
$table = $authority->getTable();
$prefix = Models::prefix();

$query->from($table)
->join($pivot, "{$table}.{$authority->getKeyName()}", '=', $pivot.'.entity_id')
->whereColumn("{$prefix}{$pivot}.role_id", "{$prefix}{$roles}.id")
->whereColumn("{$pivot}.role_id", "{$roles}.id")
->where($pivot.'.entity_type', $authority->getMorphClass())
->where("{$table}.{$authority->getKeyName()}", $authority->getKey());

Expand All @@ -127,11 +125,10 @@ protected static function getAuthorityConstraint(Model $authority, $allowed)
$permissions = Models::table('permissions');
$abilities = Models::table('abilities');
$table = $authority->getTable();
$prefix = Models::prefix();

$query->from($table)
->join($permissions, "{$table}.{$authority->getKeyName()}", '=', $permissions.'.entity_id')
->whereColumn("{$prefix}{$permissions}.ability_id", "{$prefix}{$abilities}.id")
->whereColumn("{$permissions}.ability_id", "{$abilities}.id")
->where("{$permissions}.forbidden", ! $allowed)
->where("{$permissions}.entity_type", $authority->getMorphClass())
->where("{$table}.{$authority->getKeyName()}", $authority->getKey());
Expand All @@ -152,10 +149,9 @@ protected static function getEveryoneConstraint($allowed)
return function ($query) use ($allowed) {
$permissions = Models::table('permissions');
$abilities = Models::table('abilities');
$prefix = Models::prefix();

$query->from($permissions)
->whereColumn("{$prefix}{$permissions}.ability_id", "{$prefix}{$abilities}.id")
->whereColumn("{$permissions}.ability_id", "{$abilities}.id")
->where("{$permissions}.forbidden", ! $allowed)
->whereNull('entity_id');

Expand Down
3 changes: 1 addition & 2 deletions src/Database/Queries/Roles.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ public function constrainWhereAssignedTo($query, $model, array $keys = null)
$key = "{$table}.{$model->getKeyName()}";
$pivot = Models::table('assigned_roles');
$roles = Models::table('roles');
$prefix = Models::prefix();

$query->from($table)
->join($pivot, $key, '=', $pivot.'.entity_id')
->whereColumn("{$prefix}{$pivot}.role_id", "{$prefix}{$roles}.id")
->whereColumn("{$pivot}.role_id", "{$roles}.id")
->where("{$pivot}.entity_type", $model->getMorphClass())
->whereIn($key, $keys);

Expand Down

0 comments on commit 9f54c9d

Please sign in to comment.