Serve a shop's own staff to the till, so the built-in PINs can retire
The terminal shipped with three names and three PINs compiled into it. Same three on every install, readable by anyone with the APK, and permanent — nothing anywhere could replace them. `/pos/staff` answers with the people the back office says may ring a bill at an outlet, and the same list rides down with the session so a till is ready to trade the moment it signs in. The terminal writes them over its own and deactivates whatever it had, which is what actually kills the seeded logins. Two sources are unioned because the schema has two and neither is complete. `tenantstaffs` is the table built for this and holds 12 rows on the entire platform; `app_users.locationid` is where staff actually ended up. Either alone returns nothing for almost every shop. The endpoint takes no location parameter. The answer carries PINs, so the outlet comes from the caller's token and a request without one is refused whatever POS_AUTH_REQUIRED says — a till must not be able to ask who works at the shop next door. Rows with no PIN are dropped rather than sent: a name on screen nobody can sign in as reads as a broken terminal rather than as an unfinished setup. Duplicate PINs are dropped too, keeping the first — live data has 1234 on eleven accounts and 1111 on nine, and two people sharing one would make the till attribute a bill to whichever row it checked first. The PIN travels in the clear over TLS, deliberately. Four digits are brute-forceable in microseconds however they are wrapped, so hashing here would buy the appearance of strength and not the substance — while costing something real, since the terminal salts every PIN with its own salt before storing it and could never verify a hash computed here. A PIN is shift attribution, not a security boundary; the boundary is the session token. Verified against live data, and it says the fallback still matters: outlet 1135 — the one the POS actually uses — has zero staff, and the only staff row found anywhere is a delivery rider on PIN 1111. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -129,6 +129,15 @@ func (r *posRepository) PosLogin(req models.PosLoginRequest) (*models.PosSession
|
||||
}
|
||||
|
||||
r.decoratePosSession(session)
|
||||
|
||||
// Staff come down with the session so a till is ready to trade the moment
|
||||
// it signs in. A failure here is not a failed sign-in: a shop with no staff
|
||||
// recorded — which is almost all of them today — must still be able to open
|
||||
// its terminal.
|
||||
if staff, err := r.PosStaff(session.Tenantid, session.Locationid); err == nil {
|
||||
session.Staff = staff
|
||||
}
|
||||
|
||||
return session, nil
|
||||
}
|
||||
|
||||
@@ -297,3 +306,61 @@ func constantTimeEqual(a, b string) bool {
|
||||
}
|
||||
return diff == 0
|
||||
}
|
||||
|
||||
// PosStaff lists the people who may ring a bill at an outlet.
|
||||
//
|
||||
// Two sources, unioned, because the schema has two and neither is complete.
|
||||
// `tenantstaffs` is the table built for this and holds 12 rows on the entire
|
||||
// platform; `app_users.locationid` is where staff actually ended up. Reading
|
||||
// only the purpose-built table would return nothing for almost every shop, and
|
||||
// reading only `app_users` would miss anyone assigned through the back office's
|
||||
// staff screen. So both.
|
||||
//
|
||||
// Only people with a PIN come back. A row with `pin = 0` cannot ring anything —
|
||||
// offering it to the till would put a name on screen that no one can sign in
|
||||
// as, which reads as a broken terminal rather than as an unfinished setup.
|
||||
func (r *posRepository) PosStaff(tenantID, locationID int) ([]models.PosStaffMember, error) {
|
||||
rows := make([]models.PosStaffMember, 0)
|
||||
|
||||
query := `
|
||||
SELECT DISTINCT
|
||||
a.userid,
|
||||
TRIM(CONCAT(COALESCE(a.firstname,''), ' ', COALESCE(a.lastname,''))) AS fullname,
|
||||
COALESCE(r.rolename, '') AS role,
|
||||
CAST(a.pin AS TEXT) AS pin,
|
||||
COALESCE(a.status, '') AS status
|
||||
FROM app_users a
|
||||
LEFT JOIN app_roles r ON r.roleid = a.roleid
|
||||
WHERE a.tenantid = ?
|
||||
AND COALESCE(a.pin, 0) > 0
|
||||
AND LOWER(COALESCE(a.status, 'active')) <> 'inactive'
|
||||
AND (
|
||||
a.locationid = ?
|
||||
OR EXISTS (SELECT 1 FROM tenantstaffs s
|
||||
WHERE s.userid = a.userid
|
||||
AND s.tenantid = a.tenantid
|
||||
AND s.locationid = ?
|
||||
AND LOWER(COALESCE(s.status, 'active')) <> 'inactive')
|
||||
)
|
||||
ORDER BY fullname`
|
||||
|
||||
if err := r.db.Raw(query, tenantID, locationID, locationID).Scan(&rows).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// A PIN shared by two people at one outlet would make the till attribute a
|
||||
// bill to whichever row it happened to check first — so the second one is
|
||||
// dropped rather than sent. Live data has 1234 on eleven accounts and 1111
|
||||
// on nine, so this is not hypothetical.
|
||||
seen := make(map[string]bool, len(rows))
|
||||
unique := rows[:0]
|
||||
for _, row := range rows {
|
||||
if seen[row.Pin] {
|
||||
continue
|
||||
}
|
||||
seen[row.Pin] = true
|
||||
unique = append(unique, row)
|
||||
}
|
||||
|
||||
return unique, nil
|
||||
}
|
||||
|
||||
@@ -42,6 +42,7 @@ type PosRepository interface {
|
||||
// own record, rather than being named by the till and believed.
|
||||
PosLogin(req models.PosLoginRequest) (*models.PosSession, error)
|
||||
PosLocationAllowed(tenantID, locationID int) (bool, error)
|
||||
PosStaff(tenantID, locationID int) ([]models.PosStaffMember, error)
|
||||
|
||||
// Reading counter sales back out. Without these a committed bill is
|
||||
// unreachable from every screen in the product.
|
||||
|
||||
Reference in New Issue
Block a user