Skip to content

Recover role management work and fix what it would break - #31

Merged
HeftyB merged 9 commits into
mainfrom
feature/role-management
Sep 18, 2026
Merged

HeftyB merged 9 commits into
mainfrom
feature/role-management

Conversation

@HeftyB

@HeftyB HeftyB commented Sep 18, 2026

Copy link
Copy Markdown
Owner

Closes #13

Merge with "Create a merge commit", not squash, so d6fd705 keeps its February 2025 date.

Background

d6fd705 is the last commit that survived a hardware failure in early 2025. It was pushed to working but never merged, so the last two months of work were invisible from main. It adds role management (RoleController, RoleDepartment), a rebuilt HR administrator page, and a switch from jakarta.validation-api to spring-boot-starter-validation.

This PR brings that commit in unchanged and fixes what it would have broken. The branch was brought up to date by merging main into it rather than rebasing, which would have rewritten the original commit.

Fixes on top of the recovered commit

Role endpoints were open to any logged-in user. RoleController relied on @PreAuthorize, but method security was never enabled, and /roles/** sat outside the /admin/** URL rule.

  • Enabled @EnableMethodSecurity. This also makes the existing check on AdminController effective.
  • Moved the endpoints to /admin/roles, so the URL rule and the method check each protect them independently.
  • Removed GET /roles, which rendered a role_home template that doesn't exist.
  • Redirect to /admin instead of echoing the Referer header, which became redirect:null when the header was missing.
  • Parameters are bound as typed values (RoleDepartment, long), so bad input gets a 400 instead of a 500.

Every new repair order would have failed to save. Before merging, I audited everything the validation starter turns on. With a Bean Validation provider present, Hibernate validates entities before insert and update, so constraints that were decorative on main become enforced:

Checked Result
43 constrained entity fields 5 are primitives, where @NotNull can never fail
Code paths that create those entities (repair orders, jobs, time punches, timesheets, employees, customers, users) Required fields are set in every path, except one:
RepairOrder.priority @Pattern("/([0-9])+/g"), a JavaScript regex literal that no value can match, including the field's own default "0". Fixed to [0-9]; the form input is now a 0–9 number field

Also removed a leftover "Cool Button" with no behavior from the HR page's roles toolbar.

Behavior changes to know about

  • @Valid is now enforced. A customer without a last name, or an employee with a blank first or last name, gets a 400 instead of being saved.
  • A repair order with no advisor is rejected. That happens when the logged-in user has no linked employee record; previously it saved with a null advisor.

Tests

The commits are ordered test-first for both bugs: the failing test lands first, then the fix.

Test class Covers
RoleControllerSecurityTest (6) anonymous → login; non-admin → 403; the method check alone rejects non-admins; admin create/delete; unknown department → 400
RepairOrderValidationTest (9) default priority valid; 0/5/9 valid; "", " ", a, 10, -1 rejected
CustomerControllerTest (2) full create flow persists and redirects; missing last name → 400

18 tests passing, 0 DDL errors on the H2 test profile.

Not in this PR

🤖 Generated with Claude Code

HeftyB and others added 9 commits February 6, 2025 01:15
RoleController carries @PreAuthorize("hasRole('ROLE_ADMIN')"), but
method security is never enabled, and /roles/** falls outside the
/admin/** URL rule. Any authenticated user can create or delete
roles. The non-admin case currently gets a 302 and the role is
saved; it should get a 403.

Adds spring-security-test for @WithMockUser and csrf().

[Ticket: #13]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Without @EnableMethodSecurity, the @PreAuthorize annotations on
RoleController and AdminController are silently ignored. Enabling
it makes the role check on RoleController effective, so non-admin
users now get a 403.

[Ticket: #13]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Role management now sits behind both the /admin/** URL rule and the
controller's @PreAuthorize check, instead of relying on one alone.

- Remove GET /roles, which rendered a role_home template that was
  never written. The HR administrator page already lists roles.
- Redirect to /admin after create and delete instead of echoing the
  Referer header, which produced "redirect:null" when it was absent.
- Bind department as the RoleDepartment enum and roleId as a long,
  so bad input returns 400 instead of a 500 from valueOf/parseLong.
- Use hasRole('ADMIN') in both controllers, matching the URL rule;
  Spring adds the ROLE_ prefix.

Tests cover delete, bad input, and the method-level check on its
own, independent of the URL rule.

[Ticket: #13]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
RepairOrder.priority is constrained with @pattern("/([0-9])+/g"),
a JavaScript regex literal. Java treats the slashes and "g" as
literal characters, so no single-digit priority can match, including
the field's own default of "0".

This was harmless while the project had no Bean Validation provider.
The spring-boot-starter-validation dependency from the working branch
makes Hibernate validate entities before insert, at which point every
new repair order would be rejected.

[Ticket: #13]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Replace the JavaScript-style pattern with "[0-9]", a single digit,
which also makes @SiZe(max = 1) redundant. The new repair order form
now uses a number input limited to 0-9, so the browser stops
out-of-range values before they reach the server.

[Ticket: #13]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
With a Bean Validation provider on the classpath, @Valid on
CustomerController is enforced for the first time. Cover the normal
create flow end to end, and confirm a customer without a last name
is now rejected with 400 instead of being saved.

[Ticket: #13]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
The roles toolbar had a "Cool Button" with no id, handler or action.

[Ticket: #13]

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@HeftyB HeftyB added this to the Portfolio readiness milestone Sep 18, 2026
@HeftyB HeftyB added bug Something isn't working enhancement New feature or request security Security-relevant change testing Tests and test infrastructure labels Sep 18, 2026
@HeftyB HeftyB self-assigned this Sep 18, 2026
@HeftyB
HeftyB merged commit aa37c1f into main Sep 18, 2026
1 check passed
@HeftyB
HeftyB deleted the feature/role-management branch September 18, 2026 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request security Security-relevant change testing Tests and test infrastructure

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Merge recovered role-management work from the working branch

1 participant