数据:收窄账号所有权与单值凭证读取
This commit is contained in:
1 parent
aa3003ceda
commit
b32f0a1173
13 files changed
+165
-56
No files matched your search
@@ -129,7 +129,7 @@ func (s *Server) listAutomationRules(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
cookieID := strings.TrimSpace(query.Get("cookie_id"))
|
||||
if cookieID != "" {
|
||||
if _, ok := s.cookieForUser(r, sess.UserID, cookieID); !ok {
|
||||
if !s.cookieOwnedByUser(r.Context(), sess.UserID, cookieID) {
|
||||
writeErr(w, http.StatusForbidden, "无权限操作该账号")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -693,7 +693,7 @@ func (s *Server) deleteCookie(w http.ResponseWriter, r *http.Request) {
|
||||
// setCookieAutoConfirm 设置自动确认发货。
|
||||
func (s *Server) setCookieAutoConfirm(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -728,7 +728,7 @@ func (s *Server) getCookieAutoConfirm(w http.ResponseWriter, r *http.Request) {
|
||||
// setCookieRemark 设置备注。
|
||||
func (s *Server) setCookieRemark(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -749,7 +749,7 @@ func (s *Server) setCookieRemark(w http.ResponseWriter, r *http.Request) {
|
||||
// setCookiePauseDuration 设置暂停时长。
|
||||
func (s *Server) setCookiePauseDuration(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -776,7 +776,7 @@ func (s *Server) setCookiePauseDuration(w http.ResponseWriter, r *http.Request)
|
||||
// getCookiePauseDuration 获取暂停时长。
|
||||
func (s *Server) getCookiePauseDuration(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
paused, pausedUntil, _ := s.Store.Cookies.IsPaused(r.Context(), cid)
|
||||
|
||||
@@ -40,7 +40,7 @@ func (s *Server) mountDefaultRepliesReal(r chi.Router) {
|
||||
|
||||
func (s *Server) getDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
dr, err := s.Store.DefaultReps.Get(r.Context(), cid)
|
||||
@@ -56,7 +56,7 @@ func (s *Server) getDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) setDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -153,7 +153,7 @@ func (s *Server) listDefaultRepliesMap(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) deleteDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
_, err := s.Store.DB.ExecContext(r.Context(), `DELETE FROM default_replies WHERE cookie_id=?`, cid)
|
||||
@@ -166,7 +166,7 @@ func (s *Server) deleteDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) clearDefaultReplyRecords(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
if _, err := s.Store.DB.ExecContext(r.Context(), `DELETE FROM default_reply_records WHERE cookie_id=?`, cid); err != nil {
|
||||
|
||||
@@ -632,7 +632,7 @@ func (s *Server) enrichSyncedItemMultiSpec(ctx context.Context, client mtop.Clie
|
||||
|
||||
func (s *Server) listItemsByCookie(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
items, err := s.Store.Items.AllForCookie(r.Context(), cid)
|
||||
@@ -649,7 +649,7 @@ func (s *Server) listItemsByCookie(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) getItem(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
itemID := chi.URLParam(r, "item_id")
|
||||
@@ -668,7 +668,7 @@ func (s *Server) getItem(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) createItem(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -702,7 +702,7 @@ func (s *Server) createItem(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) updateItem(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
itemID := chi.URLParam(r, "item_id")
|
||||
@@ -773,7 +773,7 @@ func (s *Server) updateItem(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) deleteItem(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
itemID := chi.URLParam(r, "item_id")
|
||||
@@ -786,7 +786,7 @@ func (s *Server) deleteItem(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) setItemMultiSpec(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
itemID := chi.URLParam(r, "item_id")
|
||||
@@ -806,7 +806,7 @@ func (s *Server) setItemMultiSpec(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) setItemMultiQuantity(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
itemID := chi.URLParam(r, "item_id")
|
||||
|
||||
@@ -1400,44 +1400,53 @@ func (s *Server) cleanupExpiredPublishUploads(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// cookieOwnedByUser 判断指定账号是否属于用户,不读取或解密 Cookie 明文。
|
||||
func (s *Server) cookieOwnedByUser(ctx context.Context, userID int64, cookieID string) bool {
|
||||
all, err := s.Store.Cookies.AllForUser(ctx, userID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
_, ok := all[cookieID]
|
||||
return ok
|
||||
// owned 表示数据库中是否存在匹配用户和账号 ID 的记录。
|
||||
owned, err := s.Store.Cookies.ExistsOwned(ctx, userID, cookieID)
|
||||
return err == nil && owned
|
||||
}
|
||||
|
||||
// cookieValueForUser 读取指定用户拥有的单个账号 Cookie 明文。
|
||||
func (s *Server) cookieValueForUser(ctx context.Context, userID int64, cookieID string) (string, error) {
|
||||
all, err := s.Store.Cookies.AllForUser(ctx, userID)
|
||||
// value 是按 user_id 与账号 ID 联合过滤后解密的单个 Cookie 明文。
|
||||
value, err := s.Store.Cookies.GetValueOwned(ctx, userID, cookieID)
|
||||
if err != nil {
|
||||
if errors.Is(err, db.ErrNotFound) {
|
||||
return "", errors.New("账号不存在或 Cookie 为空")
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
value, ok := all[cookieID]
|
||||
if !ok || strings.TrimSpace(value) == "" {
|
||||
if strings.TrimSpace(value) == "" {
|
||||
return "", errors.New("账号不存在或 Cookie 为空")
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// cardOwnedByUser 判断指定卡券组是否属于用户。
|
||||
func (s *Server) cardOwnedByUser(ctx context.Context, userID int64, cardID int64) bool {
|
||||
// exists 表示数据库中是否存在匹配用户和卡券组 ID 的记录。
|
||||
var exists bool
|
||||
// err 表示卡券组所有权查询失败的原因。
|
||||
err := s.Store.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM cards WHERE id=? AND user_id=?)`, cardID, userID).Scan(&exists)
|
||||
return err == nil && exists
|
||||
}
|
||||
|
||||
// publishUploadRoot 返回发布图片上传文件的根目录。
|
||||
func (s *Server) publishUploadRoot() string {
|
||||
return defaultPublishUploadRoot()
|
||||
}
|
||||
|
||||
// defaultPublishUploadRoot 返回环境变量指定或默认的发布上传目录。
|
||||
func defaultPublishUploadRoot() string {
|
||||
// v 是去除首尾空白后的上传目录环境变量值。
|
||||
if v := strings.TrimSpace(os.Getenv("XIANYU_UPLOAD_DIR")); v != "" {
|
||||
return v
|
||||
}
|
||||
return filepath.Join("data", "uploads")
|
||||
}
|
||||
|
||||
// parseLooseBool 将常见的真值文本转换为布尔值。
|
||||
func parseLooseBool(raw string) bool {
|
||||
raw = strings.ToLower(strings.TrimSpace(raw))
|
||||
switch raw {
|
||||
@@ -1448,18 +1457,22 @@ func parseLooseBool(raw string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// atoiPublishDefault 将数字文本转换为整数,无法解析时返回给定默认值。
|
||||
func atoiPublishDefault(raw string, def int) int {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return def
|
||||
}
|
||||
// f 和 err 分别表示解析出的浮点数及其错误。
|
||||
if f, err := strconv.ParseFloat(raw, 64); err == nil {
|
||||
return int(f)
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// firstNonEmpty 返回参数中第一个非空白字符串。
|
||||
func firstNonEmpty(values ...string) string {
|
||||
// v 是当前遍历到的候选字符串。
|
||||
for _, v := range values {
|
||||
if strings.TrimSpace(v) != "" {
|
||||
return v
|
||||
|
||||
@@ -26,7 +26,7 @@ func (s *Server) mountKeywordsReal(r chi.Router) {
|
||||
|
||||
func (s *Server) listKeywords(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
rows, err := s.Store.Keywords.AllRows(r.Context(), cid)
|
||||
@@ -43,7 +43,7 @@ func (s *Server) listKeywords(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) listKeywordsWithItemID(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
rows, err := s.Store.Keywords.AllRows(r.Context(), cid)
|
||||
@@ -60,7 +60,7 @@ func (s *Server) listKeywordsWithItemID(w http.ResponseWriter, r *http.Request)
|
||||
|
||||
func (s *Server) listKeywordsWithType(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
rows, err := s.Store.Keywords.AllRows(r.Context(), cid)
|
||||
@@ -80,7 +80,7 @@ func (s *Server) listKeywordsWithType(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) addKeyword(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -104,7 +104,7 @@ func (s *Server) addKeyword(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) addKeywordWithItemID(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
@@ -204,7 +204,7 @@ func (s *Server) addKeywordWithItemID(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) updateKeywordByID(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
@@ -261,7 +261,7 @@ func (s *Server) updateKeywordByID(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) deleteKeywordByID(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
||||
@@ -278,7 +278,7 @@ func (s *Server) deleteKeywordByID(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) deleteKeyword(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
index := atoiDefault(chi.URLParam(r, "index"), -1)
|
||||
@@ -314,7 +314,7 @@ func (s *Server) listItemReplies(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) getItemReply(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
itemID := chi.URLParam(r, "item_id")
|
||||
@@ -330,7 +330,7 @@ func (s *Server) getItemReply(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) setItemReply(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
itemID := chi.URLParam(r, "item_id")
|
||||
@@ -350,7 +350,7 @@ func (s *Server) setItemReply(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) deleteItemReply(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
itemID := chi.URLParam(r, "item_id")
|
||||
|
||||
@@ -158,7 +158,7 @@ func (s *Server) testChannel(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) getAccountBindings(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
ids, err := s.Store.Notifications.AccountBindings(r.Context(), cid)
|
||||
@@ -208,7 +208,7 @@ func (s *Server) listMessageNotifications(w http.ResponseWriter, r *http.Request
|
||||
|
||||
func (s *Server) setAccountBindings(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cid")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
|
||||
@@ -56,7 +56,7 @@ func (s *Server) listOrders(w http.ResponseWriter, r *http.Request) {
|
||||
pageSize = 20
|
||||
}
|
||||
if cookieID != "" {
|
||||
if _, ok := s.cookieForUser(r, sess.UserID, cookieID); !ok {
|
||||
if !s.cookieOwnedByUser(r.Context(), sess.UserID, cookieID) {
|
||||
writeErr(w, http.StatusForbidden, "无权限操作该账号")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -25,12 +25,45 @@ func (s *Server) requireCookieOwner(w http.ResponseWriter, r *http.Request, cook
|
||||
return d, true
|
||||
}
|
||||
|
||||
// requireCookieOwnership 校验当前会话是否拥有账号,只读取账号所有权元数据,不解密凭证。
|
||||
func (s *Server) requireCookieOwnership(w http.ResponseWriter, r *http.Request, cookieID string) bool {
|
||||
// sess 是当前请求经过认证中间件注入的会话。
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
if sess == nil {
|
||||
writeErr(w, http.StatusUnauthorized, "未授权访问")
|
||||
return false
|
||||
}
|
||||
// owned 表示当前用户是否直接拥有该账号。
|
||||
owned, ownedErr := s.Store.Cookies.ExistsOwned(r.Context(), sess.UserID, cookieID)
|
||||
if ownedErr != nil {
|
||||
writeErr(w, http.StatusNotFound, "账号不存在")
|
||||
return false
|
||||
}
|
||||
if owned {
|
||||
return true
|
||||
}
|
||||
// ownerID 用于区分账号不存在和账号属于其他用户,保持原有 404/403 响应语义。
|
||||
ownerID, ownerErr := s.Store.Cookies.GetOwnerID(r.Context(), cookieID)
|
||||
if ownerErr != nil {
|
||||
writeErr(w, http.StatusNotFound, "账号不存在")
|
||||
return false
|
||||
}
|
||||
if ownerID != sess.UserID {
|
||||
writeErr(w, http.StatusForbidden, "无权限操作该账号")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// requireOrderOwner 校验当前会话是否拥有订单关联的账号。
|
||||
func (s *Server) requireOrderOwner(w http.ResponseWriter, r *http.Request, orderID string) (*db.Order, bool) {
|
||||
// sess 是当前请求经过认证中间件注入的会话。
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
if sess == nil {
|
||||
writeErr(w, http.StatusUnauthorized, "未授权访问")
|
||||
return nil, false
|
||||
}
|
||||
// order 和 err 分别表示订单记录及其查询错误。
|
||||
order, err := s.Store.Orders.Get(r.Context(), orderID)
|
||||
if err != nil || order == nil {
|
||||
writeErr(w, http.StatusNotFound, "订单不存在")
|
||||
@@ -40,19 +73,23 @@ func (s *Server) requireOrderOwner(w http.ResponseWriter, r *http.Request, order
|
||||
writeErr(w, http.StatusForbidden, "订单未绑定账号,无法操作")
|
||||
return nil, false
|
||||
}
|
||||
if _, ok := s.cookieForUser(r, sess.UserID, order.CookieID); !ok {
|
||||
// owned 表示当前会话是否拥有订单绑定的账号。
|
||||
if !s.cookieOwnedByUser(r.Context(), sess.UserID, order.CookieID) {
|
||||
writeErr(w, http.StatusForbidden, "无权操作此订单")
|
||||
return nil, false
|
||||
}
|
||||
return order, true
|
||||
}
|
||||
|
||||
// requireCardOwner 校验当前会话是否拥有卡券组。
|
||||
func (s *Server) requireCardOwner(w http.ResponseWriter, r *http.Request, cardID int64) (*db.CardFull, bool) {
|
||||
// sess 是当前请求经过认证中间件注入的会话。
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
if sess == nil {
|
||||
writeErr(w, http.StatusUnauthorized, "未授权访问")
|
||||
return nil, false
|
||||
}
|
||||
// card 和 err 分别表示卡券组记录及其查询错误。
|
||||
card, err := s.Store.Cards.Get(r.Context(), cardID)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusNotFound, "卡券不存在")
|
||||
@@ -65,13 +102,17 @@ func (s *Server) requireCardOwner(w http.ResponseWriter, r *http.Request, cardID
|
||||
return card, true
|
||||
}
|
||||
|
||||
// requireChannelOwner 校验当前会话是否拥有通知渠道。
|
||||
func (s *Server) requireChannelOwner(w http.ResponseWriter, r *http.Request, channelID int64) bool {
|
||||
// sess 是当前请求经过认证中间件注入的会话。
|
||||
sess := auth.SessionFromContext(r.Context())
|
||||
if sess == nil {
|
||||
writeErr(w, http.StatusUnauthorized, "未授权访问")
|
||||
return false
|
||||
}
|
||||
// exists 表示通知渠道是否由当前用户拥有。
|
||||
var exists bool
|
||||
// err 表示通知渠道所有权查询失败的原因。
|
||||
err := s.Store.DB.QueryRowContext(r.Context(),
|
||||
`SELECT EXISTS(SELECT 1 FROM notification_channels WHERE id=? AND user_id=?)`,
|
||||
channelID, sess.UserID).Scan(&exists)
|
||||
@@ -81,12 +122,3 @@ func (s *Server) requireChannelOwner(w http.ResponseWriter, r *http.Request, cha
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) cookieForUser(r *http.Request, userID int64, cookieID string) (string, bool) {
|
||||
all, err := s.Store.Cookies.AllForUser(r.Context(), userID)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
value, ok := all[cookieID]
|
||||
return value, ok
|
||||
}
|
||||
@@ -155,7 +155,7 @@ func (s *Server) listAIReply(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (s *Server) getAIReply(w http.ResponseWriter, r *http.Request) {
|
||||
cid := chi.URLParam(r, "cookie_id")
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
cfg, err := s.Store.AIReply.Get(r.Context(), cid)
|
||||
@@ -196,7 +196,7 @@ func (s *Server) setAIReply(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
if _, ok := s.requireCookieOwner(w, r, cid); !ok {
|
||||
if !s.requireCookieOwnership(w, r, cid) {
|
||||
return
|
||||
}
|
||||
if req.MaxDiscountPercent < 0 || req.MaxDiscountPercent > 100 {
|
||||
|
||||
Reference in new issue
Block a user