feat: Implement public access for tenant content
- Add TenantOptionalAuth middleware to allow access to public content without requiring authentication. - Introduce ListPublicPublished and PublicDetail methods in the content service to retrieve publicly accessible content. - Create tenant_public HTTP routes for listing and showing public content, including preview and main asset retrieval. - Enhance content tests to cover scenarios for public content access and permissions. - Update specifications to reflect the new public content access features and rules.
This commit is contained in:
@@ -8,6 +8,8 @@ import (
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/super"
|
||||
"quyun/v2/app/http/tenant"
|
||||
"quyun/v2/app/http/tenant_join"
|
||||
"quyun/v2/app/http/tenant_public"
|
||||
"quyun/v2/app/jobs"
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/app/services"
|
||||
@@ -53,6 +55,8 @@ func Command() atom.Option {
|
||||
middlewares.Provide,
|
||||
super.Provide,
|
||||
tenant.Provide,
|
||||
tenant_join.Provide,
|
||||
tenant_public.Provide,
|
||||
// {Provider: api.Provide},
|
||||
// {Provider: web.Provide},
|
||||
),
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package tenantjoin
|
||||
package tenant_join
|
||||
|
||||
import (
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/tenantjoin/dto"
|
||||
"quyun/v2/app/http/tenant_join/dto"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/providers/jwt"
|
||||
@@ -81,7 +81,7 @@ func (*join) createJoinRequest(
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenant.ID,
|
||||
"user_id": claims.UserID,
|
||||
}).Info("tenantjoin.create_join_request")
|
||||
}).Info("tenant_join.create_join_request")
|
||||
|
||||
return services.Tenant.CreateJoinRequest(ctx.Context(), tenant.ID, claims.UserID, form)
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package tenantjoin
|
||||
package tenant_join
|
||||
|
||||
import (
|
||||
"quyun/v2/app/middlewares"
|
||||
@@ -1,11 +1,11 @@
|
||||
// Code generated by atomctl. DO NOT EDIT.
|
||||
|
||||
// Package tenantjoin provides HTTP route definitions and registration
|
||||
// Package tenant_join provides HTTP route definitions and registration
|
||||
// for the quyun/v2 application.
|
||||
package tenantjoin
|
||||
package tenant_join
|
||||
|
||||
import (
|
||||
"quyun/v2/app/http/tenantjoin/dto"
|
||||
"quyun/v2/app/http/tenant_join/dto"
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/providers/jwt"
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
)
|
||||
|
||||
// Routes implements the HttpRoute contract and provides route registration
|
||||
// for all controllers in the tenantjoin module.
|
||||
// for all controllers in the tenant_join module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
@@ -30,14 +30,14 @@ type Routes struct {
|
||||
|
||||
// Prepare initializes the routes provider with logging configuration.
|
||||
func (r *Routes) Prepare() error {
|
||||
r.log = log.WithField("module", "routes.tenantjoin")
|
||||
r.log = log.WithField("module", "routes.tenant_join")
|
||||
r.log.Info("Initializing routes module")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Name returns the unique identifier for this routes provider.
|
||||
func (r *Routes) Name() string {
|
||||
return "tenantjoin"
|
||||
return "tenant_join"
|
||||
}
|
||||
|
||||
// Register registers all HTTP routes with the provided fiber router.
|
||||
@@ -1,4 +1,4 @@
|
||||
package tenantjoin
|
||||
package tenant_join
|
||||
|
||||
func (r *Routes) Path() string {
|
||||
return "/t/:tenantCode/v1"
|
||||
181
backend/app/http/tenant_public/content.go
Normal file
181
backend/app/http/tenant_public/content.go
Normal file
@@ -0,0 +1,181 @@
|
||||
package tenant_public
|
||||
|
||||
import (
|
||||
"quyun/v2/app/errorx"
|
||||
tenant_dto "quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
"quyun/v2/providers/jwt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// content 提供“租户维度的公开只读接口”(不要求租户成员)。
|
||||
//
|
||||
// @provider
|
||||
type content struct{}
|
||||
|
||||
func viewerUserID(ctx fiber.Ctx) int64 {
|
||||
claims, ok := ctx.Locals(consts.CtxKeyClaims).(*jwt.Claims)
|
||||
if !ok || claims == nil {
|
||||
return 0
|
||||
}
|
||||
return claims.UserID
|
||||
}
|
||||
|
||||
// list
|
||||
//
|
||||
// @Summary 公开内容列表(已发布 + public)
|
||||
// @Tags TenantPublic
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param tenantCode path string true "Tenant Code"
|
||||
// @Param filter query tenant_dto.ContentListFilter true "Filter"
|
||||
// @Success 200 {object} requests.Pager{items=tenant_dto.ContentItem}
|
||||
//
|
||||
// @Router /t/:tenantCode/v1/public/contents [get]
|
||||
// @Bind tenant local key(tenant)
|
||||
// @Bind filter query
|
||||
func (*content) list(
|
||||
ctx fiber.Ctx,
|
||||
tenant *models.Tenant,
|
||||
filter *tenant_dto.ContentListFilter,
|
||||
) (*requests.Pager, error) {
|
||||
uid := viewerUserID(ctx)
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenant.ID,
|
||||
"user_id": uid,
|
||||
}).Info("tenant_public.contents.list")
|
||||
|
||||
if filter == nil {
|
||||
filter = &tenant_dto.ContentListFilter{}
|
||||
}
|
||||
filter.Pagination.Format()
|
||||
return services.Content.ListPublicPublished(ctx, tenant.ID, uid, filter)
|
||||
}
|
||||
|
||||
// show
|
||||
//
|
||||
// @Summary 公开内容详情(已发布 + public)
|
||||
// @Tags TenantPublic
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param tenantCode path string true "Tenant Code"
|
||||
// @Param contentID path int64 true "ContentID"
|
||||
// @Success 200 {object} tenant_dto.ContentDetail
|
||||
//
|
||||
// @Router /t/:tenantCode/v1/public/contents/:contentID [get]
|
||||
// @Bind tenant local key(tenant)
|
||||
// @Bind contentID path
|
||||
func (*content) show(ctx fiber.Ctx, tenant *models.Tenant, contentID int64) (*tenant_dto.ContentDetail, error) {
|
||||
uid := viewerUserID(ctx)
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenant.ID,
|
||||
"user_id": uid,
|
||||
"content_id": contentID,
|
||||
}).Info("tenant_public.contents.show")
|
||||
|
||||
item, err := services.Content.PublicDetail(ctx, tenant.ID, uid, contentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &tenant_dto.ContentDetail{
|
||||
Content: item.Content,
|
||||
Price: item.Price,
|
||||
HasAccess: item.HasAccess,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// previewAssets
|
||||
//
|
||||
// @Summary 获取公开试看资源(preview role)
|
||||
// @Tags TenantPublic
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param tenantCode path string true "Tenant Code"
|
||||
// @Param contentID path int64 true "ContentID"
|
||||
// @Success 200 {object} tenant_dto.ContentAssetsResponse
|
||||
//
|
||||
// @Router /t/:tenantCode/v1/public/contents/:contentID/preview [get]
|
||||
// @Bind tenant local key(tenant)
|
||||
// @Bind contentID path
|
||||
func (*content) previewAssets(
|
||||
ctx fiber.Ctx,
|
||||
tenant *models.Tenant,
|
||||
contentID int64,
|
||||
) (*tenant_dto.ContentAssetsResponse, error) {
|
||||
uid := viewerUserID(ctx)
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenant.ID,
|
||||
"user_id": uid,
|
||||
"content_id": contentID,
|
||||
}).Info("tenant_public.contents.preview_assets")
|
||||
|
||||
detail, err := services.Content.PublicDetail(ctx, tenant.ID, uid, contentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
assets, err := services.Content.AssetsByRole(ctx, tenant.ID, contentID, consts.ContentAssetRolePreview)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
previewSeconds := int32(detail.Content.PreviewSeconds)
|
||||
if previewSeconds <= 0 {
|
||||
previewSeconds = consts.DefaultContentPreviewSeconds
|
||||
}
|
||||
|
||||
return &tenant_dto.ContentAssetsResponse{
|
||||
Content: detail.Content,
|
||||
Assets: assets,
|
||||
PreviewSeconds: previewSeconds,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// mainAssets
|
||||
//
|
||||
// @Summary 获取公开正片资源(main role;免费/作者/已购)
|
||||
// @Tags TenantPublic
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param tenantCode path string true "Tenant Code"
|
||||
// @Param contentID path int64 true "ContentID"
|
||||
// @Success 200 {object} tenant_dto.ContentAssetsResponse
|
||||
//
|
||||
// @Router /t/:tenantCode/v1/public/contents/:contentID/assets [get]
|
||||
// @Bind tenant local key(tenant)
|
||||
// @Bind contentID path
|
||||
func (*content) mainAssets(
|
||||
ctx fiber.Ctx,
|
||||
tenant *models.Tenant,
|
||||
contentID int64,
|
||||
) (*tenant_dto.ContentAssetsResponse, error) {
|
||||
uid := viewerUserID(ctx)
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenant.ID,
|
||||
"user_id": uid,
|
||||
"content_id": contentID,
|
||||
}).Info("tenantpublic.contents.main_assets")
|
||||
|
||||
detail, err := services.Content.PublicDetail(ctx, tenant.ID, uid, contentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !detail.HasAccess {
|
||||
return nil, errorx.ErrPermissionDenied.WithMsg("未购买或无权限访问")
|
||||
}
|
||||
|
||||
assets, err := services.Content.AssetsByRole(ctx, tenant.ID, contentID, consts.ContentAssetRoleMain)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &tenant_dto.ContentAssetsResponse{
|
||||
Content: detail.Content,
|
||||
Assets: assets,
|
||||
}, nil
|
||||
}
|
||||
37
backend/app/http/tenant_public/provider.gen.go
Executable file
37
backend/app/http/tenant_public/provider.gen.go
Executable file
@@ -0,0 +1,37 @@
|
||||
package tenant_public
|
||||
|
||||
import (
|
||||
"quyun/v2/app/middlewares"
|
||||
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/atom/opt"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func() (*content, error) {
|
||||
obj := &content{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
content *content,
|
||||
middlewares *middlewares.Middlewares,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
content: content,
|
||||
middlewares: middlewares,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupRoutes); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
72
backend/app/http/tenant_public/routes.gen.go
Normal file
72
backend/app/http/tenant_public/routes.gen.go
Normal file
@@ -0,0 +1,72 @@
|
||||
// Code generated by atomctl. DO NOT EDIT.
|
||||
|
||||
// Package tenant_public provides HTTP route definitions and registration
|
||||
// for the quyun/v2 application.
|
||||
package tenant_public
|
||||
|
||||
import (
|
||||
tenant_dto "quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/database/models"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
_ "go.ipao.vip/atom"
|
||||
_ "go.ipao.vip/atom/contracts"
|
||||
. "go.ipao.vip/atom/fen"
|
||||
)
|
||||
|
||||
// Routes implements the HttpRoute contract and provides route registration
|
||||
// for all controllers in the tenant_public module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
middlewares *middlewares.Middlewares
|
||||
// Controller instances
|
||||
content *content
|
||||
}
|
||||
|
||||
// Prepare initializes the routes provider with logging configuration.
|
||||
func (r *Routes) Prepare() error {
|
||||
r.log = log.WithField("module", "routes.tenant_public")
|
||||
r.log.Info("Initializing routes module")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Name returns the unique identifier for this routes provider.
|
||||
func (r *Routes) Name() string {
|
||||
return "tenant_public"
|
||||
}
|
||||
|
||||
// Register registers all HTTP routes with the provided fiber router.
|
||||
// Each route is registered with its corresponding controller action and parameter bindings.
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// Register routes for controller: content
|
||||
r.log.Debugf("Registering route: Get /t/:tenantCode/v1/public/contents -> content.list")
|
||||
router.Get("/t/:tenantCode/v1/public/contents"[len(r.Path()):], DataFunc2(
|
||||
r.content.list,
|
||||
Local[*models.Tenant]("tenant"),
|
||||
Query[tenant_dto.ContentListFilter]("filter"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /t/:tenantCode/v1/public/contents/:contentID -> content.show")
|
||||
router.Get("/t/:tenantCode/v1/public/contents/:contentID"[len(r.Path()):], DataFunc2(
|
||||
r.content.show,
|
||||
Local[*models.Tenant]("tenant"),
|
||||
PathParam[int64]("contentID"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /t/:tenantCode/v1/public/contents/:contentID/assets -> content.mainAssets")
|
||||
router.Get("/t/:tenantCode/v1/public/contents/:contentID/assets"[len(r.Path()):], DataFunc2(
|
||||
r.content.mainAssets,
|
||||
Local[*models.Tenant]("tenant"),
|
||||
PathParam[int64]("contentID"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Get /t/:tenantCode/v1/public/contents/:contentID/preview -> content.previewAssets")
|
||||
router.Get("/t/:tenantCode/v1/public/contents/:contentID/preview"[len(r.Path()):], DataFunc2(
|
||||
r.content.previewAssets,
|
||||
Local[*models.Tenant]("tenant"),
|
||||
PathParam[int64]("contentID"),
|
||||
))
|
||||
|
||||
r.log.Info("Successfully registered all routes")
|
||||
}
|
||||
12
backend/app/http/tenant_public/routes.manual.go
Normal file
12
backend/app/http/tenant_public/routes.manual.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package tenant_public
|
||||
|
||||
func (r *Routes) Path() string {
|
||||
return "/t/:tenantCode/v1"
|
||||
}
|
||||
|
||||
func (r *Routes) Middlewares() []any {
|
||||
return []any{
|
||||
r.middlewares.TenantResolve,
|
||||
r.middlewares.TenantOptionalAuth,
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,40 @@ func (f *Middlewares) TenantAuth(c fiber.Ctx) error {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
// TenantOptionalAuth 在 token 存在时解析并写入 claims,但允许无 token 的请求继续。
|
||||
// 用于“公开只读”类接口:可匿名访问,但若携带 token 则可以得到更准确的 has_access 等判断。
|
||||
func (f *Middlewares) TenantOptionalAuth(c fiber.Ctx) error {
|
||||
authHeader := c.Get(jwt.HttpHeader)
|
||||
if authHeader == "" {
|
||||
f.log.Debug("middlewares.tenant.optional_auth.no_token")
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
claims, err := f.jwt.Parse(authHeader)
|
||||
if err != nil {
|
||||
f.log.WithError(err).Warn("middlewares.tenant.optional_auth.invalid_token")
|
||||
switch err {
|
||||
case jwt.TokenExpired:
|
||||
return errorx.ErrTokenExpired
|
||||
case jwt.TokenMalformed, jwt.TokenNotValidYet, jwt.TokenInvalid:
|
||||
return errorx.ErrTokenInvalid
|
||||
default:
|
||||
return errorx.ErrTokenInvalid
|
||||
}
|
||||
}
|
||||
if claims.UserID == 0 {
|
||||
f.log.Warn("middlewares.tenant.optional_auth.missing_user_id")
|
||||
return errorx.ErrTokenInvalid
|
||||
}
|
||||
|
||||
f.log.WithFields(map[string]any{
|
||||
"user_id": claims.UserID,
|
||||
}).Debug("middlewares.tenant.optional_auth.ok")
|
||||
|
||||
c.Locals(consts.CtxKeyClaims, claims)
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
func (f *Middlewares) TenantRequireMember(c fiber.Ctx) error {
|
||||
tenantModel, ok := c.Locals(consts.CtxKeyTenant).(*models.Tenant)
|
||||
if !ok || tenantModel == nil {
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database"
|
||||
@@ -257,6 +258,117 @@ func (s *content) ListPublished(ctx context.Context, tenantID, userID int64, fil
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ListPublicPublished 返回“公开可见”的已发布内容列表(给游客/非成员使用)。
|
||||
// 规则:仅返回 published + visibility=public;tenant_only/private 永不通过公开接口暴露。
|
||||
func (s *content) ListPublicPublished(ctx context.Context, tenantID, viewerUserID int64, filter *dto.ContentListFilter) (*requests.Pager, error) {
|
||||
if filter == nil {
|
||||
filter = &dto.ContentListFilter{}
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"user_id": viewerUserID,
|
||||
"page": filter.Page,
|
||||
"limit": filter.Limit,
|
||||
}).Info("services.content.list_public_published")
|
||||
|
||||
tbl, query := models.ContentQuery.QueryContext(ctx)
|
||||
|
||||
conds := []gen.Condition{
|
||||
tbl.TenantID.Eq(tenantID),
|
||||
tbl.Status.Eq(consts.ContentStatusPublished),
|
||||
tbl.Visibility.Eq(consts.ContentVisibilityPublic),
|
||||
tbl.DeletedAt.IsNull(),
|
||||
}
|
||||
if filter.Keyword != nil && *filter.Keyword != "" {
|
||||
conds = append(conds, tbl.Title.Like(database.WrapLike(*filter.Keyword)))
|
||||
}
|
||||
|
||||
filter.Pagination.Format()
|
||||
items, total, err := query.Where(conds...).Order(tbl.ID.Desc()).FindByPage(int(filter.Offset()), int(filter.Limit))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
contentIDs := lo.Map(items, func(item *models.Content, _ int) int64 { return item.ID })
|
||||
priceByContent, err := s.contentPriceMapping(ctx, tenantID, contentIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
accessSet := map[int64]bool{}
|
||||
if viewerUserID > 0 {
|
||||
m, err := s.accessSet(ctx, tenantID, viewerUserID, contentIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
accessSet = m
|
||||
}
|
||||
|
||||
respItems := lo.Map(items, func(model *models.Content, _ int) *dto.ContentItem {
|
||||
price := priceByContent[model.ID]
|
||||
free := price == nil || price.PriceAmount == 0
|
||||
has := free || accessSet[model.ID] || model.UserID == viewerUserID
|
||||
return &dto.ContentItem{
|
||||
Content: model,
|
||||
Price: price,
|
||||
HasAccess: has,
|
||||
}
|
||||
})
|
||||
|
||||
return &requests.Pager{
|
||||
Pagination: filter.Pagination,
|
||||
Total: total,
|
||||
Items: respItems,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// PublicDetail 返回“公开可见”的内容详情(给游客/非成员使用)。
|
||||
// 规则:仅允许 published + visibility=public;否则统一返回 not found,避免信息泄露。
|
||||
func (s *content) PublicDetail(ctx context.Context, tenantID, viewerUserID, contentID int64) (*ContentDetailResult, error) {
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenantID,
|
||||
"user_id": viewerUserID,
|
||||
"content_id": contentID,
|
||||
}).Info("services.content.public_detail")
|
||||
|
||||
tbl, query := models.ContentQuery.QueryContext(ctx)
|
||||
model, err := query.Where(
|
||||
tbl.TenantID.Eq(tenantID),
|
||||
tbl.ID.Eq(contentID),
|
||||
tbl.DeletedAt.IsNull(),
|
||||
).First()
|
||||
if err != nil {
|
||||
return nil, errorx.ErrRecordNotFound.WithMsg("content not found")
|
||||
}
|
||||
|
||||
// Public endpoints only expose published + public contents.
|
||||
if model.Status != consts.ContentStatusPublished || model.Visibility != consts.ContentVisibilityPublic {
|
||||
return nil, errorx.ErrRecordNotFound.WithMsg("content not found")
|
||||
}
|
||||
|
||||
price, err := s.contentPrice(ctx, tenantID, contentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
free := price == nil || price.PriceAmount == 0
|
||||
|
||||
hasAccess := model.UserID == viewerUserID || free
|
||||
if !hasAccess && viewerUserID > 0 {
|
||||
ok, err := s.HasAccess(ctx, tenantID, viewerUserID, contentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hasAccess = ok
|
||||
}
|
||||
|
||||
return &ContentDetailResult{
|
||||
Content: model,
|
||||
Price: price,
|
||||
HasAccess: hasAccess,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *content) Detail(ctx context.Context, tenantID, userID, contentID int64) (*ContentDetailResult, error) {
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenantID,
|
||||
|
||||
@@ -2,11 +2,14 @@ package services
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"quyun/v2/app/commands/testx"
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
@@ -18,6 +21,7 @@ import (
|
||||
"go.ipao.vip/atom/contracts"
|
||||
"go.ipao.vip/gen/types"
|
||||
"go.uber.org/dig"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type ContentTestSuiteInjectParams struct {
|
||||
@@ -241,3 +245,247 @@ func (s *ContentTestSuite) Test_HasAccess() {
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ContentTestSuite) Test_ListPublicPublished() {
|
||||
Convey("Content.ListPublicPublished", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
now := time.Now().UTC()
|
||||
tenantID := int64(1)
|
||||
ownerID := int64(2)
|
||||
|
||||
database.Truncate(ctx, s.DB,
|
||||
models.TableNameContentAccess,
|
||||
models.TableNameContentPrice,
|
||||
models.TableNameContent,
|
||||
)
|
||||
|
||||
publicPaid := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
Title: "public_paid",
|
||||
Description: "d",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityPublic,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
PublishedAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(publicPaid.Create(ctx), ShouldBeNil)
|
||||
So((&models.ContentPrice{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
ContentID: publicPaid.ID,
|
||||
Currency: consts.CurrencyCNY,
|
||||
PriceAmount: 100,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
|
||||
publicFree := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
Title: "public_free",
|
||||
Description: "d",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityPublic,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
PublishedAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(publicFree.Create(ctx), ShouldBeNil)
|
||||
|
||||
tenantOnly := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
Title: "tenant_only",
|
||||
Description: "d",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityTenantOnly,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
PublishedAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(tenantOnly.Create(ctx), ShouldBeNil)
|
||||
|
||||
privateContent := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
Title: "private",
|
||||
Description: "d",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityPrivate,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
PublishedAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(privateContent.Create(ctx), ShouldBeNil)
|
||||
|
||||
draftPublic := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
Title: "draft_public",
|
||||
Description: "d",
|
||||
Status: consts.ContentStatusDraft,
|
||||
Visibility: consts.ContentVisibilityPublic,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(draftPublic.Create(ctx), ShouldBeNil)
|
||||
|
||||
deletedPublic := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
Title: "deleted_public",
|
||||
Description: "d",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityPublic,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
PublishedAt: now,
|
||||
DeletedAt: gorm.DeletedAt{Time: now, Valid: true},
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(deletedPublic.Create(ctx), ShouldBeNil)
|
||||
|
||||
Convey("游客仅能看到 public+published,且免费内容 has_access=true", func() {
|
||||
pager, err := Content.ListPublicPublished(ctx, tenantID, 0, &dto.ContentListFilter{Pagination: requests.Pagination{Page: 1, Limit: 20}})
|
||||
So(err, ShouldBeNil)
|
||||
So(pager.Total, ShouldEqual, 2)
|
||||
|
||||
items := pager.Items.([]*dto.ContentItem)
|
||||
So(len(items), ShouldEqual, 2)
|
||||
|
||||
got := map[int64]*dto.ContentItem{}
|
||||
for _, it := range items {
|
||||
got[it.Content.ID] = it
|
||||
}
|
||||
So(got[publicPaid.ID], ShouldNotBeNil)
|
||||
So(got[publicFree.ID], ShouldNotBeNil)
|
||||
So(got[publicPaid.ID].HasAccess, ShouldBeFalse)
|
||||
So(got[publicFree.ID].HasAccess, ShouldBeTrue)
|
||||
})
|
||||
|
||||
Convey("已登录用户若有权益则 has_access=true", func() {
|
||||
viewerID := int64(99)
|
||||
access := &models.ContentAccess{
|
||||
TenantID: tenantID,
|
||||
UserID: viewerID,
|
||||
ContentID: publicPaid.ID,
|
||||
OrderID: 123,
|
||||
Status: consts.ContentAccessStatusActive,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(access.Create(ctx), ShouldBeNil)
|
||||
|
||||
pager, err := Content.ListPublicPublished(ctx, tenantID, viewerID, &dto.ContentListFilter{Pagination: requests.Pagination{Page: 1, Limit: 20}})
|
||||
So(err, ShouldBeNil)
|
||||
So(pager.Total, ShouldEqual, 2)
|
||||
|
||||
items := pager.Items.([]*dto.ContentItem)
|
||||
got := map[int64]*dto.ContentItem{}
|
||||
for _, it := range items {
|
||||
got[it.Content.ID] = it
|
||||
}
|
||||
So(got[publicPaid.ID].HasAccess, ShouldBeTrue)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ContentTestSuite) Test_PublicDetail() {
|
||||
Convey("Content.PublicDetail", s.T(), func() {
|
||||
ctx := s.T().Context()
|
||||
now := time.Now().UTC()
|
||||
tenantID := int64(1)
|
||||
ownerID := int64(2)
|
||||
|
||||
database.Truncate(ctx, s.DB,
|
||||
models.TableNameContentAccess,
|
||||
models.TableNameContentPrice,
|
||||
models.TableNameContent,
|
||||
)
|
||||
|
||||
publicPaid := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
Title: "public_paid",
|
||||
Description: "d",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityPublic,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
PublishedAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(publicPaid.Create(ctx), ShouldBeNil)
|
||||
So((&models.ContentPrice{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
ContentID: publicPaid.ID,
|
||||
Currency: consts.CurrencyCNY,
|
||||
PriceAmount: 100,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}).Create(ctx), ShouldBeNil)
|
||||
|
||||
tenantOnly := &models.Content{
|
||||
TenantID: tenantID,
|
||||
UserID: ownerID,
|
||||
Title: "tenant_only",
|
||||
Description: "d",
|
||||
Status: consts.ContentStatusPublished,
|
||||
Visibility: consts.ContentVisibilityTenantOnly,
|
||||
PreviewSeconds: consts.DefaultContentPreviewSeconds,
|
||||
PreviewDownloadable: false,
|
||||
PublishedAt: now,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(tenantOnly.Create(ctx), ShouldBeNil)
|
||||
|
||||
Convey("游客访问 public+paid:可见但无正片权限", func() {
|
||||
out, err := Content.PublicDetail(ctx, tenantID, 0, publicPaid.ID)
|
||||
So(err, ShouldBeNil)
|
||||
So(out, ShouldNotBeNil)
|
||||
So(out.HasAccess, ShouldBeFalse)
|
||||
})
|
||||
|
||||
Convey("tenant_only 在 public detail 下应表现为 not found", func() {
|
||||
_, err := Content.PublicDetail(ctx, tenantID, 0, tenantOnly.ID)
|
||||
So(err, ShouldNotBeNil)
|
||||
var appErr *errorx.AppError
|
||||
So(errors.As(err, &appErr), ShouldBeTrue)
|
||||
So(appErr.Code, ShouldEqual, errorx.ErrRecordNotFound.Code)
|
||||
})
|
||||
|
||||
Convey("有权益的已登录用户访问 public+paid:has_access=true", func() {
|
||||
viewerID := int64(99)
|
||||
access := &models.ContentAccess{
|
||||
TenantID: tenantID,
|
||||
UserID: viewerID,
|
||||
ContentID: publicPaid.ID,
|
||||
OrderID: 123,
|
||||
Status: consts.ContentAccessStatusActive,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
So(access.Create(ctx), ShouldBeNil)
|
||||
|
||||
out, err := Content.PublicDetail(ctx, tenantID, viewerID, publicPaid.ID)
|
||||
So(err, ShouldBeNil)
|
||||
So(out.HasAccess, ShouldBeTrue)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/tenant/dto"
|
||||
tenantjoindto "quyun/v2/app/http/tenantjoin/dto"
|
||||
tenant_join_dto "quyun/v2/app/http/tenant_join/dto"
|
||||
"quyun/v2/app/requests"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
@@ -297,7 +297,7 @@ func (t *tenant) JoinByInvite(ctx context.Context, tenantID, userID int64, invit
|
||||
}
|
||||
|
||||
// CreateJoinRequest 用户提交加入租户申请(无邀请码场景)。
|
||||
func (t *tenant) CreateJoinRequest(ctx context.Context, tenantID, userID int64, form *tenantjoindto.JoinRequestCreateForm) (*models.TenantJoinRequest, error) {
|
||||
func (t *tenant) CreateJoinRequest(ctx context.Context, tenantID, userID int64, form *tenant_join_dto.JoinRequestCreateForm) (*models.TenantJoinRequest, error) {
|
||||
if tenantID <= 0 || userID <= 0 {
|
||||
return nil, errorx.ErrInvalidParameter.WithMsg("invalid tenant_id/user_id")
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
"quyun/v2/app/commands/testx"
|
||||
"quyun/v2/app/errorx"
|
||||
tenantdto "quyun/v2/app/http/tenant/dto"
|
||||
tenantjoindto "quyun/v2/app/http/tenantjoin/dto"
|
||||
tenant_join_dto "quyun/v2/app/http/tenant_join/dto"
|
||||
"quyun/v2/database"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/pkg/consts"
|
||||
@@ -262,7 +262,7 @@ func (s *TenantJoinTestSuite) Test_CreateJoinRequest() {
|
||||
Status: consts.UserStatusVerified,
|
||||
}).Error, ShouldBeNil)
|
||||
|
||||
_, err := Tenant.CreateJoinRequest(ctx, tenantID, userID, &tenantjoindto.JoinRequestCreateForm{Reason: "x"})
|
||||
_, err := Tenant.CreateJoinRequest(ctx, tenantID, userID, &tenant_join_dto.JoinRequestCreateForm{Reason: "x"})
|
||||
So(err, ShouldNotBeNil)
|
||||
|
||||
var appErr *errorx.AppError
|
||||
@@ -273,11 +273,11 @@ func (s *TenantJoinTestSuite) Test_CreateJoinRequest() {
|
||||
Convey("重复提交应返回同一个 pending 申请(幂等)", func() {
|
||||
s.truncateAll(ctx)
|
||||
|
||||
out1, err := Tenant.CreateJoinRequest(ctx, tenantID, userID, &tenantjoindto.JoinRequestCreateForm{Reason: "a"})
|
||||
out1, err := Tenant.CreateJoinRequest(ctx, tenantID, userID, &tenant_join_dto.JoinRequestCreateForm{Reason: "a"})
|
||||
So(err, ShouldBeNil)
|
||||
So(out1, ShouldNotBeNil)
|
||||
|
||||
out2, err := Tenant.CreateJoinRequest(ctx, tenantID, userID, &tenantjoindto.JoinRequestCreateForm{Reason: "b"})
|
||||
out2, err := Tenant.CreateJoinRequest(ctx, tenantID, userID, &tenant_join_dto.JoinRequestCreateForm{Reason: "b"})
|
||||
So(err, ShouldBeNil)
|
||||
So(out2, ShouldNotBeNil)
|
||||
So(out2.ID, ShouldEqual, out1.ID)
|
||||
|
||||
Reference in New Issue
Block a user