Skip to content

Commit

Permalink
2.x user menu post action (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarJose123 authored Nov 9, 2024
2 parents 62f51ee + 4b44c30 commit bb87a9a
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 122 deletions.
1 change: 0 additions & 1 deletion config/filament-lockscreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

return [


/*
* Lock Screen Icon
*/
Expand Down
34 changes: 17 additions & 17 deletions resources/lang/ckb/default.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

return [
'heading' => 'قوفڵی شاشه‌',
'user_menu_title' => 'قوفڵی شاشه‌',
'fields' => [
'password' => 'وشەی نهێنی',
],
'button' => [
'switch_account' => 'گۆرینی هەژمار',
'submit_label' => 'چوونە ژوورەوە',
],
'notification' => [
'title' => 'شکستی چوونەژوورەوە',
'message' => 'دوای شکستی چوونەژوورەوەی سەرکەوتوو ئاڕاستەکرایتەوە بۆ لاپەڕەی چوونەژوورەوە.',
],
];
<?php

return [
'heading' => 'قوفڵی شاشه‌',
'user_menu_title' => 'قوفڵی شاشه‌',
'fields' => [
'password' => 'وشەی نهێنی',
],
'button' => [
'switch_account' => 'گۆرینی هەژمار',
'submit_label' => 'چوونە ژوورەوە',
],
'notification' => [
'title' => 'شکستی چوونەژوورەوە',
'message' => 'دوای شکستی چوونەژوورەوەی سەرکەوتوو ئاڕاستەکرایتەوە بۆ لاپەڕەی چوونەژوورەوە.',
],
];
34 changes: 17 additions & 17 deletions resources/lang/ku/default.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?php

return [
'heading' => 'قوفڵی شاشه‌',
'user_menu_title' => 'قوفڵی شاشه‌',
'fields' => [
'password' => 'وشەی نهێنی',
],
'button' => [
'switch_account' => 'گۆرینی هەژمار',
'submit_label' => 'چوونە ژوورەوە',
],
'notification' => [
'title' => 'شکستی چوونەژوورەوە',
'message' => 'دوای شکستی چوونەژوورەوەی سەرکەوتوو ئاڕاستەکرایتەوە بۆ لاپەڕەی چوونەژوورەوە.',
],
];
<?php

return [
'heading' => 'قوفڵی شاشه‌',
'user_menu_title' => 'قوفڵی شاشه‌',
'fields' => [
'password' => 'وشەی نهێنی',
],
'button' => [
'switch_account' => 'گۆرینی هەژمار',
'submit_label' => 'چوونە ژوورەوە',
],
'notification' => [
'title' => 'شکستی چوونەژوورەوە',
'message' => 'دوای شکستی چوونەژوورەوەی سەرکەوتوو ئاڕاستەکرایتەوە بۆ لاپەڕەی چوونەژوورەوە.',
],
];
7 changes: 4 additions & 3 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
use Filament\Facades\Filament;
use Illuminate\Support\Facades\Route;
use lockscreen\FilamentLockscreen\Http\Livewire\LockerScreen;

use lockscreen\FilamentLockscreen\Http\LockscreenSessionController;

Route::name('lockscreen.')
->group(function (){
->group(function () {
foreach (Filament::getPanels() as $panel) {
$panelId = $panel->getId();
$domains = $panel->getDomains();
Expand All @@ -17,6 +17,8 @@
->name("{$panelId}.")
->prefix($panel->getPath())
->group(function () {
Route::post('lock-session', [LockscreenSessionController::class, 'lockSession'])
->name('lock-session');
Route::get(
(config()->has('filament-lockscreen.url') && config('filament-lockscreen.url') !== '' && config('filament-lockscreen.url') !== '/')
? config('filament-lockscreen.url')
Expand All @@ -27,6 +29,5 @@

}


}
});
8 changes: 2 additions & 6 deletions src/FilamentLockscreenServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

namespace lockscreen\FilamentLockscreen;

use Filament\Facades\Filament;
use Filament\Navigation\UserMenuItem;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Routing\Router;
use Livewire\Livewire;
use lockscreen\FilamentLockscreen\Http\Livewire\LockerScreen;
use lockscreen\FilamentLockscreen\Http\Middleware\Locker;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class FilamentLockscreenServiceProvider extends PackageServiceProvider
{
Expand Down
41 changes: 26 additions & 15 deletions src/Http/Livewire/LockerScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

use DanHarrin\LivewireRateLimiting\Exceptions\TooManyRequestsException;
use DanHarrin\LivewireRateLimiting\WithRateLimiting;
use Filament\Facades\Filament;
use Filament\Actions\Action;
use Filament\Exceptions\NoDefaultPanelSetException;
use Filament\Facades\Filament;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Pages\Actions\ActionGroup;
use Filament\Pages\BasePage;
use Filament\Pages\Concerns\InteractsWithFormActions;
use Filament\Forms\Contracts\HasForms;
use Filament\Notifications\Notification;
use Filament\Pages\SimplePage;
use Illuminate\Contracts\View\View;
use Livewire\Component;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class LockerScreen extends BasePage
{
Expand All @@ -28,29 +27,38 @@ class LockerScreen extends BasePage

public ?string $password = '';

protected static string $view ='filament-lockscreen::page.auth.login' ;
protected static string $view = 'filament-lockscreen::page.auth.login';

private ?string $account_username_field;

private ?string $account_password_field;

/**
* @throws ContainerExceptionInterface
* @throws NoDefaultPanelSetException
* @throws NotFoundExceptionInterface
*/
public function mount()
{
// Check if the request is still authenticated or not before rendering the page,
// if not authenticated then redirect to the login page of current panel, or default panel if current panel could not be detected.

if (!Filament::auth()->check())
{

if (! Filament::auth()->check()) {
if (filament()->getCurrentPanel()) {
return redirect(filament()->getCurrentPanel()->getLoginUrl());
}

return redirect(filament()->getDefaultPanel()->getLoginUrl());
}

session(['lockscreen' => true]);
// redirect to the filament default home url if session is not locked
if (!session()->has('lockscreen')) {
return redirect(session()->has('next') ? session('next') : filament()->getDefaultPanel()->getPath());
}

if (! config('filament-lockscreen.enable_redirect_to')) {
if (! session()->has('next') || session()->get('next') === null) {
if (! session()->has('next') || session('next') === null) {
session(['next' => url()->previous()]);
}
}
Expand Down Expand Up @@ -96,8 +104,10 @@ public function authenticate(): \Illuminate\Routing\Redirector|\Illuminate\Http\
if (config('filament-lockscreen.rate_limit.force_logout', false)) {
$this->forceLogout();
$panelId = filament()->getCurrentPanel()->getId();

return redirect()->route("filament.{$panelId}.auth.login");
}

return null;
}
}
Expand All @@ -107,6 +117,7 @@ public function authenticate(): \Illuminate\Routing\Redirector|\Illuminate\Http\
$this->account_password_field => $data['password'],
])) {
$this->addError('password', __('filament-panels::pages/auth/login.messages.failed'));

return null;
}

Expand All @@ -118,7 +129,7 @@ public function authenticate(): \Illuminate\Routing\Redirector|\Illuminate\Http\
return redirect()->route(config('filament-lockscreen.redirect_route'));
}
// store to variable
$url = session()->get('next');
$url = session('next');
// remove the value
session()->forget('next');

Expand All @@ -127,7 +138,7 @@ public function authenticate(): \Illuminate\Routing\Redirector|\Illuminate\Http\

protected function getFormSchema(): array
{
return[
return [
TextInput::make('password')
->label(__('filament-lockscreen::default.fields.password'))
->password()
Expand All @@ -138,7 +149,7 @@ protected function getFormSchema(): array

public function getTitle(): \Illuminate\Contracts\Support\Htmlable|string
{
return static::$title ?? (string) str(__('filament-lockscreen::default.heading'))
return (string) str(__('filament-lockscreen::default.heading'))
->kebab()
->replace('-', ' ')
->title();
Expand All @@ -165,9 +176,9 @@ protected function getFormActions(): array
$this->getAuthenticateFormAction(),
];
}

protected function hasFullWidthFormActions(): bool
{
return true;
}

}
5 changes: 1 addition & 4 deletions src/Http/Livewire/LockerScreenLoginPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@

namespace lockscreen\FilamentLockscreen\Http\Livewire;

class LockerScreenLoginPage extends LockerScreen
{

}
class LockerScreenLoginPage extends LockerScreen {}
32 changes: 32 additions & 0 deletions src/Http/LockscreenSessionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace lockscreen\FilamentLockscreen\Http;

use Filament\Facades\Filament;
use Illuminate\Http\RedirectResponse;

class LockscreenSessionController
{
/**
* @throws \Exception
*/
public function lockSession(): RedirectResponse
{
$currentPanel = filament()->getCurrentPanel();
/**
* Check if the request is still authenticated or not before rendering the page,
* if not authenticated then redirect to the login page of current panel, or default panel if current panel could not be detected.
*/
if (! Filament::auth()->check()) {
if (filament()->getCurrentPanel()) {
return redirect(filament()->getCurrentPanel()->getLoginUrl());
}

return redirect(filament()->getDefaultPanel()->getLoginUrl());
}

session(['lockscreen' => true]);

return redirect()->route("lockscreen.{$currentPanel->getId()}.page");
}
}
6 changes: 4 additions & 2 deletions src/Http/Middleware/Locker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace lockscreen\FilamentLockscreen\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

Expand All @@ -12,13 +11,16 @@ class Locker
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \Exception
*/
public function handle($request, Closure $next)
{
if ($request->method() === 'GET' && $request->session()->get('lockscreen') ) {
if ($request->method() === 'GET' && $request->session()->get('lockscreen')) {
$panelId = filament()->getCurrentPanel()?->getId();

return redirect()->route("lockscreen.{$panelId}.page");
}

return $next($request);
}
}
4 changes: 2 additions & 2 deletions src/Http/Middleware/LockerTimer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class LockerTimer
*/
public function handle(Request $request, Closure $next)
{
if (!$request->session()->has('lockscreen')) {
if (! $request->session()->has('lockscreen')) {
$lastActivity = $request->session()->get('locker_last_activity');
$activityTimeout = config('filament-lockscreen.activity_timeout', 60 * 30 /* 30 minutes */);
$activityTimeout = config('filament-lockscreen.activity_timeout', 60 * 30 /* 30 minutes */);

if ($request->method() === 'GET' && $lastActivity && (time() - $lastActivity) > $activityTimeout) {
$request->session()->put('lockscreen', true);
Expand Down
12 changes: 3 additions & 9 deletions src/Lockscreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

use Filament\Contracts\Plugin;
use Filament\Navigation\MenuItem;
use Filament\Navigation\UserMenuItem;
use Filament\Panel;
use Filament\PanelProvider;
use Livewire\Livewire;
use lockscreen\FilamentLockscreen\Http\Livewire\LockerScreen;

Expand All @@ -16,7 +14,7 @@ class Lockscreen implements Plugin

public function getId(): string
{
return 'filament-lockscreen';
return 'filament-lockscreen';
}

public static function make(): static
Expand All @@ -29,22 +27,18 @@ public static function get(): static
return filament(app(static::class)->getId());
}

public function register(Panel $panel): void
{

}
public function register(Panel $panel): void {}

public function boot(Panel $panel): void
{

$panel->userMenuItems([
'lockscreen' => MenuItem::make()
->label(fn () => __('filament-lockscreen::default.user_menu_title'))
->url(route("lockscreen.{$panel->getId()}.page"))
->postAction(route("lockscreen.{$panel->getId()}.lock-session"))
->icon(config('filament-lockscreen.icon')),
]);

Livewire::component('LockerScreen', LockerScreen::class);
}

}
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

5 changes: 0 additions & 5 deletions tests/Pest.php

This file was deleted.

Loading

0 comments on commit bb87a9a

Please sign in to comment.