Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Management #153

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/cachet.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
| This is the model that will be used to authenticate users. This model
| must be an instance of Illuminate\Foundation\Auth\User.
*/
'user_model' => \App\Models\User::class,
'user_model' => env('CACHET_USER_MODEL', \App\Models\User::class),

'user_migrations' => env('CACHET_USER_MIGRATIONS', true),

/*
|--------------------------------------------------------------------------
Expand Down
44 changes: 44 additions & 0 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Cachet\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\Cachet\Models\User>
*/
class UserFactory extends Factory
{
/**
* The current password being used by the factory.
*/
protected static ?string $password;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'name' => fake()->name(),
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
];
}

/**
* Indicate that the model's email address should be unverified.
*/
public function unverified(): static
{
return $this->state(fn (array $attributes) => [
'email_verified_at' => null,
]);
}
}
11 changes: 11 additions & 0 deletions src/Concerns/CachetUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Cachet\Concerns;

interface CachetUser
{
/**
* Determine if the user is an admin.
*/
public function isAdmin(): bool;
}
8 changes: 0 additions & 8 deletions src/Concerns/User.php

This file was deleted.

110 changes: 110 additions & 0 deletions src/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Cachet\Filament\Resources;

use Cachet\Filament\Resources\UserResource\Pages;
use Cachet\Filament\Resources\UserResource\RelationManagers;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class UserResource extends Resource
{
protected static ?string $navigationIcon = 'heroicon-o-users';

public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make()->columns()->schema([
Forms\Components\TextInput::make('name')
->label(__('Name'))
->required()
->maxLength(255)
->autocomplete(false),

Forms\Components\TextInput::make('email')
->label(__('Email address'))
->email()
->required()
->maxLength(255)
->autocomplete(false)
->unique('users', 'email'),

Forms\Components\TextInput::make('password')
->label(__('Password'))
->password()
->required(fn (string $context): bool => $context === 'create')
->maxLength(255)
->autocomplete(false),

Forms\Components\TextInput::make('password_confirmation')
->password()
->required(fn (string $context): bool => $context === 'create')
->maxLength(255)
->same('password')
->label(__('Confirm Password')),
])
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->label(__('Name'))
->searchable(),

Tables\Columns\TextColumn::make('email')
->label(__('Email address'))
->searchable(),

Tables\Columns\TextColumn::make('email_verified_at')
->label(__('Email Verified At'))
->dateTime(),
])
->filters([
//
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\Action::make('verify-email')
->label(__('Verify Email'))
->icon('heroicon-o-check-badge')
->disabled(fn (Authenticatable $record): bool => $record->hasVerifiedEmail())
->action(fn (Builder $query, Authenticatable $record) => $record->sendEmailVerificationNotification()),
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make(),
]),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
return [
'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
'edit' => Pages\EditUser::route('/{record}/edit'),
];
}

public static function getModel(): string
{
return config('cachet.user_model');
}
}
12 changes: 12 additions & 0 deletions src/Filament/Resources/UserResource/Pages/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Cachet\Filament\Resources\UserResource\Pages;

use Cachet\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\CreateRecord;

class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;
}
19 changes: 19 additions & 0 deletions src/Filament/Resources/UserResource/Pages/EditUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Cachet\Filament\Resources\UserResource\Pages;

use Cachet\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditUser extends EditRecord
{
protected static string $resource = UserResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
19 changes: 19 additions & 0 deletions src/Filament/Resources/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Cachet\Filament\Resources\UserResource\Pages;

use Cachet\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
76 changes: 76 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Cachet\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Cachet\Concerns\CachetUser;
use Cachet\Database\Factories\UserFactory;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

/**
* @property int $id
* @property string $name
* @property string $email
* @property string $password
* @property bool $is_admin
*/
class User extends Authenticatable implements CachetUser, MustVerifyEmail
{
/** @use HasFactory<\Cachet\Database\Factories\UserFactory> */
use HasFactory, Notifiable;

/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'email',
'password',
'is_admin',
];

/**
* The attributes that should be hidden for serialization.
*
* @var list<string>
*/
protected $hidden = [
'password',
'remember_token',
];

/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'email_verified_at' => 'datetime',
'password' => 'hashed',
'is_admin' => 'bool',
];
}

/**
* Determine if the user is an admin.
*/
public function isAdmin(): bool
{
return $this->is_admin;
}

/**
* Create a new factory instance for the model.
*/
protected static function newFactory(): Factory
{
return UserFactory::new();
}
}
10 changes: 10 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@ providers:
- Spatie\LaravelSettings\LaravelSettingsServiceProvider
- Spatie\LaravelData\LaravelDataServiceProvider

env:
- MAIL_MAILER=smtp
- MAIL_HOST=127.0.0.1
- MAIL_PORT=2525
- MAIL_USERNAME=cachet
- MAIL_PASSWORD=null
- MAIL_ENCRYPTION=null
- MAIL_FROM_ADDRESS="[email protected]"
- MAIL_FROM_NAME=Cachet

migrations:
- workbench/database/migrations

Expand Down
16 changes: 3 additions & 13 deletions workbench/app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@

namespace Workbench\App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Cachet\Models\User as CachetUser;

class User extends Authenticatable
class User extends CachetUser
{
use Notifiable;

/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
//
}