mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
38 lines
751 B
Go
38 lines
751 B
Go
package middleware
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/zap-proto/zip"
|
|
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/models/user"
|
|
"github.com/hanzoai/commerce/util/json/http"
|
|
)
|
|
|
|
func AccountRequired() zip.Handler {
|
|
return func(c *zip.Ctx) error {
|
|
tok := GetToken(c)
|
|
|
|
id := tok.UserId
|
|
if id == "" {
|
|
return http.Fail(c, 403, "Access Denied", errors.New("Access Denied"))
|
|
}
|
|
|
|
org := GetOrganization(c)
|
|
db := datastore.New(org.Namespaced(c.Context()))
|
|
u := user.New(db)
|
|
|
|
if err := u.GetById(id); err != nil {
|
|
return http.Fail(c, 403, "Access Denied", errors.New("Access Denied"))
|
|
}
|
|
|
|
c.Locals("user", u)
|
|
return c.Next()
|
|
}
|
|
}
|
|
|
|
func GetUser(c *zip.Ctx) *user.User {
|
|
return c.Locals("user").(*user.User)
|
|
}
|