# Permission-Aware Action

## Objective

Decide once how a control behaves when the user is not allowed to use it.

A shared wrapper for buttons, menu items, and links that resolves visibility, disabled state, and the reason a user cannot proceed.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Inventory every action in the app that any role can be denied, and give each one a named capability rather than scattering role comparisons through the components that render it.
2. Pick one of three treatments per action and record the choice: hide it, show it disabled with a reason, or show it enabled and let the attempt fail into an upgrade or request-access path. Defaulting everything to hidden makes the product look smaller than it is.
3. Whenever an action is disabled, say why in words the user can act on — the role that is required, the prerequisite that is missing, the plan limit that has been reached — and where possible offer the next step.
4. Treat prerequisites and permissions through the same wrapper. A save blocked by an incomplete profile and a save blocked by a viewer role should not be two unrelated mechanisms.
5. Do not treat the client-side check as enforcement. The server must reject the request independently, and the wrapper exists only to spare the user a pointless attempt.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Every capability the interface consults must be enforced again on the server for the same request. A hidden button is a courtesy, not a control, and anyone can call the endpoint directly.
- Hiding, disabling, and explaining are three different decisions with three different costs, and each action needs a deliberate one. Hiding teaches the user nothing; disabling without a reason is worse than hiding; explaining an action they will never be granted is noise.
- Some denials must not disclose that the thing exists. Telling a user they lack permission on a specific record confirms the record is real, so for those cases present the same not-found response an unrelated identifier would produce.
- Roles, seats, and workspace membership change while a session is open. The wrapper must re-evaluate on those changes rather than holding whatever was true at first render.
- A disabled control must still be reachable by keyboard and its reason must be announced, because a tooltip that only appears on hover is invisible to a screen reader and to a touch user.
- Actions rendered inside a bulk selection need a defined behaviour when the user may act on some of the chosen records and not others — act on the permitted subset and say so, or refuse the whole batch, but not silently drop rows.
- The reason text must not leak internal policy names, permission strings, or the identity of whoever holds the missing role.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Every deniable action in the app is declared as a named capability and rendered through the shared wrapper.
- [ ] Each action has a recorded, deliberate treatment: hidden, disabled with a reason, or attemptable.
- [ ] The server rejects every denied action independently of the interface.
- [ ] Denials that would confirm the existence of a private record are indistinguishable from not-found.
- [ ] A role or workspace change updates the affected controls within the same session, without a reload.
- [ ] Disabled controls are keyboard reachable and their reasons are announced to assistive technology.
- [ ] No reason string exposes an internal policy or permission identifier.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
