Skip to content

Commit 69b79cb

Browse files
committed
feat: extract out the authroization check into a new function
1 parent 8c09fd9 commit 69b79cb

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/Traits/Publishable.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace Javaabu\Helpers\Traits;
77

88
use App\Models\User;
9+
use Illuminate\Contracts\Auth\Access\Authorizable;
910

1011
trait Publishable
1112
{
@@ -127,6 +128,11 @@ public function getRejectedKey()
127128
return $status_class::REJECTED;
128129
}
129130

131+
public function canPublish(Authorizable $user): bool
132+
{
133+
return $user->can('publish', static::class);
134+
}
135+
130136
/**
131137
* Update the status
132138
*
@@ -136,12 +142,14 @@ public function getRejectedKey()
136142
*/
137143
public function updateStatus($status, $publish = false)
138144
{
145+
$user = auth()->user();
146+
139147
//first check if requesting for publishing
140148
if ($publish || $status == $this->getPublishedKey()) {
141149
$this->publish();
142150
} elseif ($status == $this->getRejectedKey()) {
143151
$this->reject();
144-
} elseif ($status && auth()->check() && auth()->user()->can('publish', static::class)) {
152+
} elseif ($status && $user && $this->canPublish($user)) {
145153
$this->status = $status;
146154
} else {
147155
$this->draft();

0 commit comments

Comments
 (0)