-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace TomatoPHP\FilamentCms\Filament\Pages\Traits; | ||
|
||
use BezhanSalleh\FilamentShield\Support\Utils; | ||
use Filament\Facades\Filament; | ||
use Illuminate\Support\Str; | ||
|
||
trait HasShield | ||
{ | ||
public function booted(): void | ||
{ | ||
if(filament('filament-cms')->isShieldAllowed()){ | ||
$this->beforeBooted(); | ||
|
||
if (! static::canAccess()) { | ||
|
||
Notification::make() | ||
->title(__('filament-shield::filament-shield.forbidden')) | ||
->warning() | ||
->send(); | ||
|
||
$this->beforeShieldRedirects(); | ||
|
||
redirect($this->getShieldRedirectPath()); | ||
|
||
return; | ||
} | ||
|
||
if (method_exists(parent::class, 'booted')) { | ||
parent::booted(); | ||
} | ||
|
||
$this->afterBooted(); | ||
} | ||
} | ||
|
||
protected function beforeBooted(): void | ||
{ | ||
} | ||
|
||
protected function afterBooted(): void | ||
{ | ||
} | ||
|
||
protected function beforeShieldRedirects(): void | ||
{ | ||
} | ||
|
||
protected function getShieldRedirectPath(): string | ||
{ | ||
return Filament::getUrl(); | ||
} | ||
|
||
protected static function getPermissionName(): string | ||
{ | ||
return Str::of(class_basename(static::class)) | ||
->prepend( | ||
Str::of(Utils::getPagePermissionPrefix()) | ||
->append('_') | ||
->toString() | ||
) | ||
->toString(); | ||
} | ||
|
||
public static function canAccess(): bool | ||
{ | ||
if(filament('filament-cms')->isShieldAllowed()){ | ||
return Filament::auth()->user()->can(static::getPermissionName()); | ||
} | ||
else { | ||
return true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters