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:
@@ -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,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user