Make sure UTC is always used internally (#713)

* force app timezone to be UTC

* remove asDateTime overwrite

* add custom column to display dates in user timezone

* use `APP_TIMEZONE` as default timezone for new users

* revert accidental pinting
This commit is contained in:
Boy132 2024-11-15 20:41:33 +01:00 committed by GitHub
parent 408897cfcf
commit 54039e25a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 27 additions and 44 deletions

View File

@ -4,6 +4,7 @@ namespace App\Filament\Resources\ApiKeyResource\Pages;
use App\Filament\Resources\ApiKeyResource;
use App\Models\ApiKey;
use App\Tables\Columns\DateTimeColumn;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Filament\Tables\Actions\CreateAction;
@ -35,15 +36,13 @@ class ListApiKeys extends ListRecords
->hidden()
->searchable(),
TextColumn::make('last_used_at')
DateTimeColumn::make('last_used_at')
->label('Last Used')
->placeholder('Not Used')
->dateTime()
->sortable(),
TextColumn::make('created_at')
DateTimeColumn::make('created_at')
->label('Created')
->dateTime()
->sortable(),
TextColumn::make('user.username')

View File

@ -4,6 +4,7 @@ namespace App\Filament\Resources\DatabaseHostResource\RelationManagers;
use App\Models\Database;
use App\Services\Databases\DatabasePasswordService;
use App\Tables\Columns\DateTimeColumn;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
@ -54,7 +55,7 @@ class DatabasesRelationManager extends RelationManager
->icon('tabler-brand-docker')
->url(fn (Database $database) => route('filament.admin.resources.servers.edit', ['record' => $database->server_id])),
TextColumn::make('max_connections'),
TextColumn::make('created_at')->dateTime(),
DateTimeColumn::make('created_at'),
])
->actions([
DeleteAction::make(),

View File

@ -3,6 +3,7 @@
namespace App\Filament\Resources\DatabaseResource\Pages;
use App\Filament\Resources\DatabaseResource;
use App\Tables\Columns\DateTimeColumn;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
use Filament\Tables\Actions\BulkActionGroup;
@ -34,12 +35,10 @@ class ListDatabases extends ListRecords
TextColumn::make('max_connections')
->numeric()
->sortable(),
TextColumn::make('created_at')
->dateTime()
DateTimeColumn::make('created_at')
->sortable()
->toggleable(isToggledHiddenByDefault: true),
TextColumn::make('updated_at')
->dateTime()
DateTimeColumn::make('updated_at')
->sortable()
->toggleable(isToggledHiddenByDefault: true),
])

View File

@ -2,11 +2,7 @@
namespace App\Models;
use Carbon\CarbonInterface;
use DateTimeInterface;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Container\Container;
@ -16,7 +12,6 @@ use App\Exceptions\Model\DataValidationException;
use Illuminate\Database\Eloquent\Model as IlluminateModel;
use Illuminate\Validation\Factory as ValidationFactory;
use Illuminate\Validation\Validator;
use InvalidArgumentException;
abstract class Model extends IlluminateModel
{
@ -69,36 +64,6 @@ abstract class Model extends IlluminateModel
return 'uuid';
}
protected function asDateTime($value): Carbon
{
$timezone = auth()->user()?->timezone ?? config('app.timezone', 'UTC');
if ($value instanceof CarbonInterface) {
return Date::instance($value->timezone($timezone));
}
if ($value instanceof DateTimeInterface) {
return Date::parse($value->format('Y-m-d H:i:s.u'), $timezone);
}
if (is_numeric($value)) {
return Date::createFromTimestamp($value, $timezone);
}
if ($this->isStandardDateFormat($value)) {
return Date::instance(Carbon::createFromFormat('Y-m-d', $value)->timezone($timezone)->startOfDay());
}
$format = $this->getDateFormat();
try {
$date = Date::createFromFormat($format, $value)->timezone($timezone);
} catch (InvalidArgumentException) {
$date = false;
}
return $date ?: Date::parse($value);
}
/**
* Returns the validator instance used by this model.
*/

View File

@ -188,6 +188,8 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
static::creating(function (self $user) {
$user->uuid = Str::uuid()->toString();
$user->timezone = env('APP_TIMEZONE', 'UTC');
return true;
});

View File

@ -0,0 +1,15 @@
<?php
namespace App\Tables\Columns;
use Filament\Tables\Columns\TextColumn;
class DateTimeColumn extends TextColumn
{
protected function setUp(): void
{
parent::setUp();
$this->dateTime(timezone: auth()->user()?->timezone ?? config('app.timezone', 'UTC'));
}
}

View File

@ -7,6 +7,8 @@ return [
'version' => 'canary',
'timezone' => 'UTC',
'exceptions' => [
'report_all' => env('APP_REPORT_ALL_EXCEPTIONS', false),
],