Skip to content

Commit

Permalink
Limit demographic data changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschucki committed Mar 16, 2024
1 parent 0d57dea commit 37cb6d6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 14 deletions.
54 changes: 45 additions & 9 deletions app/Filament/Pages/UserSettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Filament\Forms\Components\Actions\Action as FormAction;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Placeholder;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Tabs;
Expand All @@ -32,6 +33,7 @@
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Filament\Support\Exceptions\Halt;
use Illuminate\Support\Carbon;

class UserSettingsPage extends Page implements HasForms
{
Expand Down Expand Up @@ -108,14 +110,38 @@ public function form(Form $form): Form
return $form->schema([
Grid::make(2)->schema([
Section::make('Demografische Daten')->schema([
Select::make('gender')->label('Geschlecht')->options(Gender::class),
DatePicker::make('birthday')

Select::make('gender')->disabled(fn () => ! \Auth::user()->canUpdateDemographicData())->label('Geschlecht')->options(Gender::class),
DatePicker::make('birthday')->disabled(fn () => ! \Auth::user()->canUpdateDemographicData())
->label('Geburtstag')
->nullable()
->before('today')
->displayFormat('d.m.Y'),
Select::make('nationality')->label('Nationalität')->options(Nationality::class),
Select::make('region')->label('Region')->options(Region::class),
Select::make('nationality')->disabled(fn () => ! \Auth::user()->canUpdateDemographicData())->searchable()->label('Nationalität')->options(Nationality::class),
Select::make('region')->disabled(fn () => ! \Auth::user()->canUpdateDemographicData())->searchable()->label('Region')->options(Region::class),
Placeholder::make('info')->label('')->content('Demografische Daten können nur alle 2 Monate geändert werden um zu verhindern, dass man sich für Umfragen als andere Zielgruppe ausgibt.')->extraAttributes(['class' => 'text-justify']),
Placeholder::make('next_change')->label('')->content(function () {
$sText = 'Deine nächste Änderung ist möglich in: ';
$dLastChange = Carbon::make(\Auth::user()->last_data_change);

if ($dLastChange === null) {
$sText .= 'Sofort';

return $sText;
}

$dNextChange = $dLastChange->addMonths(2);

if ($dNextChange->isPast()) {
$sText .= 'Sofort';

return $sText;
}

$sText .= $dNextChange->diffInDays().' Tagen'." ({$dNextChange->format('d.m.Y H:i')} Uhr)";

return $sText;
})->extraAttributes(['class' => 'text-justify']),
])->columnSpan(1),
Section::make('Benutzerdaten')->schema([
TextInput::make('name')->label('Benutzername')->disabled(),
Expand Down Expand Up @@ -163,11 +189,21 @@ public function save(): void
}

$data = $this->form->getState();
$demoGraphicDataKeys = array_keys($user->getDemographicData());
$aOriginalDemographicData = $user->getDemographicData();
ksort($aOriginalDemographicData);
$demoGraphicDataKeys = array_keys($aOriginalDemographicData);
$demographicData = array_filter($data, static fn ($key) => in_array($key, $demoGraphicDataKeys, true), ARRAY_FILTER_USE_KEY);

// Update demographic data
$user->update($demographicData);
ksort($demographicData);

if ($user->last_data_change === null || Carbon::make($user->last_data_change)->addMonths(2)->isPast()) {
// Check if dirty ignore array order and compare
if ($aOriginalDemographicData !== $demographicData) {
$user->update($demographicData);
$user->update([
'last_data_change' => Carbon::now(),
]);
}
}
if ($user->email !== $data['email']) {
$user->update([
'email_verified_at' => null,
Expand Down Expand Up @@ -195,7 +231,7 @@ public function save(): void
/**
* @throws Exception
*/
protected function resendEmailVerificationEmail()
protected function resendEmailVerificationEmail(): void
{
try {
$this->rateLimit(2);
Expand Down
2 changes: 2 additions & 0 deletions app/Filament/Resources/MyPollResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;
use Yepsua\Filament\Tables\Components\RatingColumn;

class MyPollResource extends Resource
Expand Down Expand Up @@ -64,6 +65,7 @@ public static function form(Form $form): Form
try {
return TargetGroupService::calculateTargetGroupFromBuilder($get('target_group')).' Teilnehmer';
} catch (\Throwable $throwable) {
Log::info($throwable->getMessage());
Notification::make('target_group_error')->title('Fehler')->body('Beim berechnen der Zielgruppe ist ein Fehler aufgetreten.')->danger()->actions([
Action::make('target_group_error_action')->label('Melden')->button()->url('https://github.com/pr0p0ll/pr0p0ll/issues/new', true),
])->send();
Expand Down
2 changes: 1 addition & 1 deletion app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class UserResource extends Resource
protected static ?string $slug = 'benutzer';

protected static ?string $label = 'Benutzer';
protected static ?string $pluralLabel = 'Benutzer';

protected static ?string $pluralLabel = 'Benutzer';

protected static ?string $navigationIcon = 'heroicon-o-users';

Expand Down
5 changes: 5 additions & 0 deletions app/Filament/Widgets/NeedsDataReviewWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ class NeedsDataReviewWidget extends Widget
protected static string $view = 'filament.widgets.needs-data-review-widget';

protected int|string|array $columnSpan = 2;

public static function canView(): bool
{
return \Auth::user()?->canUpdateDemographicData();
}
}
14 changes: 14 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable implements BannableInterface, FilamentUser, HasAvatar, MustVerifyEmail
Expand All @@ -43,6 +44,7 @@ class User extends Authenticatable implements BannableInterface, FilamentUser, H
'region',
'banned_at',
'pr0gramm_identifier',
'last_data_change',
];

/**
Expand Down Expand Up @@ -70,6 +72,7 @@ class User extends Authenticatable implements BannableInterface, FilamentUser, H
'password' => 'hashed',
'birthday' => 'date',
'banned_at' => 'date',
'last_data_change' => 'datetime',
];

public function notificationPreference(): HasOne
Expand Down Expand Up @@ -185,4 +188,15 @@ public function participations(): BelongsToMany
'rating',
]);
}

public function canUpdateDemographicData(): bool
{
$dLastChange = Carbon::make($this->last_data_change);

if ($dLastChange === null) {
return true;
}

return $dLastChange->addMonths(2)->isPast();
}
}
4 changes: 0 additions & 4 deletions app/Providers/Filament/Pr0p0llPanelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace App\Providers\Filament;

use App\Filament\Pages\Login;
use App\Filament\Widgets\NeedsDataReviewWidget;
use Cog\Laravel\Ban\Http\Middleware\ForbidBannedUser;
use Filament\Enums\ThemeMode;
use Filament\Facades\Filament;
Expand Down Expand Up @@ -54,9 +53,6 @@ public function panel(Panel $panel): Panel
])
->login(Login::class)
->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
->widgets([
NeedsDataReviewWidget::class,
])
->plugins([FilamentApexChartsPlugin::make()])
->userMenuItems([
MenuItem::make()->label('Startseite')->url('/')->icon('heroicon-o-home'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('users', static function (Blueprint $table) {
$table->dateTime('last_data_change')->nullable()->after('region');
});
}
};

0 comments on commit 37cb6d6

Please sign in to comment.