feat: add username management system

This commit is contained in:
idevakk
2025-11-15 21:41:28 -08:00
parent ca94c360ea
commit ea0bc91251
11 changed files with 647 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Filament\Resources\Usernames\Pages;
use App\Filament\Resources\Usernames\UsernameResource;
use Filament\Resources\Pages\CreateRecord;
class CreateUsername extends CreateRecord
{
protected static string $resource = UsernameResource::class;
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Filament\Resources\Usernames\Pages;
use App\Filament\Resources\Usernames\UsernameResource;
use Filament\Actions\DeleteAction;
use Filament\Actions\ForceDeleteAction;
use Filament\Actions\RestoreAction;
use Filament\Resources\Pages\EditRecord;
class EditUsername extends EditRecord
{
protected static string $resource = UsernameResource::class;
protected function getHeaderActions(): array
{
return [
DeleteAction::make(),
ForceDeleteAction::make(),
RestoreAction::make(),
];
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\Usernames\Pages;
use App\Filament\Resources\Usernames\UsernameResource;
use Filament\Actions\CreateAction;
use Filament\Resources\Pages\ListRecords;
class ListUsernames extends ListRecords
{
protected static string $resource = UsernameResource::class;
protected function getHeaderActions(): array
{
return [
CreateAction::make(),
];
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace App\Filament\Resources\Usernames\Schemas;
use App\enum\ProviderType;
use App\enum\UsernameType;
use Filament\Forms\Components\DateTimePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\ToggleButtons;
use Filament\Infolists\Components\TextEntry;
use Filament\Schemas\Schema;
class UsernameForm
{
public static function configure(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('username')
->columnSpan(1)
->alphaDash()
->helperText('Email: myusername@gmail.com | Username: myusername')
->required(),
TextInput::make('daily_mailbox_limit')
->integer()
->minValue(1)
->default(100)
->helperText('How many mailboxes can be created with this domain daily')
->columnSpan(1)
->required(),
ToggleButtons::make('is_active')
->options([
true => 'Active',
false => 'Disabled',
])
->inline()
->default(true)
->columnSpanFull()
->required(),
Select::make('username_type')
->options(UsernameType::class)
->enum(UsernameType::class)
->required(),
Select::make('provider_type')
->options(ProviderType::class)
->enum(ProviderType::class)
->required(),
DateTimePicker::make('starts_at'),
DateTimePicker::make('ends_at'),
TextEntry::make('last_used_at')
->label('Last Used At')
->formatStateUsing(fn ($state) => $state ? $state->diffForHumans() : 'Never')
->visible(fn ($context) => $context === 'edit'),
TextEntry::make('checked_at')
->label('Last Checked At')
->formatStateUsing(fn ($state) => $state ? $state->diffForHumans() : 'Never')
->visible(fn ($context) => $context === 'edit'),
TextEntry::make('deleted_at')
->label('Deleted At')
->formatStateUsing(fn ($state) => $state ? $state->diffForHumans() : null)
->color('danger')
->icon('heroicon-o-trash')
->visible(fn ($record) => $record?->deleted_at !== null),
]);
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace App\Filament\Resources\Usernames\Tables;
use App\enum\ProviderType;
use App\enum\UsernameType;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Actions\ForceDeleteBulkAction;
use Filament\Actions\RestoreBulkAction;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Columns\ToggleColumn;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Filters\TrashedFilter;
use Filament\Tables\Table;
class UsernamesTable
{
public static function configure(Table $table): Table
{
return $table
->columns([
TextColumn::make('username')
->label('Usernames')
->searchable()
->weight('medium')
->icon(Heroicon::OutlinedUser)
->copyable()
->copyMessage('Domain copied!')
->copyMessageDuration(1500),
ToggleColumn::make('is_active')
->label('Active')
->alignCenter(),
TextColumn::make('username_type')
->label('Type')
->formatStateUsing(fn ($state) => $state ? UsernameType::tryFrom($state)?->getLabel() : '-')
->badge()
->color(fn ($state) => $state ? UsernameType::tryFrom($state)?->getColor() : 'gray')
->alignCenter(),
TextColumn::make('provider_type')
->label('Provider')
->formatStateUsing(fn ($state) => $state ? ProviderType::tryFrom($state)?->getLabel() : '-')
->badge()
->color(fn ($state) => $state ? ProviderType::tryFrom($state)?->getColor() : 'gray')
->alignCenter(),
TextColumn::make('daily_mailbox_limit')
->label('Daily Limit')
->numeric()
->formatStateUsing(fn ($state) => number_format($state))
->alignCenter()
->icon('heroicon-o-inbox'),
TextColumn::make('last_used_at')
->label('Last Used')
->dateTime('M j, Y g:i A')
->placeholder('Never')
->sortable()
->since()
->alignCenter(),
TextColumn::make('checked_at')
->label('Checked')
->dateTime('M j, Y')
->placeholder('Never')
->sortable()
->since()
->alignCenter()
->toggleable(isToggledHiddenByDefault: true),
])
->filters([
SelectFilter::make('username_type')
->label('Username Type')
->options(UsernameType::class),
SelectFilter::make('provider_type')
->label('Provider Type')
->options(ProviderType::class),
SelectFilter::make('is_active')
->label('Status')
->options([
true => 'Active',
false => 'Inactive',
]),
TrashedFilter::make(),
])
->recordActions([
EditAction::make(),
])
->toolbarActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
ForceDeleteBulkAction::make(),
RestoreBulkAction::make(),
]),
])
->emptyStateHeading('No usernames found')
->emptyStateDescription('Get started by creating your first username.')
->emptyStateActions([
// Add create action if needed
])
->poll('60s')
->striped()
->defaultPaginationPageOption(10)
->paginated([10, 25, 50, 100])
->reorderable('sort_order')
->defaultSort('username');
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace App\Filament\Resources\Usernames;
use App\Filament\Resources\Usernames\Pages\CreateUsername;
use App\Filament\Resources\Usernames\Pages\EditUsername;
use App\Filament\Resources\Usernames\Pages\ListUsernames;
use App\Filament\Resources\Usernames\Schemas\UsernameForm;
use App\Filament\Resources\Usernames\Tables\UsernamesTable;
use App\Models\Username;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
class UsernameResource extends Resource
{
protected static ?string $model = Username::class;
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedUserGroup;
protected static string|null|\UnitEnum $navigationGroup = 'Email';
public static function form(Schema $schema): Schema
{
return UsernameForm::configure($schema);
}
public static function table(Table $table): Table
{
return UsernamesTable::configure($table);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => ListUsernames::route('/'),
'create' => CreateUsername::route('/create'),
'edit' => EditUsername::route('/{record}/edit'),
];
}
public static function getRecordRouteBindingEloquentQuery(): Builder
{
return parent::getRecordRouteBindingEloquentQuery()
->withoutGlobalScopes([
SoftDeletingScope::class,
]);
}
}

72
app/Models/Username.php Normal file
View File

@@ -0,0 +1,72 @@
<?php
namespace App\Models;
use App\enum\ProviderType;
use App\enum\UsernameType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Username extends Model
{
use HasFactory;
use SoftDeletes;
protected $fillable = [
'username',
'is_active',
'daily_mailbox_limit',
'username_type',
'provider_type',
'starts_at',
'ends_at',
'last_used_at',
'checked_at',
];
protected function casts(): array
{
return [
'is_active' => 'boolean',
'daily_mailbox_limit' => 'integer',
'starts_at' => 'datetime',
'ends_at' => 'datetime',
'last_used_at' => 'datetime',
'checked_at' => 'datetime',
];
}
/**
* Retrieve active username by type and provider.
*
* @param UsernameType|null $usernameType Filter by username type
* @param ProviderType|null $providerType Filter by provider type
* @return array Array of usernames
*/
public static function getActiveUsernameByType(
?UsernameType $usernameType = null,
?ProviderType $providerType = null,
): array {
$query = static::query()
->where('is_active', true)
->where(function ($query) {
$query->whereNull('starts_at')
->orWhere('starts_at', '<=', now());
})
->where(function ($query) {
$query->whereNull('ends_at')
->orWhere('ends_at', '>=', now());
});
if ($usernameType) {
$query->where('username_type', $usernameType);
}
if ($providerType) {
$query->where('provider_type', $providerType);
}
return $query->pluck('username')->toArray();
}
}

25
app/enum/UsernameType.php Normal file
View File

@@ -0,0 +1,25 @@
<?php
namespace App\enum;
enum UsernameType: string
{
case PUBLIC = 'public';
case PREMIUM = 'premium';
public function getColor(): string
{
return match ($this) {
self::PUBLIC => 'warning',
self::PREMIUM => 'success',
};
}
public function getLabel(): string
{
return match ($this) {
self::PUBLIC => 'Public',
self::PREMIUM => 'Premium',
};
}
}

View File

@@ -0,0 +1,135 @@
<?php
namespace Database\Factories;
use App\enum\ProviderType;
use App\enum\UsernameType;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Username>
*/
class UsernameFactory extends Factory
{
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'username' => $this->faker->unique()->userName(),
'is_active' => $this->faker->boolean(80), // 80% chance of being active
'daily_mailbox_limit' => $this->faker->numberBetween(50, 500),
'username_type' => $this->faker->randomElement(UsernameType::class),
'provider_type' => $this->faker->randomElement(ProviderType::class),
'starts_at' => $this->faker->optional(0.3)->dateTimeBetween('-1 year', 'now'), // 30% chance of having start date
'ends_at' => $this->faker->optional(0.2)->dateTimeBetween('now', '+2 years'), // 20% chance of having end date
'last_used_at' => $this->faker->optional(0.7)->dateTimeBetween('-1 month', 'now'), // 70% chance of being used
'checked_at' => $this->faker->optional(0.8)->dateTimeBetween('-1 week', 'now'), // 80% chance of being checked
];
}
/**
* Indicate that the username is active.
*/
public function active(): static
{
return $this->state(fn (array $attributes) => [
'is_active' => true,
]);
}
/**
* Indicate that the username is inactive.
*/
public function inactive(): static
{
return $this->state(fn (array $attributes) => [
'is_active' => false,
]);
}
/**
* Indicate that the username is public.
*/
public function public(): static
{
return $this->state(fn (array $attributes) => [
'username_type' => UsernameType::PUBLIC,
]);
}
/**
* Indicate that the username is premium.
*/
public function premium(): static
{
return $this->state(fn (array $attributes) => [
'username_type' => UsernameType::PREMIUM,
]);
}
/**
* Indicate that the username is for Gmail.
*/
public function gmail(): static
{
return $this->state(fn (array $attributes) => [
'provider_type' => ProviderType::GMAIL,
]);
}
/**
* Indicate that the username is for Yahoo.
*/
public function yahoo(): static
{
return $this->state(fn (array $attributes) => [
'provider_type' => ProviderType::YAHOO,
]);
}
/**
* Indicate that the username is for Outlook.
*/
public function outlook(): static
{
return $this->state(fn (array $attributes) => [
'provider_type' => ProviderType::OUTLOOK,
]);
}
/**
* Indicate that the username is for custom provider.
*/
public function custom(): static
{
return $this->state(fn (array $attributes) => [
'provider_type' => ProviderType::CUSTOM,
]);
}
/**
* Indicate that the username has expiration dates.
*/
public function withExpiration(): static
{
return $this->state(fn (array $attributes) => [
'starts_at' => $this->faker->dateTimeBetween('-1 month', 'now'),
'ends_at' => $this->faker->dateTimeBetween('now', '+1 year'),
]);
}
/**
* Indicate that the username has been recently used.
*/
public function recentlyUsed(): static
{
return $this->state(fn (array $attributes) => [
'last_used_at' => $this->faker->dateTimeBetween('-1 day', 'now'),
'checked_at' => $this->faker->dateTimeBetween('-1 day', 'now'),
]);
}
}

View File

@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('usernames', function (Blueprint $table) {
$table->id();
$table->string('username')->unique();
$table->boolean('is_active')->default(true);
$table->integer('daily_mailbox_limit')->default(100);
$table->string('username_type')->nullable();
$table->string('provider_type')->nullable();
$table->timestamp('starts_at')->nullable();
$table->timestamp('ends_at')->nullable();
$table->timestamp('last_used_at')->nullable();
$table->timestamp('checked_at')->nullable();
$table->softDeletes();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('usernames');
}
};

View File

@@ -0,0 +1,90 @@
<?php
namespace Database\Seeders;
use App\Models\Username;
use Illuminate\Database\Seeder;
class UsernameSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
// Create a variety of usernames for testing
Username::factory()
->count(15)
->active()
->public()
->sequence(fn ($sequence) => [
'username' => 'public_user_'.($sequence->index + 1),
])
->create();
Username::factory()
->count(10)
->active()
->premium()
->sequence(fn ($sequence) => [
'username' => 'premium_user_'.($sequence->index + 1),
])
->create();
Username::factory()
->count(8)
->active()
->gmail()
->sequence(fn ($sequence) => [
'username' => 'gmail_user_'.($sequence->index + 1),
])
->create();
Username::factory()
->count(6)
->active()
->yahoo()
->sequence(fn ($sequence) => [
'username' => 'yahoo_user_'.($sequence->index + 1),
])
->create();
Username::factory()
->count(6)
->active()
->outlook()
->sequence(fn ($sequence) => [
'username' => 'outlook_user_'.($sequence->index + 1),
])
->create();
Username::factory()
->count(4)
->active()
->custom()
->withExpiration()
->sequence(fn ($sequence) => [
'username' => 'custom_user_'.($sequence->index + 1),
])
->create();
// Create some inactive usernames
Username::factory()
->count(5)
->inactive()
->sequence(fn ($sequence) => [
'username' => 'inactive_user_'.($sequence->index + 1),
])
->create();
// Create some recently used usernames
Username::factory()
->count(8)
->active()
->recentlyUsed()
->sequence(fn ($sequence) => [
'username' => 'recent_user_'.($sequence->index + 1),
])
->create();
}
}