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

Commit

Permalink
Merge pull request #16 from telkins/custom-user
Browse files Browse the repository at this point in the history
Allow for model-specific managing models and custom guards
  • Loading branch information
pactode authored Nov 4, 2021
2 parents 1651719 + c9a4733 commit e11bd52
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 e11bd52

Please sign in to comment.