Skip to content
This repository has been archived by the owner on Sep 22, 2024. It is now read-only.

Commit

Permalink
Updated Manageable to allow for model-specific managing models and cu…
Browse files Browse the repository at this point in the history
…stom guards. NEEDS TESTS.
  • Loading branch information
telkins committed Oct 13, 2021
1 parent 1651719 commit c9a4733
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/Manageable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public static function bootManageable(): void

public function creator(): BelongsTo
{
return $this->belongsTo(config('auth.providers.users.model'), 'created_by');
return $this->belongsTo($this->getManageableUsersModel(), 'created_by');
}

public function editor(): BelongsTo
{
return $this->belongsTo(config('auth.providers.users.model'), 'updated_by');
return $this->belongsTo($this->getManageableUsersModel(), 'updated_by');
}

public function hasCreator(): bool
Expand All @@ -41,9 +41,27 @@ public function hasEditor(): bool

protected function setManageable(string $type, string $relation): void
{
if (Auth::check()) {
$this->{$type} = Auth::id();
$this->setRelation($relation, Auth::user());
$guard = $this->getManageableGuard();

if ($guard->check()) {
$this->{$type} = $guard->id();
$this->setRelation($relation, $guard->user());
}
}

protected function getManageableUsersModel()
{
return method_exists($this, 'manageableUsersModel')
? $this->manageableUsersModel()
: ($this->manageableUsersModel ?? config('auth.providers.users.model'));
}

protected function getManageableGuard()
{
$manageableGuardName = method_exists($this, 'manageableGuardName')
? $this->manageableGuardName()
: ($this->manageableGuardName ?? config('auth.defaults.guard'));

return Auth::guard($manageableGuardName);
}
}

0 comments on commit c9a4733

Please sign in to comment.