-
Notifications
You must be signed in to change notification settings - Fork 46
feat(panel-admin): UserResource com List/View/Edit e Role hierarchy #455
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
YuriSouzaDev
wants to merge
6
commits into
he4rt:4.x
Choose a base branch
from
YuriSouzaDev:feat/user-resource-panel-admin
base: 4.x
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0f6baed
feat(panel-admin): UserResource com List/View/Edit e Role hierarchy
YuriSouzaDev 87bada0
fix(panel-admin): restringe acesso ao painel admin por role e central…
YuriSouzaDev 91d9c7b
fix(identity): não apaga address no soft delete e torna rollback seguro
YuriSouzaDev ec9d5b3
fix(identity): normaliza espaços na lista de admins configurados
YuriSouzaDev bc3d71e
fix(identity): garante username sem colisão no rollback de soft-deletes
YuriSouzaDev 310e496
test: atualiza AddressTest pro comportamento correto de soft/force de…
YuriSouzaDev 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
88 changes: 88 additions & 0 deletions
88
...entity/database/migrations/2026_07_26_120000_add_role_and_soft_deletes_to_users_table.php
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,88 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use He4rt\Identity\User\Enums\Role; | ||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\DB; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| public function up(): void | ||
| { | ||
| Schema::table('users', static function (Blueprint $table): void { | ||
| $table->string('role')->default(Role::Member->value)->after('is_donator'); | ||
| $table->softDeletesTz(); | ||
| }); | ||
|
|
||
| DB::statement('ALTER TABLE users DROP CONSTRAINT IF EXISTS users_username_unique'); | ||
|
|
||
| // Scoped to active rows only: soft-deleted users must not block a | ||
| // username from being reused (e.g. account merges reassign it). | ||
| DB::statement('CREATE UNIQUE INDEX users_username_unique ON users (username) WHERE deleted_at IS NULL'); | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| DB::statement('ALTER TABLE users DROP CONSTRAINT IF EXISTS users_username_unique'); | ||
| DB::statement('DROP INDEX IF EXISTS users_username_unique'); | ||
|
|
||
| $this->deduplicateUsernames(); | ||
|
|
||
| Schema::table('users', static function (Blueprint $table): void { | ||
| $table->unique('username'); | ||
| $table->dropColumn(['role', 'deleted_at']); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * up() only enforced uniqueness among active rows, so a soft-deleted row | ||
| * may share a username with an active row (or another trashed row). | ||
| * Rename those duplicates before restoring the global unique constraint, | ||
| * keeping the active row (or the oldest one, if all are trashed) | ||
| * untouched. Candidates are checked against every username already in | ||
| * the table (not just the other duplicates), incrementing a counter | ||
| * until a free one is found, so this can't collide with an unrelated | ||
| * pre-existing username. The suffix is intentionally neutral (not | ||
| * "_deleted_"): the renamed row isn't necessarily trashed. | ||
| */ | ||
| private function deduplicateUsernames(): void | ||
| { | ||
| /** @var array<string, true> $takenUsernames */ | ||
| $takenUsernames = DB::table('users')->pluck('username') | ||
| ->mapWithKeys(static fn (string $username): array => [$username => true]) | ||
| ->all(); | ||
|
|
||
| $duplicateLosers = DB::select(<<<'SQL' | ||
| SELECT id, username | ||
| FROM ( | ||
| SELECT id, username, row_number() OVER ( | ||
| PARTITION BY username | ||
| ORDER BY deleted_at IS NULL DESC, created_at ASC | ||
| ) AS row_number | ||
| FROM users | ||
| ) ranked | ||
| WHERE row_number > 1 | ||
| SQL); | ||
|
|
||
| foreach ($duplicateLosers as $row) { | ||
| /** @var array{id: string, username: string} $row */ | ||
| $row = (array) $row; | ||
| $id = $row['id']; | ||
| $base = $row['username'].'_dup_'.mb_substr($id, 0, 8); | ||
| $candidate = $base; | ||
| $attempt = 1; | ||
|
|
||
| while (isset($takenUsernames[$candidate])) { | ||
| $candidate = $base.'_'.$attempt; | ||
| $attempt++; | ||
| } | ||
|
|
||
| $takenUsernames[$candidate] = true; | ||
|
|
||
| DB::table('users')->where('id', $id)->update(['username' => $candidate]); | ||
| } | ||
| } | ||
| }; | ||
35 changes: 35 additions & 0 deletions
35
...dentity/database/migrations/2026_07_27_000000_promote_configured_admins_to_staff_role.php
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,35 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use He4rt\Identity\User\Enums\Role; | ||
| use He4rt\Identity\User\Models\User; | ||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Support\Facades\DB; | ||
|
|
||
| return new class extends Migration | ||
| { | ||
| /** | ||
| * Backfills users listed in HE4RT_ADMINS_USERNAMES (isAdmin() bypass) | ||
| * that don't already have Staff/Compliance role, so panel authorization | ||
| * relies on a single source of truth (role) instead of the env list. | ||
| */ | ||
| public function up(): void | ||
| { | ||
| $usernames = User::configuredAdminUsernames(); | ||
|
|
||
| if ($usernames === []) { | ||
| return; | ||
| } | ||
|
|
||
| DB::table('users') | ||
| ->whereIn('username', $usernames) | ||
| ->whereNotIn('role', [Role::Staff->value, Role::Compliance->value]) | ||
| ->update(['role' => Role::Staff->value]); | ||
| } | ||
|
|
||
| public function down(): void | ||
| { | ||
| // Data backfill, not reversible. | ||
| } | ||
| }; |
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,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| return [ | ||
| 'role' => [ | ||
| 'staff' => 'Staff', | ||
| 'compliance' => 'Compliance', | ||
| 'recruiter' => 'Recruiter', | ||
| 'squad_captain' => 'Squad Captain', | ||
| 'member' => 'Member', | ||
| ], | ||
| ]; |
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,13 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| return [ | ||
| 'role' => [ | ||
| 'staff' => 'Staff', | ||
| 'compliance' => 'Compliance', | ||
| 'recruiter' => 'Recrutador', | ||
| 'squad_captain' => 'Capitão de Squad', | ||
| 'member' => 'Membro', | ||
| ], | ||
| ]; |
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,73 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace He4rt\Identity\User\Enums; | ||
|
|
||
| use App\Enums\Concerns\StringifyEnum; | ||
| use Filament\Support\Colors\Color; | ||
| use Filament\Support\Contracts\HasColor; | ||
| use Filament\Support\Contracts\HasIcon; | ||
| use Filament\Support\Contracts\HasLabel; | ||
| use Filament\Support\Icons\Heroicon; | ||
|
|
||
| enum Role: string implements HasColor, HasIcon, HasLabel | ||
| { | ||
| use StringifyEnum; | ||
|
|
||
| case Staff = 'staff'; | ||
| case Compliance = 'compliance'; | ||
| case Recruiter = 'recruiter'; | ||
| case SquadCaptain = 'squad_captain'; | ||
| case Member = 'member'; | ||
|
|
||
| public function getLabel(): string | ||
| { | ||
| return __('identity::enums.role.'.$this->value); | ||
| } | ||
|
|
||
| public function getColor(): array | ||
| { | ||
| return match ($this) { | ||
| self::Staff => Color::Amber, | ||
| self::Compliance => Color::Red, | ||
| self::Recruiter => Color::Blue, | ||
| self::SquadCaptain => Color::Purple, | ||
| self::Member => Color::Gray, | ||
| }; | ||
| } | ||
|
|
||
| public function getIcon(): Heroicon | ||
| { | ||
| return match ($this) { | ||
| self::Staff => Heroicon::ShieldCheck, | ||
| self::Compliance => Heroicon::Scale, | ||
| self::Recruiter => Heroicon::Briefcase, | ||
| self::SquadCaptain => Heroicon::Flag, | ||
| self::Member => Heroicon::User, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Compliance é hierarquicamente um superconjunto de Staff: quem pode | ||
| * fazer hard delete também pode editar/soft-deletar. | ||
| */ | ||
| public function isStaff(): bool | ||
| { | ||
| return in_array($this, [self::Staff, self::Compliance], strict: true); | ||
| } | ||
|
|
||
| public function isCompliance(): bool | ||
| { | ||
| return $this === self::Compliance; | ||
| } | ||
|
|
||
| /** | ||
| * Quem enxerga a ficha de um membro no painel: staff/compliance (gestão) | ||
| * e recruiter/squad captain (recrutamento), mas não member. | ||
| */ | ||
| public function canViewUsers(): bool | ||
| { | ||
| return in_array($this, [self::Staff, self::Compliance, self::Recruiter, self::SquadCaptain], strict: true); | ||
| } | ||
| } |
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.