Skip to content

feat: add built-in roles and user role assignment - #71

Open
PANiXiDA wants to merge 7 commits into
developmentfrom
feature/user-roles
Open

PANiXiDA wants to merge 7 commits into
developmentfrom
feature/user-roles

Conversation

@PANiXiDA

@PANiXiDA PANiXiDA commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Changes

  • Seed Developer, Moderator, and Player at startup using the existing lowercase role names (developer, moderator, player). Preserve existing role IDs and claims; assign Developer during public registration.
  • Accept optional roleIds on user creation/update and return role IDs in user details. On update, omitted/null preserves membership and [] clears it. Reject invalid/unknown IDs before persistence; invalidate sessions when membership changes.
  • Restore Host → Identity.Contracts for generated Wolverine handlers, and allow Host in the architecture rules. The previous production API image failed during publish after this reference was removed.
  • Add Common to both Docker restore stages and Identity.Contracts to the API stage.
  • Add role assignment/seeding coverage and document the contracts and architecture exception.

Validation

  • All standard checks passed on 3d92010 after restoring the original workflow: tests, architecture, formatting, Helm, and Sonar Quality Gate. Latest CI: https://github.com/tactical-heroes/api/actions/runs/38293147207
  • GitHub tests, architecture checks, formatting, Helm, both complete Docker builds, and Sonar Quality Gate passed on 6176235. New-code coverage: 98.90%; duplication: 0.76%; new issues: 0.
  • CI: https://github.com/tactical-heroes/api/actions/runs/38291996310
  • No local .NET or Docker builds/tests were run.
  • Local verification: run dotnet test --project tests/Identity/PANiXiDA.TacticalHeroes.Identity.UnitTests/PANiXiDA.TacticalHeroes.Identity.UnitTests.csproj; repeat for Identity.IntegrationTests, Identity.FunctionalTests, and ArchitectureTests.
  • Container verification: docker build -f src/PANiXiDA.TacticalHeroes.Host/Dockerfile . and docker build -f tools/PANiXiDA.TacticalHeroes.Ef.Migrator/Dockerfile . with configured NuGet credentials.
  • HTTP verification: POST/PUT /api/v1/users with role IDs from /api/v1/roles, read back GET /api/v1/users/{id}, and confirm public registration assigns Developer.

Compatibility

  • Additive contracts: optional request field roleIds and a user-details roleIds field. Role changes invalidate user sessions; existing users are not automatically reassigned. New default roles have no permission/section grants.
  • No database migrations or new configuration. Existing role/user-role tables are reused; no frontend changes.
  • CI workflow is unchanged.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-12T18:06:13.067854Z 8569a17 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8569a17a23

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if (user.Status.IsBlocked)
if (user.Status.IsBlocked || rolesChanged)
{
await RevokeAllTokensAsync(user.Id.Value, cancellationToken);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Commit role changes only after the user update succeeds

When a user with an existing token changes roles, SyncRoles and the other mappings are already tracked in the same scoped IdentityWriteDbContext used by the OpenIddict token manager; revoking the token saves that context before UpdateSecurityStampAsync validates the user. If the request also supplies a duplicate username/email, the later Identity update can return a conflict even though the role membership and mapped fields were already persisted, potentially granting a privileged role despite the failed response. Keep revocation and the user update in a transaction or otherwise prevent token persistence from flushing the pending user graph first.

Useful? React with 👍 / 👎.

Comment on lines +15 to +18
if (await roleManager.FindByNameAsync(name) is not null)
{
continue;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Make role seeding safe across replicas

The production chart was checked and configures two replicas in deploy/helm/tactical-heroes-api/values-production.yaml; during the first deployment, both hosted services can observe a missing role here and then race to create it. The losing replica encounters the unique normalized-role-name constraint and fails startup, either from CreateAsync or the explicit exception below. Treat a duplicate created concurrently as success or serialize the seeding operation.

Useful? React with 👍 / 👎.

Comment on lines +85 to 87
if (user.Status.IsBlocked || rolesChanged)
{
await RevokeAllTokensAsync(user.Id.Value, cancellationToken);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Revoke sessions when an assigned role is modified

This invalidates tokens only when a user's membership set changes. Once this change allows users to be assigned roles, updating a role's claims/name or deleting it through RolesRepository changes the effective authorization graph without touching rolesChanged, the users' security stamps, or their OpenIddict tokens. Existing access tokens therefore retain removed role permissions until expiration despite introspection, so role updates and deletions must also invalidate tokens for every assigned user.

Useful? React with 👍 / 👎.

@panixida-sonarqube

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants