fix(chat): 修复跨站会话文件权限
This commit is contained in:
@@ -15,6 +15,32 @@ type fileAccessQuery struct {
|
||||
args []any
|
||||
}
|
||||
|
||||
func conversationTenantAccessClause(alias string) string {
|
||||
return `(
|
||||
? = 0
|
||||
OR ` + alias + `.tenant_id = ?
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro
|
||||
WHERE ro.id = ` + alias + `.order_id
|
||||
AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM rental_listings rl
|
||||
WHERE rl.id = ` + alias + `.listing_id
|
||||
AND rl.source_tenant_id = ?
|
||||
)
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM rental_orders rlo
|
||||
WHERE rlo.listing_id = ` + alias + `.listing_id
|
||||
AND rlo.buyer_tenant_id = ?
|
||||
)
|
||||
)`
|
||||
}
|
||||
|
||||
func conversationTenantAccessArgs(tenantID uint64) []any {
|
||||
return []any{tenantID, tenantID, tenantID, tenantID, tenantID, tenantID}
|
||||
}
|
||||
|
||||
func fileReferenceValues(key, basePrefix string) []string {
|
||||
seen := make(map[string]struct{}, 16)
|
||||
values := make([]string, 0, 16)
|
||||
@@ -141,13 +167,9 @@ WHERE (o.owner_id = ? OR o.renter_id = ?)
|
||||
JOIN chat_participants p ON p.conversation_id = m.conversation_id
|
||||
LEFT JOIN chat_conversations c ON c.id = m.conversation_id
|
||||
WHERE p.participant_type = 'user' AND p.participant_id = ?
|
||||
AND (? = 0 OR c.tenant_id = ? OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro WHERE ro.id = c.order_id AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
) OR EXISTS (
|
||||
SELECT 1 FROM rental_listings rl WHERE rl.id = c.listing_id AND rl.source_tenant_id = ?
|
||||
))
|
||||
AND ` + conversationTenantAccessClause("c") + `
|
||||
AND ` + chatMembers,
|
||||
args: append([]any{userID, tenantID, tenantID, tenantID, tenantID, tenantID}, jsonArgs...),
|
||||
args: append(append([]any{userID}, conversationTenantAccessArgs(tenantID)...), jsonArgs...),
|
||||
}}, true
|
||||
case "qrcode", "support-qrcode":
|
||||
return []fileAccessQuery{
|
||||
@@ -155,24 +177,18 @@ WHERE p.participant_type = 'user' AND p.participant_id = ?
|
||||
sql: `SELECT COUNT(1) FROM chat_qrcode_pool q
|
||||
JOIN chat_conversations c ON c.id = q.conversation_id
|
||||
JOIN chat_participants p ON p.conversation_id = c.id AND p.participant_type = 'user' AND p.participant_id = ?
|
||||
WHERE (? = 0 OR c.tenant_id = ? OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro WHERE ro.id = c.order_id AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
))
|
||||
WHERE ` + conversationTenantAccessClause("c") + `
|
||||
AND ` + qrcodeClause,
|
||||
args: append([]any{userID, tenantID, tenantID, tenantID, tenantID}, qrcodeArgs...),
|
||||
args: append(append([]any{userID}, conversationTenantAccessArgs(tenantID)...), qrcodeArgs...),
|
||||
},
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM chat_messages m
|
||||
JOIN chat_participants p ON p.conversation_id = m.conversation_id
|
||||
LEFT JOIN chat_conversations c ON c.id = m.conversation_id
|
||||
WHERE p.participant_type = 'user' AND p.participant_id = ?
|
||||
AND (? = 0 OR c.tenant_id = ? OR EXISTS (
|
||||
SELECT 1 FROM rental_orders ro WHERE ro.id = c.order_id AND (ro.buyer_tenant_id = ? OR ro.source_tenant_id = ?)
|
||||
) OR EXISTS (
|
||||
SELECT 1 FROM rental_listings rl WHERE rl.id = c.listing_id AND rl.source_tenant_id = ?
|
||||
))
|
||||
AND ` + conversationTenantAccessClause("c") + `
|
||||
AND ` + chatMembers,
|
||||
args: append([]any{userID, tenantID, tenantID, tenantID, tenantID, tenantID}, jsonArgs...),
|
||||
args: append(append([]any{userID}, conversationTenantAccessArgs(tenantID)...), jsonArgs...),
|
||||
},
|
||||
}, true
|
||||
case "handoff":
|
||||
@@ -244,30 +260,48 @@ WHERE (? = 0 OR (l.source_tenant_id = ? AND (l.distribution_site_id IS NULL OR l
|
||||
args: append([]any{tenantID, tenantID}, jsonArgs...),
|
||||
}}, true
|
||||
case "chat":
|
||||
if isPlatformSuperAdmin(ctx, db, adminID) {
|
||||
return []fileAccessQuery{{
|
||||
sql: `SELECT COUNT(1) FROM chat_messages m WHERE ` + chatMembers,
|
||||
args: jsonArgs,
|
||||
}}, true
|
||||
}
|
||||
return []fileAccessQuery{{
|
||||
sql: `SELECT COUNT(1) FROM chat_messages m
|
||||
JOIN chat_conversations c ON c.id = m.conversation_id
|
||||
WHERE (? = 0 OR c.tenant_id = ?) AND ` + chatMembers,
|
||||
args: append([]any{tenantID, tenantID}, jsonArgs...),
|
||||
WHERE ` + conversationTenantAccessClause("c") + ` AND ` + chatMembers,
|
||||
args: append(conversationTenantAccessArgs(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 tenantID == 0 {
|
||||
return []fileAccessQuery{{
|
||||
sql: `SELECT COUNT(1) FROM chat_qrcode_pool q WHERE ` + qrcodeClause,
|
||||
args: qrcodeArgs,
|
||||
}}, true
|
||||
}
|
||||
poolScope := "(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 = ?)"
|
||||
poolScope = "(q.tenant_id = ?)"
|
||||
}
|
||||
return []fileAccessQuery{
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM chat_qrcode_pool q
|
||||
WHERE (? = 0 OR ` + qrcodeScope + `)
|
||||
AND ` + qrcodeClause,
|
||||
args: append([]any{tenantID, tenantID}, qrcodeArgs...),
|
||||
WHERE (
|
||||
` + poolScope + `
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM chat_conversations c
|
||||
WHERE c.id = q.conversation_id
|
||||
AND ` + conversationTenantAccessClause("c") + `
|
||||
)
|
||||
)
|
||||
AND ` + qrcodeClause,
|
||||
args: append(append([]any{tenantID}, conversationTenantAccessArgs(tenantID)...), qrcodeArgs...),
|
||||
},
|
||||
{
|
||||
sql: `SELECT COUNT(1) FROM chat_messages m
|
||||
JOIN chat_conversations c ON c.id = m.conversation_id
|
||||
WHERE (? = 0 OR c.tenant_id = ?) AND ` + chatMembers,
|
||||
args: append([]any{tenantID, tenantID}, jsonArgs...),
|
||||
WHERE ` + conversationTenantAccessClause("c") + ` AND ` + chatMembers,
|
||||
args: append(conversationTenantAccessArgs(tenantID), jsonArgs...),
|
||||
},
|
||||
}, true
|
||||
case "handoff":
|
||||
|
||||
Reference in New Issue
Block a user