-
Notifications
You must be signed in to change notification settings - Fork 4
#5 improve user role and site management #8
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
Open
thapacodes4u
wants to merge
20
commits into
DataLinx:main
Choose a base branch
from
thapacodes4u:#5-improve-user-role-and-site-management
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
7b3c664
refactor(tests): update middleware reference to SetupSite
thapacodes4u dfd2877
fix: improve user role and site management
thapacodes4u 9570f80
fix: resolving conflict
thapacodes4u 4bfefec
fix: resolving formatting issue
thapacodes4u 1dece7a
fix: removing commented codes & improving site selector in role-resource
thapacodes4u 01fbf73
fix: site id showing in role view page
thapacodes4u 4073a06
fix: completing UserTest
thapacodes4u 6e14906
fix: failing test
thapacodes4u 124f622
fix: removing sites form field from UserResource
thapacodes4u 9848e20
fix: error in testcases
thapacodes4u eebe71e
fix: resolving conflict
thapacodes4u de8ce4c
test: add more user tests
SlimDeluxe 0ae5bc8
Merge branch 'main' of github.com:thapacodes4u/eclipsephp-core into #…
b01fc2c
feat: adding role and permission in user resource
16ac22f
fix: resolving conflict
31803db
feat: implement global and site-specific role system for users
abe64f3
fix: resolving conflict
755946e
fix: revert something back
694fbc2
fix: resolving the failing tests
4a4309a
fix: clean comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| <?php | ||
|
|
||
| namespace Eclipse\Core\Filament\Resources; | ||
|
|
||
| use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions; | ||
| use BezhanSalleh\FilamentShield\Forms\ShieldSelectAllToggle; | ||
| use Eclipse\Core\Filament\Resources\RoleResource\Pages; | ||
| use BezhanSalleh\FilamentShield\Support\Utils; | ||
| use BezhanSalleh\FilamentShield\Traits\HasShieldFormComponents; | ||
| use Filament\Facades\Filament; | ||
| use Filament\Forms; | ||
| use Filament\Forms\Form; | ||
| use Filament\Pages\SubNavigationPosition; | ||
| use Filament\Resources\Resource; | ||
| use Filament\Tables; | ||
| use Filament\Tables\Table; | ||
| use Illuminate\Contracts\Support\Arrayable; | ||
| use Illuminate\Support\HtmlString; | ||
| use Illuminate\Support\Str; | ||
| use Illuminate\Validation\Rules\Unique; | ||
|
|
||
| class RoleResource extends Resource implements HasShieldPermissions | ||
| { | ||
| use HasShieldFormComponents; | ||
|
|
||
| protected static ?string $recordTitleAttribute = 'name'; | ||
|
|
||
| public static function getPermissionPrefixes(): array | ||
| { | ||
| return [ | ||
| 'view', | ||
| 'view_any', | ||
| 'create', | ||
| 'update', | ||
| 'delete', | ||
| 'delete_any', | ||
| ]; | ||
| } | ||
|
|
||
| public static function form(Form $form): Form | ||
| { | ||
| return $form | ||
| ->schema([ | ||
| Forms\Components\Grid::make() | ||
| ->schema([ | ||
| Forms\Components\Section::make() | ||
| ->schema([ | ||
| Forms\Components\TextInput::make('name') | ||
| ->label(__('filament-shield::filament-shield.field.name')) | ||
| ->unique( | ||
| ignoreRecord: true, /** @phpstan-ignore-next-line */ | ||
| modifyRuleUsing: fn (Unique $rule) => ! Utils::isTenancyEnabled() ? $rule : $rule->where(Utils::getTenantModelForeignKey(), Filament::getTenant()?->id) | ||
| ) | ||
| ->required() | ||
| ->maxLength(255), | ||
|
|
||
| Forms\Components\TextInput::make('guard_name') | ||
| ->label(__('filament-shield::filament-shield.field.guard_name')) | ||
| ->default(Utils::getFilamentAuthGuard()) | ||
| ->nullable() | ||
| ->maxLength(255), | ||
|
|
||
| Forms\Components\Select::make(config('permission.column_names.team_foreign_key')) | ||
| ->label(__('filament-shield::filament-shield.field.team')) | ||
| ->placeholder(__('filament-shield::filament-shield.field.team.placeholder')) | ||
| /** @phpstan-ignore-next-line */ | ||
| ->default([Filament::getTenant()?->id]) | ||
| ->options(fn (): Arrayable => Utils::getTenantModel() ? Utils::getTenantModel()::pluck('name', 'id') : collect()) | ||
| ->dehydrated(fn (): bool => ! (static::shield()->isCentralApp() && Utils::isTenancyEnabled())), | ||
| ShieldSelectAllToggle::make('select_all') | ||
| ->onIcon('heroicon-s-shield-check') | ||
| ->offIcon('heroicon-s-shield-exclamation') | ||
| ->label(__('filament-shield::filament-shield.field.select_all.name')) | ||
| ->helperText(fn (): HtmlString => new HtmlString(__('filament-shield::filament-shield.field.select_all.message'))) | ||
| ->dehydrated(fn (bool $state): bool => $state), | ||
|
|
||
| ]) | ||
| ->columns([ | ||
| 'sm' => 2, | ||
| 'lg' => 3, | ||
| ]), | ||
| ]), | ||
| static::getShieldFormComponents(), | ||
| ]); | ||
| } | ||
|
|
||
| public static function table(Table $table): Table | ||
| { | ||
| return $table | ||
| ->columns([ | ||
| Tables\Columns\TextColumn::make('name') | ||
| ->weight('font-medium') | ||
| ->label(__('filament-shield::filament-shield.column.name')) | ||
| ->formatStateUsing(fn ($state): string => Str::headline($state)) | ||
| ->searchable(), | ||
| Tables\Columns\TextColumn::make('guard_name') | ||
| ->badge() | ||
| ->color('warning') | ||
| ->label(__('filament-shield::filament-shield.column.guard_name')), | ||
| Tables\Columns\TextColumn::make('site.name') | ||
| ->default('Global') | ||
| ->badge() | ||
| ->color(fn (mixed $state): string => str($state)->contains('Global') ? 'gray' : 'primary') | ||
| ->label(__('filament-shield::filament-shield.column.team')) | ||
| ->searchable(), | ||
| Tables\Columns\TextColumn::make('permissions_count') | ||
| ->badge() | ||
| ->label(__('filament-shield::filament-shield.column.permissions')) | ||
| ->counts('permissions') | ||
| ->colors(['success']), | ||
| Tables\Columns\TextColumn::make('updated_at') | ||
| ->label(__('filament-shield::filament-shield.column.updated_at')) | ||
| ->dateTime(), | ||
| ]) | ||
| ->filters([ | ||
| // | ||
| ]) | ||
| ->actions([ | ||
| Tables\Actions\EditAction::make(), | ||
| Tables\Actions\DeleteAction::make(), | ||
| ]) | ||
| ->bulkActions([ | ||
| Tables\Actions\DeleteBulkAction::make(), | ||
| ]); | ||
| } | ||
|
|
||
| public static function getRelations(): array | ||
| { | ||
| return [ | ||
| // | ||
| ]; | ||
| } | ||
|
|
||
| public static function getPages(): array | ||
| { | ||
| return [ | ||
| 'index' => Pages\ListRoles::route('/'), | ||
| 'create' => Pages\CreateRole::route('/create'), | ||
| 'view' => Pages\ViewRole::route('/{record}'), | ||
| 'edit' => Pages\EditRole::route('/{record}/edit'), | ||
| ]; | ||
| } | ||
|
|
||
| public static function getCluster(): ?string | ||
| { | ||
| return Utils::getResourceCluster() ?? static::$cluster; | ||
| } | ||
|
|
||
| public static function getModel(): string | ||
| { | ||
| return Utils::getRoleModel(); | ||
| } | ||
|
|
||
| public static function getModelLabel(): string | ||
| { | ||
| return __('filament-shield::filament-shield.resource.label.role'); | ||
| } | ||
|
|
||
| public static function getPluralModelLabel(): string | ||
| { | ||
| return __('filament-shield::filament-shield.resource.label.roles'); | ||
| } | ||
|
|
||
| public static function shouldRegisterNavigation(): bool | ||
| { | ||
| return Utils::isResourceNavigationRegistered(); | ||
| } | ||
|
|
||
| public static function getNavigationGroup(): ?string | ||
| { | ||
| return Utils::isResourceNavigationGroupEnabled() | ||
| ? __('filament-shield::filament-shield.nav.group') | ||
| : ''; | ||
| } | ||
|
|
||
| public static function getNavigationLabel(): string | ||
| { | ||
| return __('filament-shield::filament-shield.nav.role.label'); | ||
| } | ||
|
|
||
| public static function getNavigationIcon(): string | ||
| { | ||
| return __('filament-shield::filament-shield.nav.role.icon'); | ||
| } | ||
|
|
||
| public static function getNavigationSort(): ?int | ||
| { | ||
| return Utils::getResourceNavigationSort(); | ||
| } | ||
|
|
||
| public static function getSubNavigationPosition(): SubNavigationPosition | ||
| { | ||
| return Utils::getSubNavigationPosition() ?? static::$subNavigationPosition; | ||
| } | ||
|
|
||
| public static function getSlug(): string | ||
| { | ||
| return Utils::getResourceSlug(); | ||
| } | ||
|
|
||
| public static function getNavigationBadge(): ?string | ||
| { | ||
| return Utils::isResourceNavigationBadgeEnabled() | ||
| ? strval(static::getEloquentQuery()->count()) | ||
| : null; | ||
| } | ||
|
|
||
| public static function isScopedToTenant(): bool | ||
| { | ||
| return Utils::isScopedToTenant(); | ||
| } | ||
|
|
||
| public static function canGloballySearch(): bool | ||
| { | ||
| return Utils::isResourceGloballySearchable() && count(static::getGloballySearchableAttributes()) && static::canViewAny(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| namespace Eclipse\Core\Filament\Resources\RoleResource\Pages; | ||
|
|
||
| use Eclipse\Core\Filament\Resources\RoleResource; | ||
| use BezhanSalleh\FilamentShield\Support\Utils; | ||
| use Filament\Resources\Pages\CreateRecord; | ||
| use Illuminate\Support\Arr; | ||
| use Illuminate\Support\Collection; | ||
|
|
||
| class CreateRole extends CreateRecord | ||
| { | ||
| protected static string $resource = RoleResource::class; | ||
|
|
||
| public Collection $permissions; | ||
|
|
||
| protected function mutateFormDataBeforeCreate(array $data): array | ||
| { | ||
| $this->permissions = collect($data) | ||
| ->filter(function ($permission, $key) { | ||
| return ! in_array($key, ['name', 'guard_name', 'select_all', Utils::getTenantModelForeignKey()]); | ||
| }) | ||
| ->values() | ||
| ->flatten() | ||
| ->unique(); | ||
|
|
||
| if (Arr::has($data, Utils::getTenantModelForeignKey())) { | ||
| return Arr::only($data, ['name', 'guard_name', Utils::getTenantModelForeignKey()]); | ||
| } | ||
|
|
||
| return Arr::only($data, ['name', 'guard_name']); | ||
| } | ||
|
|
||
| protected function afterCreate(): void | ||
| { | ||
| $permissionModels = collect(); | ||
| $this->permissions->each(function ($permission) use ($permissionModels) { | ||
| $permissionModels->push(Utils::getPermissionModel()::firstOrCreate([ | ||
| /** @phpstan-ignore-next-line */ | ||
| 'name' => $permission, | ||
| 'guard_name' => $this->data['guard_name'], | ||
| ])); | ||
| }); | ||
|
|
||
| $this->record->syncPermissions($permissionModels); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?php | ||
|
|
||
| namespace Eclipse\Core\Filament\Resources\RoleResource\Pages; | ||
|
|
||
| use Eclipse\Core\Filament\Resources\RoleResource; | ||
| use BezhanSalleh\FilamentShield\Support\Utils; | ||
| use Filament\Actions; | ||
| use Filament\Resources\Pages\EditRecord; | ||
| use Illuminate\Support\Arr; | ||
| use Illuminate\Support\Collection; | ||
|
|
||
| class EditRole extends EditRecord | ||
| { | ||
| protected static string $resource = RoleResource::class; | ||
|
|
||
| public Collection $permissions; | ||
|
|
||
| protected function getActions(): array | ||
| { | ||
| return [ | ||
| Actions\DeleteAction::make(), | ||
| ]; | ||
| } | ||
|
|
||
| protected function mutateFormDataBeforeSave(array $data): array | ||
| { | ||
| $this->permissions = collect($data) | ||
| ->filter(function ($permission, $key) { | ||
| return ! in_array($key, ['name', 'guard_name', 'select_all', Utils::getTenantModelForeignKey()]); | ||
| }) | ||
| ->values() | ||
| ->flatten() | ||
| ->unique(); | ||
|
|
||
| if (Arr::has($data, Utils::getTenantModelForeignKey())) { | ||
| return Arr::only($data, ['name', 'guard_name', Utils::getTenantModelForeignKey()]); | ||
| } | ||
|
|
||
| return Arr::only($data, ['name', 'guard_name']); | ||
| } | ||
|
|
||
| protected function afterSave(): void | ||
| { | ||
| $permissionModels = collect(); | ||
| $this->permissions->each(function ($permission) use ($permissionModels) { | ||
| $permissionModels->push(Utils::getPermissionModel()::firstOrCreate([ | ||
| 'name' => $permission, | ||
| 'guard_name' => $this->data['guard_name'], | ||
| ])); | ||
| }); | ||
|
|
||
| $this->record->syncPermissions($permissionModels); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| <?php | ||
|
|
||
| namespace Eclipse\Core\Filament\Resources\RoleResource\Pages; | ||
|
|
||
| use Eclipse\Core\Filament\Resources\RoleResource; | ||
| use Filament\Actions; | ||
| use Filament\Resources\Pages\ListRecords; | ||
|
|
||
| class ListRoles extends ListRecords | ||
| { | ||
| protected static string $resource = RoleResource::class; | ||
|
|
||
| protected function getActions(): array | ||
| { | ||
| return [ | ||
| Actions\CreateAction::make(), | ||
| ]; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.