fix(chat): 修复超级管理员查看分站二维码图片被拒
This commit is contained in:
@@ -232,6 +232,67 @@ func TestPrivateFileAuthorizerAllowsDeliveredQrCodeParticipants(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminPrivateFileAuthorizerAllowsSuperAdminDistributionQrCodes(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
role := model.Role{Code: "super_admin", Name: "超级管理员"}
|
||||
if err := db.Create(&role).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
admins := []model.AdminUser{
|
||||
{Username: "file-super-admin", PasswordHash: "x", Status: "active"},
|
||||
{Username: "file-normal-admin", PasswordHash: "x", Status: "active"},
|
||||
}
|
||||
if err := db.Create(&admins).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := db.Create(&model.AdminUserRole{
|
||||
AdminID: admins[0].ID,
|
||||
RoleID: role.ID,
|
||||
TenantID: 0,
|
||||
}).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
site := model.DistributionSite{
|
||||
SiteCode: "file_dist_qr",
|
||||
SiteName: "文件权限分销站",
|
||||
Domain: "file-dist-qr.example.com",
|
||||
Status: "active",
|
||||
}
|
||||
if err := db.Create(&site).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
distributionSiteID := site.ID
|
||||
qrcode := model.ChatQrCode{
|
||||
TenantID: 101,
|
||||
DistributionSiteID: &distributionSiteID,
|
||||
ImageURL: "/api/files/object?key=qrcode%2Fdistribution-site.png",
|
||||
Status: "unused",
|
||||
CreatedBy: admins[0].ID,
|
||||
}
|
||||
if err := db.Create(&qrcode).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
authorize := adminPrivateFileAuthorizer(db)
|
||||
ctxA := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 101})
|
||||
ctxB := tenancy.WithScope(context.Background(), tenancy.Scope{TenantID: 202})
|
||||
key := "qrcode/distribution-site.png"
|
||||
if allowed, err := authorize(ctxA, admins[0].ID, key); err != nil || !allowed {
|
||||
t.Fatalf("super admin distribution qrcode allowed=%v err=%v", allowed, err)
|
||||
}
|
||||
if allowed, err := authorize(ctxA, admins[1].ID, key); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if allowed {
|
||||
t.Fatal("normal admin must not read distribution site qrcode")
|
||||
}
|
||||
if allowed, err := authorize(ctxB, admins[0].ID, key); err != nil {
|
||||
t.Fatal(err)
|
||||
} else if allowed {
|
||||
t.Fatal("super admin must not read another tenant's distribution qrcode")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAdminPrivateFileAuthorizerAllowsOnlySuperAdminFinanceVouchers(t *testing.T) {
|
||||
db := openAuthorizerTestDB(t)
|
||||
roles := []model.Role{
|
||||
|
||||
@@ -251,10 +251,15 @@ WHERE (? = 0 OR c.tenant_id = ?) AND ` + chatMembers,
|
||||
args: append([]any{tenantID, tenantID}, jsonArgs...),
|
||||
}}, true
|
||||
case "qrcode", "support-qrcode":
|
||||
qrcodeScope := "(q.tenant_id = ? AND (q.distribution_site_id IS NULL OR q.distribution_site_id = 0))"
|
||||
if isPlatformSuperAdmin(ctx, db, adminID) {
|
||||
// 平台超级管理员需要查看经营租户下各历史分销站点的独立码池。
|
||||
qrcodeScope = "(q.tenant_id = ?)"
|
||||
}
|
||||
return []fileAccessQuery{
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM chat_qrcode_pool q
|
||||
WHERE (? = 0 OR (q.tenant_id = ? AND (q.distribution_site_id IS NULL OR q.distribution_site_id = 0)))
|
||||
WHERE (? = 0 OR ` + qrcodeScope + `)
|
||||
AND ` + qrcodeClause,
|
||||
args: append([]any{tenantID, tenantID}, qrcodeArgs...),
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user