This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
// Copyright (C) 2024 veypi <i@veypi.com>
// 2025-03-04 16:08:06
// Distributed under terms of the MIT license.
packagemiddleware
import(
"github.com/veypi/vbase/cfg"
"github.com/veypi/vbase/models"
"github.com/veypi/vigo"
)
// OrgContext 组织上下文中间件
// 从header或query参数中获取org_id,并验证用户是否为该组织成员
funcOrgContext()func(*vigo.X)error{
returnfunc(x*vigo.X)error{
orgID:=x.Request.Header.Get("X-Org-ID")
iforgID==""{
orgID=x.Request.URL.Query().Get("org_id")
}
iforgID==""{
// 没有指定组织,跳过
returnnil
}
userID:=""
ifuid,ok:=x.Get("user_id").(string);ok{
userID=uid
}
ifuserID==""{
returnvigo.ErrNotAuthorized
}
// 验证用户是否为组织成员
varmembermodels.OrgMember
iferr:=cfg.DB().Where("org_id = ? AND user_id = ? AND status = ?",