Task: Refactor MODX authorization considering mgr/web contexts
Refactor MODX authorization considering mgr/web contexts
Fix MODX authorization checks to correctly account for separate user states in the mgr and web contexts without relying on the existence of a shared session.
Context
During development, an important nuance of MODX authentication became clear: after a user logs into the admin panel, their session state is added to the mgr context. If you then check authorization specifically in the web context, the user may be absent there.
At the same time, you cannot simply consider a user authorized based solely on the existence of a MODX session. Within the same physical PHP session, MODX allows different user identities for different contexts. That is, a single browser session can contain, for example, one user in mgr and another in web.
This is not a specific bug, but a consequence of the MODX architecture, where the authentication state is context-specific. For a detailed explanation, see the concept:
https://modx.club/concepts/modx-pays-for-rarely-used-complexity
Problem
The current authorization logic risks making one of two mistakes:
- Checking only
weband erroneously considering the user unauthorized, even though they are logged into the Manager (mgr). - Checking only for the presence of a shared MODX session and erroneously trusting it without understanding which specific user is authorized in the required context.
Both options are incorrect.
Goal
Make MODX authorization checks unambiguous and secure: explicitly account for the context, the actual contextToken, and the user identity, rather than inferring authorization state from indirect signs.
What to Study
- Exactly how MODX stores
modx.user.contextTokensin the current version of the project. - How
mgrandwebbehave when different users log in sequentially within the same browser/PHP session. - Which MODX APIs correctly return the user for a specific context.
- What happens upon logging out from just one context.
- How the session state behaves after updating permissions/cache and re-logging in.
- Whether the application specifically requires authorization in
web, inmgr, or if an explicitly chosen trusted context is sufficient.
What to Do
- Remove logic that considers a user authorized solely based on the existence of a MODX session.
- Explicitly determine which context the check should run in for a specific scenario.
- Retrieve the user's identity/token specifically for the required context.
- If business logic allows authorization via the Manager, explicitly handle
mgras a separate valid identity source rather than trying to find the same user inweb. - Do not assume that the
mgruser andwebuser in the same session are the same. - If necessary, add a check to ensure user id/identity matches the expected scenario.
- Properly handle the absence of a token for the required context.
- Add diagnostic behavior for ambiguous states where different users are present in
mgrandweb.
Verification Scenarios
- User is authorized only in
mgr. - User is authorized only in
web. - The same user is authorized in both
mgrandweb. - Different users are authorized in
mgrandwebwithin the same PHP session. - User logged out of
webbut remained inmgr. - User logged out of
mgrbut remained inweb. - An outdated/invalid token exists in the session.
- After resetting permissions/cache, the user state changes or requires re-login.
Result
Authorization is determined by the explicit state of the required MODX context and the specific user identity. The presence of a shared browser/PHP session is no longer treated as sufficient proof of authorization.
Definition of Done
- The check correctly distinguishes between
mgrandweb. - Supports the scenario where a Manager user is authorized only in
mgr. - Different users in
mgrandwebare not mixed up. - No trust in the session without checking the context-specific identity/token.
- All listed scenarios are covered by tests or reproducible checks.
- Behavior is documented so that the next developer does not treat the MODX session as a single global user state.
Architectural Reference
In MODX, a context is not just a set of settings, but also part of the security/authentication state. In a single physical session, modx.user.contextTokens can contain different user IDs for mgr and web. This is a fundamental feature of the platform, not a project-specific exception.
Source: https://modx.club/concepts/modx-pays-for-rarely-used-complexity
Ворклоги
The new personal account uses the existing MODX user authentication
The production version of the new personal account implements a fundamentally important scenario: the new frontend works with the current MODX user authentication rather than introducing a separate login system.
A user who is already authenticated in the existing system can go to the new account and continue working with their data and applications. This is especially important for a phased launch, because the old and new personal accounts currently operate in parallel and should be perceived as two shells over a single account and a single backend.
The current result confirms the viability of the integration scheme for the new frontend with MODX authentication/session. The new pass request section is already operating using this authentication.
At the same time, the separate task of correctly handling context-specific mgr/web authentication remains relevant: the production integration uses the current MODX user, but the nuances of separate context tokens and ambiguous session states need to be brought to a fully formalized and tested scheme.