feat(apps): a sales expert can work inside their customer's account (D29)
Reported missing by the owner: in the reference an expert could open any
of their referred customers and use the app AS them — browse, see
balance and debt, place orders — «یکجورایی انگار با اکانت اون ها لاگین
کرده». D21 had deliberately left it out as a session-hijack primitive.
The owner asked for the capability back with the implementation left to
judgement, so the scope is the reference's and the mechanism is not.
Scope: everything, as the reference had it. Its axios interceptor
swapped the bearer token, so every request went as the customer,
document upload included. No route is closed in act-as mode.
Mechanism: the reference minted and handed over the customer's own
permanent api_token. Here the expert keeps their own token and sends
X-Act-As-Customer, resolved in one place — CustomerJwtStrategy
.validateExpert. That single point is why all 17 customer routes work
unchanged: validate() already returns a principal whose `id` is a
Customer id and every customer route reads it. It also fixes four real
defects of the original:
- ownership is re-checked on EVERY request, with referredByAgentId in
the same where clause as the id, so another expert's customer and a
non-existent id both answer 403, and a customer moved to another
expert loses access at once rather than at token expiry;
- nothing durable is handed out — the reference's token sat in
localStorage with no expiry and kept working after the expert left;
- adminUserId survives, so an order placed this way is attributable;
in the reference the request was literally the customer's;
- 403 rather than 401, so the app clears the act-as state instead of
logging a perfectly valid session out.
Performance: one findFirst on the primary key, only when the header is
present, and it replaces the trading-account lookup rather than adding
to it.
Three things the reference never needed, because it swapped the whole
session. queryCache's `me:` keys, favouritesStore and wageQuoteStore all
dropped their data on a TOKEN change, which act-as does not cause — so
an expert would have seen their own profile, orders, balance, ledger and
favourites inside the customer's account. The wage-quote one is the
money case: making charge is per customer, so the figure on screen would
not have been the one the order was priced at. And uploadDocument builds
its own headers, so an expert's upload would have been filed against
their own account, silently.
Added with no reference equivalent: a permanent «در حال مشاهده بهعنوان
…» bar. There, impersonation was invisible once the token had been
swapped, and placing an order on the wrong customer's gold debt was one
tap away. It is also the exit — the reference's only way out was a
logout that ended the expert's own session too. The cart is cleared in
both directions.
CustomerJwtStrategy had no test coverage at all and is now where this
feature's whole authorisation boundary lives: act-as-customer.spec.ts
covers 11 cases, including the header being ignored on a customer token,
the no-header path asserting the extra query does not run, and a revoked
session short-circuiting before the header is read.
api: 308/308 tests, 28 suites; both apps vue-tsc clean.
اپ: کارشناس فروش میتواند وارد حساب مشتریان خود شود، وضعیتشان را ببیند و از طرفشان سفارش ثبت کند.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
c518f9995b
commit
ddcc8c5649
@@ -21,10 +21,15 @@ export const useFavoritesStore = defineStore('favorites', () => {
|
||||
}
|
||||
}
|
||||
|
||||
// Loaded for whoever is signed in, cleared on logout — never another user's list.
|
||||
// Loaded for whoever is signed in, cleared on logout — never another user's
|
||||
// list. `actingAs` is watched alongside the token because «ورود به حساب
|
||||
// مشتری» changes whose favourites these are WITHOUT changing the token: a
|
||||
// sales expert entering a customer's account would otherwise see their own
|
||||
// hearts on the customer's products, and tapping one would write to the
|
||||
// customer's list.
|
||||
watch(
|
||||
() => session.token,
|
||||
(token) => {
|
||||
() => [session.token, session.actingAs?.id ?? null] as const,
|
||||
([token]) => {
|
||||
ids.value = []
|
||||
if (token) void load()
|
||||
},
|
||||
|
||||
@@ -16,7 +16,8 @@
|
||||
// making it deeply reactive would cost more than the request it saves.
|
||||
//
|
||||
// Keys under `me:` hold one customer's data. They are dropped whenever the
|
||||
// session token changes, so the next user never sees the previous one's data.
|
||||
// session token changes — or the act-as target does, which changes whose data
|
||||
// they hold without changing the token — so one user never sees another's.
|
||||
import { defineStore } from 'pinia'
|
||||
import { watch } from 'vue'
|
||||
import { useSessionStore } from './session'
|
||||
@@ -91,8 +92,13 @@ export const useQueryCache = defineStore('queryCache', () => {
|
||||
}
|
||||
}
|
||||
|
||||
// The token changes when the user changes — and, with «ورود به حساب مشتری»,
|
||||
// WHOSE data `me:` keys hold changes without the token changing at all. Both
|
||||
// have to drop the cache, or a sales expert entering a customer's account
|
||||
// would be shown their own profile, orders, balance and ledger from before
|
||||
// the switch.
|
||||
watch(
|
||||
() => session.token,
|
||||
() => [session.token, session.actingAs?.id ?? null],
|
||||
() => clearUserScoped(),
|
||||
)
|
||||
|
||||
|
||||
@@ -7,6 +7,33 @@ const MOBILE_KEY = 'gold-bank:mobile';
|
||||
const LEGACY_FLAG_KEY = 'appToken';
|
||||
/** D21 — cached so the expert menu entries render before whoami resolves. */
|
||||
const EXPERT_KEY = 'gold-bank:isSalesExpert';
|
||||
/**
|
||||
* «ورود به حساب مشتری» — which of the expert's customers they are working
|
||||
* inside, if any.
|
||||
*
|
||||
* Deliberately `sessionStorage`, not `localStorage`: closing the tab ends the
|
||||
* impersonation. The reference kept the customer's own permanent api_token in
|
||||
* localStorage (myDataStore.ts:163), so it survived restarts indefinitely and
|
||||
* outlived the expert's employment. Here it is only a hint anyway — the server
|
||||
* re-checks on every request that this customer is actually theirs.
|
||||
*/
|
||||
const ACTING_AS_KEY = 'gold-bank:actingAs';
|
||||
|
||||
export interface ActingAs {
|
||||
id: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
function readActingAs(): ActingAs | null {
|
||||
try {
|
||||
const raw = sessionStorage.getItem(ACTING_AS_KEY);
|
||||
if (!raw) return null;
|
||||
const parsed = JSON.parse(raw) as ActingAs;
|
||||
return parsed && typeof parsed.id === 'string' ? parsed : null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The logged-in customer's session.
|
||||
@@ -36,6 +63,14 @@ export const useSessionStore = defineStore('session', {
|
||||
* an ordinary customer, so flipping this in devtools reveals nothing.
|
||||
*/
|
||||
isSalesExpert: localStorage.getItem(EXPERT_KEY) === '1',
|
||||
/**
|
||||
* The customer this expert is currently working as, or null.
|
||||
*
|
||||
* Display and request-header state only — never an authorisation claim.
|
||||
* Putting another customer's id here gets a 403 from the server, which
|
||||
* scopes the lookup to `referredByAgentId` on every single request.
|
||||
*/
|
||||
actingAs: readActingAs() as ActingAs | null,
|
||||
}),
|
||||
getters: {
|
||||
/** Authenticated means holding a token, not merely knowing an id. */
|
||||
@@ -58,6 +93,23 @@ export const useSessionStore = defineStore('session', {
|
||||
if (value) localStorage.setItem(EXPERT_KEY, '1');
|
||||
else localStorage.removeItem(EXPERT_KEY);
|
||||
},
|
||||
|
||||
/**
|
||||
* Enter or leave a customer's account. `null` returns the expert to their
|
||||
* own. The reference did the same through `toggleActiveUser`
|
||||
* (myDataStore.ts:159-169), which swapped the token; nothing is swapped
|
||||
* here — the API client just starts naming this customer in a header.
|
||||
*/
|
||||
setActingAs(value: ActingAs | null) {
|
||||
this.actingAs = value;
|
||||
try {
|
||||
if (value) sessionStorage.setItem(ACTING_AS_KEY, JSON.stringify(value));
|
||||
else sessionStorage.removeItem(ACTING_AS_KEY);
|
||||
} catch {
|
||||
// A private window with storage blocked: the in-memory value still
|
||||
// drives this tab, which is all the feature needs.
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Clears everything. Called on explicit logout and by the API client when a
|
||||
* request comes back 401 — an expired or revoked token must not leave stale
|
||||
@@ -68,6 +120,12 @@ export const useSessionStore = defineStore('session', {
|
||||
this.customerId = null;
|
||||
this.mobile = null;
|
||||
this.isSalesExpert = false;
|
||||
this.actingAs = null;
|
||||
try {
|
||||
sessionStorage.removeItem(ACTING_AS_KEY);
|
||||
} catch {
|
||||
/* storage blocked; the in-memory reset above is what matters */
|
||||
}
|
||||
localStorage.removeItem(EXPERT_KEY);
|
||||
localStorage.removeItem(TOKEN_KEY);
|
||||
localStorage.removeItem(CUSTOMER_ID_KEY);
|
||||
|
||||
@@ -22,8 +22,12 @@ export const useWageQuoteStore = defineStore('wageQuotes', () => {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
// A different customer has a different percent: never reuse another's quotes.
|
||||
// That includes «ورود به حساب مشتری», which swaps WHICH customer we are
|
||||
// quoting for without swapping the token — so a sales expert would otherwise
|
||||
// see their own making charge on the customer's products, and the figure on
|
||||
// screen would not be the one the order is placed at.
|
||||
watch(
|
||||
() => session.token,
|
||||
() => [session.token, session.actingAs?.id ?? null],
|
||||
() => {
|
||||
quotes.value = {}
|
||||
pending.clear()
|
||||
|
||||
Reference in New Issue
Block a user