Skip to content

Commit

Permalink
Always update the morph map when setting custom models or tables
Browse files Browse the repository at this point in the history
Closes #378
Refs #306
  • Loading branch information
JosephSilber committed Jan 21, 2019
1 parent 8cba56c commit 6370bce
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
9 changes: 2 additions & 7 deletions src/BouncerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Silber\Bouncer;

use Silber\Bouncer\Database\Role;
use Silber\Bouncer\Database\Models;
use Silber\Bouncer\Database\Ability;
use Silber\Bouncer\Console\CleanCommand;

use Illuminate\Cache\ArrayStore;
Expand Down Expand Up @@ -74,18 +72,15 @@ protected function registerCommands()
*/
protected function registerMorphs()
{
Relation::morphMap([
Models::classname(Role::class),
Models::classname(Ability::class),
]);
Models::updateMorphMap();
}

/**
* Set the table prefix for Bouncer's tables.
*
* @return void
*/
protected function setTablePrefix()
*/
{
if ($prefix = $this->getTablePrefix()) {
Models::setPrefix($prefix);
Expand Down
26 changes: 26 additions & 0 deletions src/Database/Models.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use App\User;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;

use Silber\Bouncer\Database\Scope\Scope;
use Silber\Bouncer\Contracts\Scope as ScopeContract;

Expand Down Expand Up @@ -55,6 +57,8 @@ class Models
public static function setAbilitiesModel($model)
{
static::$models[Ability::class] = $model;

static::updateMorphMap([$model]);
}

/**
Expand All @@ -66,6 +70,8 @@ public static function setAbilitiesModel($model)
public static function setRolesModel($model)
{
static::$models[Role::class] = $model;

static::updateMorphMap([$model]);
}

/**
Expand All @@ -90,6 +96,8 @@ public static function setUsersModel($model)
public static function setTables(array $map)
{
static::$tables = array_merge(static::$tables, $map);

static::updateMorphMap();
}

/**
Expand Down Expand Up @@ -162,6 +170,24 @@ public static function classname($model)
return $model;
}

/**
* Update Eloquent's morph map with the Bouncer models and tables.
*
* @param array|null $classNames
* @return void
*/
public static function updateMorphMap($classNames = null)
{
if (is_null($classNames)) {
$classNames = [
static::classname(Role::class),
static::classname(Ability::class),
];
}

Relation::morphMap($classNames);
}

/**
* Register an attribute/callback to determine if a model is owned by a given authority.
*
Expand Down

0 comments on commit 6370bce

Please sign in to comment.