#!/usr/bin/env python3 """组织隔离实测:切换临时用户的角色,用两个 JWT 打同一组接口。 - token A: users.id=7 role=2 organization_id=NULL → 总管理员,应看到全部 - token B: users.id=7 role=1 organization_id=1 → 组织1管理员,应看不到组织B的 7777 """ import time import pymysql import jwt SECRET = "32e8708743c42fe764aff1d40737a63803a46815a9501f4bcb50c41fe3d27108" FACTS = "facts.txt" def token_for(role, org_id): conn = pymysql.connect(host="10.8.0.252", user="root", password="Hbhyg731024@", database="pms_dev") cur = conn.cursor() cur.execute("UPDATE users SET role=%s, organization_id=%s WHERE id=7", (role, org_id)) conn.commit() cur.close() conn.close() now = int(time.time()) return jwt.encode({"sub": 7, "phone": "19900000001", "exp": now + 86400, "iat": now}, SECRET, algorithm="HS256") if __name__ == "__main__": with open(FACTS, "w", encoding="utf-8") as fh: fh.write("SUPER=" + token_for(2, None) + "\n") fh.write("ORG1=" + token_for(1, 1) + "\n") print(open(FACTS, encoding="utf-8").read())