groups and roles
This commit is contained in:
parent
f82d3f59e0
commit
0b78ea4d03
50
app/Filament/Resources/Groups/GroupResource.php
Normal file
50
app/Filament/Resources/Groups/GroupResource.php
Normal file
@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Groups;
|
||||
|
||||
use App\Filament\Resources\Groups\Pages\CreateGroup;
|
||||
use App\Filament\Resources\Groups\Pages\EditGroup;
|
||||
use App\Filament\Resources\Groups\Pages\ListGroups;
|
||||
use App\Filament\Resources\Groups\Schemas\GroupForm;
|
||||
use App\Filament\Resources\Groups\Tables\GroupsTable;
|
||||
use App\Models\Group;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class GroupResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Group::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
|
||||
|
||||
protected static ?string $recordTitleAttribute = 'name';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return GroupForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return GroupsTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getRelations(): array
|
||||
{
|
||||
return [
|
||||
//
|
||||
];
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListGroups::route('/'),
|
||||
'create' => CreateGroup::route('/create'),
|
||||
'edit' => EditGroup::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
11
app/Filament/Resources/Groups/Pages/CreateGroup.php
Normal file
11
app/Filament/Resources/Groups/Pages/CreateGroup.php
Normal file
@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Groups\Pages;
|
||||
|
||||
use App\Filament\Resources\Groups\GroupResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateGroup extends CreateRecord
|
||||
{
|
||||
protected static string $resource = GroupResource::class;
|
||||
}
|
||||
19
app/Filament/Resources/Groups/Pages/EditGroup.php
Normal file
19
app/Filament/Resources/Groups/Pages/EditGroup.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Groups\Pages;
|
||||
|
||||
use App\Filament\Resources\Groups\GroupResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditGroup extends EditRecord
|
||||
{
|
||||
protected static string $resource = GroupResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/Groups/Pages/ListGroups.php
Normal file
19
app/Filament/Resources/Groups/Pages/ListGroups.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Groups\Pages;
|
||||
|
||||
use App\Filament\Resources\Groups\GroupResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListGroups extends ListRecords
|
||||
{
|
||||
protected static string $resource = GroupResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
20
app/Filament/Resources/Groups/Schemas/GroupForm.php
Normal file
20
app/Filament/Resources/Groups/Schemas/GroupForm.php
Normal file
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Groups\Schemas;
|
||||
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class GroupForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
TextInput::make('name')
|
||||
->required(),
|
||||
TextInput::make('description')
|
||||
->label('Beschreibung'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
42
app/Filament/Resources/Groups/Tables/GroupsTable.php
Normal file
42
app/Filament/Resources/Groups/Tables/GroupsTable.php
Normal file
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Groups\Tables;
|
||||
|
||||
use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class GroupsTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('created_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('updated_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
TextColumn::make('name')
|
||||
->searchable(),
|
||||
TextColumn::make('description')
|
||||
->searchable(),
|
||||
])
|
||||
->filters([
|
||||
//
|
||||
])
|
||||
->recordActions([
|
||||
EditAction::make(),
|
||||
])
|
||||
->toolbarActions([
|
||||
BulkActionGroup::make([
|
||||
DeleteBulkAction::make(),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Users\Pages;
|
||||
|
||||
use App\Filament\Resources\Users\UserResource;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Resources\Pages\ViewRecord;
|
||||
|
||||
class ViewUser extends ViewRecord
|
||||
{
|
||||
protected static string $resource = UserResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -2,8 +2,11 @@
|
||||
|
||||
namespace App\Filament\Resources\Users\Schemas;
|
||||
|
||||
use App\Filament\Resources\Groups\GroupResource;
|
||||
use App\Filament\Resources\Groups\Schemas\GroupForm;
|
||||
use Filament\Forms\Components\Checkbox;
|
||||
use Filament\Forms\Components\DateTimePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
@ -20,10 +23,23 @@ class UserForm
|
||||
->email()
|
||||
->required(),
|
||||
DateTimePicker::make('email_verified_at'),
|
||||
Checkbox::make('ordermanager'),
|
||||
TextInput::make('password')
|
||||
->password()
|
||||
->required(),
|
||||
->required()
|
||||
->visibleOn(['create']),
|
||||
Select::make('roles')
|
||||
->relationship('roles', 'title')
|
||||
->label('Rollen')
|
||||
->preload()
|
||||
->multiple(),
|
||||
Select::make('groups')
|
||||
->relationship('groups', 'name')
|
||||
->label('Gruppen')
|
||||
->preload()
|
||||
->createOptionForm(function(Schema $schema) {
|
||||
return GroupForm::configure($schema);
|
||||
})
|
||||
->multiple(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,6 +14,10 @@ class UserInfolist
|
||||
TextEntry::make('name'),
|
||||
TextEntry::make('email')
|
||||
->label('Email address'),
|
||||
TextEntry::make('groups.title')
|
||||
->label('Gruppen'),
|
||||
TextEntry::make('roles.title')
|
||||
->label('Rollen'),
|
||||
TextEntry::make('email_verified_at')
|
||||
->dateTime()
|
||||
->placeholder('-'),
|
||||
|
||||
@ -6,7 +6,6 @@ use Filament\Actions\BulkActionGroup;
|
||||
use Filament\Actions\DeleteBulkAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Actions\ViewAction;
|
||||
use Filament\Tables\Columns\CheckboxColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
@ -24,7 +23,12 @@ class UsersTable
|
||||
TextColumn::make('email_verified_at')
|
||||
->dateTime()
|
||||
->sortable(),
|
||||
CheckboxColumn::make('ordermanager'),
|
||||
TextColumn::make('roles.title')
|
||||
->label("Rollen")
|
||||
->toggleable(),
|
||||
TextColumn::make('groups.name')
|
||||
->label("Gruppen")
|
||||
->toggleable(),
|
||||
TextColumn::make('created_at')
|
||||
->dateTime()
|
||||
->sortable()
|
||||
|
||||
@ -5,7 +5,6 @@ namespace App\Filament\Resources\Users;
|
||||
use App\Filament\Resources\Users\Pages\CreateUser;
|
||||
use App\Filament\Resources\Users\Pages\EditUser;
|
||||
use App\Filament\Resources\Users\Pages\ListUsers;
|
||||
use App\Filament\Resources\Users\Pages\ViewUser;
|
||||
use App\Filament\Resources\Users\Schemas\UserForm;
|
||||
use App\Filament\Resources\Users\Schemas\UserInfolist;
|
||||
use App\Filament\Resources\Users\Tables\UsersTable;
|
||||
@ -54,7 +53,6 @@ class UserResource extends Resource
|
||||
return [
|
||||
'index' => ListUsers::route('/'),
|
||||
'create' => CreateUser::route('/create'),
|
||||
'view' => ViewUser::route('/{record}'),
|
||||
'edit' => EditUser::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
|
||||
17
app/Models/Group.php
Normal file
17
app/Models/Group.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Group extends Model
|
||||
{
|
||||
protected $fillable = ['name', 'title', 'description'];
|
||||
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class);
|
||||
}
|
||||
|
||||
}
|
||||
17
app/Models/Role.php
Normal file
17
app/Models/Role.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class Role extends Model
|
||||
{
|
||||
protected $fillable = ['name', 'title', 'description'];
|
||||
|
||||
public function users(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class);
|
||||
}
|
||||
|
||||
}
|
||||
@ -10,10 +10,11 @@ use Database\Factories\UserFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Hidden;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
#[Fillable(['name', 'email', 'password', 'ordermanager'])]
|
||||
#[Fillable(['name', 'email', 'password'])]
|
||||
#[Hidden(['password', 'remember_token'])]
|
||||
class User extends Authenticatable implements FilamentUser
|
||||
{
|
||||
@ -37,4 +38,19 @@ class User extends Authenticatable implements FilamentUser
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
|
||||
public function roles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Role::class);
|
||||
}
|
||||
|
||||
public function groups(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Group::class);
|
||||
}
|
||||
|
||||
public function hasRole(string $role): bool
|
||||
{
|
||||
return in_array($role, $this->roles()->pluck('name')->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ namespace App\Observers;
|
||||
use App\Mail\OrderArrived;
|
||||
use App\Mail\OrderRegistered;
|
||||
use App\Models\Order;
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use Illuminate\Support\Str;
|
||||
@ -18,7 +19,7 @@ class OrderObserver
|
||||
{
|
||||
$order->public_uuid = (string) Str::uuid();
|
||||
$order->save();
|
||||
$users = User::where("ordermanager", 1)->get();
|
||||
$users = Role::where("name", "ordermanager")->users()->get();
|
||||
foreach ($users as $user) {
|
||||
Mail::to($user)->send(new OrderRegistered($order));
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use Filament\Tables\Columns\Column;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
@ -31,5 +32,8 @@ class AppServiceProvider extends ServiceProvider
|
||||
->selectable()
|
||||
->defaultPaginationPageOption(50);
|
||||
});
|
||||
Column::configureUsing(function (Column $column): void {
|
||||
$column->toggleable();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
31
database/migrations/2026_06_02_135203_create_roles_table.php
Normal file
31
database/migrations/2026_06_02_135203_create_roles_table.php
Normal file
@ -0,0 +1,31 @@
|
||||
<?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('roles', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->string('name');
|
||||
$table->string('title');
|
||||
$table->string('description')->nullable();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('roles');
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,29 @@
|
||||
<?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('groups', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->string('name');
|
||||
$table->string('description')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('groups');
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Group;
|
||||
use App\Models\User;
|
||||
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('group_user', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->foreignIdFor(User::class);
|
||||
$table->foreignIdFor(Group::class);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('group_user');
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
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('role_user', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->timestamps();
|
||||
$table->foreignIdFor(Role::class);
|
||||
$table->foreignIdFor(User::class);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('role_user');
|
||||
}
|
||||
};
|
||||
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
use App\Models\Role;
|
||||
use App\Models\User;
|
||||
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
|
||||
{
|
||||
$ordermanager = Role::firstOrCreate(["name" => "ordermanager", 'title' => 'Ordermanager', 'description' => 'Verwaltet Bestellungen'])->save();
|
||||
$users = User::all();
|
||||
foreach($users as $user) {
|
||||
if($user->ordermanager) $user->roles()->attach($ordermanager);
|
||||
}
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropColumn('ordermanager');
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->boolean("ordermanager")->default(false);
|
||||
});
|
||||
$users = User::all();
|
||||
foreach($users as $user) {
|
||||
if($user->hasRole("ordermanager")) {
|
||||
$user->ordermanager = 1;
|
||||
$user->save();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user