tenant: add invites and join requests
This commit is contained in:
13
backend/app/http/tenantjoin/dto/join.go
Normal file
13
backend/app/http/tenantjoin/dto/join.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package dto
|
||||
|
||||
// JoinByInviteForm 用户通过邀请码加入租户的请求参数。
|
||||
type JoinByInviteForm struct {
|
||||
// InviteCode 邀请码:由租户管理员生成;用户提交后加入对应租户。
|
||||
InviteCode string `json:"invite_code"`
|
||||
}
|
||||
|
||||
// JoinRequestCreateForm 用户提交加入租户申请的请求参数(无邀请码场景)。
|
||||
type JoinRequestCreateForm struct {
|
||||
// Reason 申请原因(可选):用于向租户管理员说明申请加入的目的。
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
87
backend/app/http/tenantjoin/join.go
Normal file
87
backend/app/http/tenantjoin/join.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package tenantjoin
|
||||
|
||||
import (
|
||||
"quyun/v2/app/errorx"
|
||||
"quyun/v2/app/http/tenantjoin/dto"
|
||||
"quyun/v2/app/services"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/providers/jwt"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// join 提供“非成员加入租户”的相关接口(邀请码加入 / 申请加入)。
|
||||
//
|
||||
// @provider
|
||||
type join struct{}
|
||||
|
||||
// joinByInvite
|
||||
//
|
||||
// @Summary 通过邀请码加入租户
|
||||
// @Tags TenantJoin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param tenantCode path string true "Tenant Code"
|
||||
// @Param form body dto.JoinByInviteForm true "Form"
|
||||
// @Success 200 {object} models.TenantUser
|
||||
//
|
||||
// @Router /t/:tenantCode/v1/join/invite [post]
|
||||
// @Bind tenant local key(tenant)
|
||||
// @Bind claims local key(claims)
|
||||
// @Bind form body
|
||||
func (*join) joinByInvite(
|
||||
ctx fiber.Ctx,
|
||||
tenant *models.Tenant,
|
||||
claims *jwt.Claims,
|
||||
form *dto.JoinByInviteForm,
|
||||
) (*models.TenantUser, error) {
|
||||
if tenant == nil || claims == nil {
|
||||
return nil, errorx.ErrInternalError.WithMsg("context missing")
|
||||
}
|
||||
if form == nil {
|
||||
return nil, errorx.ErrInvalidParameter
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenant.ID,
|
||||
"user_id": claims.UserID,
|
||||
}).Info("tenantjoin.join_by_invite")
|
||||
|
||||
return services.Tenant.JoinByInvite(ctx.Context(), tenant.ID, claims.UserID, form.InviteCode)
|
||||
}
|
||||
|
||||
// createJoinRequest
|
||||
//
|
||||
// @Summary 提交加入租户申请
|
||||
// @Tags TenantJoin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param tenantCode path string true "Tenant Code"
|
||||
// @Param form body dto.JoinRequestCreateForm true "Form"
|
||||
// @Success 200 {object} models.TenantJoinRequest
|
||||
//
|
||||
// @Router /t/:tenantCode/v1/join/request [post]
|
||||
// @Bind tenant local key(tenant)
|
||||
// @Bind claims local key(claims)
|
||||
// @Bind form body
|
||||
func (*join) createJoinRequest(
|
||||
ctx fiber.Ctx,
|
||||
tenant *models.Tenant,
|
||||
claims *jwt.Claims,
|
||||
form *dto.JoinRequestCreateForm,
|
||||
) (*models.TenantJoinRequest, error) {
|
||||
if tenant == nil || claims == nil {
|
||||
return nil, errorx.ErrInternalError.WithMsg("context missing")
|
||||
}
|
||||
if form == nil {
|
||||
return nil, errorx.ErrInvalidParameter
|
||||
}
|
||||
|
||||
log.WithFields(log.Fields{
|
||||
"tenant_id": tenant.ID,
|
||||
"user_id": claims.UserID,
|
||||
}).Info("tenantjoin.create_join_request")
|
||||
|
||||
return services.Tenant.CreateJoinRequest(ctx.Context(), tenant.ID, claims.UserID, form)
|
||||
}
|
||||
37
backend/app/http/tenantjoin/provider.gen.go
Executable file
37
backend/app/http/tenantjoin/provider.gen.go
Executable file
@@ -0,0 +1,37 @@
|
||||
package tenantjoin
|
||||
|
||||
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() (*join, error) {
|
||||
obj := &join{}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
join *join,
|
||||
middlewares *middlewares.Middlewares,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
join: join,
|
||||
middlewares: middlewares,
|
||||
}
|
||||
if err := obj.Prepare(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}, atom.GroupRoutes); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
63
backend/app/http/tenantjoin/routes.gen.go
Normal file
63
backend/app/http/tenantjoin/routes.gen.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// Code generated by atomctl. DO NOT EDIT.
|
||||
|
||||
// Package tenantjoin provides HTTP route definitions and registration
|
||||
// for the quyun/v2 application.
|
||||
package tenantjoin
|
||||
|
||||
import (
|
||||
"quyun/v2/app/http/tenantjoin/dto"
|
||||
"quyun/v2/app/middlewares"
|
||||
"quyun/v2/database/models"
|
||||
"quyun/v2/providers/jwt"
|
||||
|
||||
"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 tenantjoin module.
|
||||
//
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
middlewares *middlewares.Middlewares
|
||||
// Controller instances
|
||||
join *join
|
||||
}
|
||||
|
||||
// Prepare initializes the routes provider with logging configuration.
|
||||
func (r *Routes) Prepare() error {
|
||||
r.log = log.WithField("module", "routes.tenantjoin")
|
||||
r.log.Info("Initializing routes module")
|
||||
return nil
|
||||
}
|
||||
|
||||
// Name returns the unique identifier for this routes provider.
|
||||
func (r *Routes) Name() string {
|
||||
return "tenantjoin"
|
||||
}
|
||||
|
||||
// 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: join
|
||||
r.log.Debugf("Registering route: Post /t/:tenantCode/v1/join/invite -> join.joinByInvite")
|
||||
router.Post("/t/:tenantCode/v1/join/invite"[len(r.Path()):], DataFunc3(
|
||||
r.join.joinByInvite,
|
||||
Local[*models.Tenant]("tenant"),
|
||||
Local[*jwt.Claims]("claims"),
|
||||
Body[dto.JoinByInviteForm]("form"),
|
||||
))
|
||||
r.log.Debugf("Registering route: Post /t/:tenantCode/v1/join/request -> join.createJoinRequest")
|
||||
router.Post("/t/:tenantCode/v1/join/request"[len(r.Path()):], DataFunc3(
|
||||
r.join.createJoinRequest,
|
||||
Local[*models.Tenant]("tenant"),
|
||||
Local[*jwt.Claims]("claims"),
|
||||
Body[dto.JoinRequestCreateForm]("form"),
|
||||
))
|
||||
|
||||
r.log.Info("Successfully registered all routes")
|
||||
}
|
||||
12
backend/app/http/tenantjoin/routes.manual.go
Normal file
12
backend/app/http/tenantjoin/routes.manual.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package tenantjoin
|
||||
|
||||
func (r *Routes) Path() string {
|
||||
return "/t/:tenantCode/v1"
|
||||
}
|
||||
|
||||
func (r *Routes) Middlewares() []any {
|
||||
return []any{
|
||||
r.middlewares.TenantResolve,
|
||||
r.middlewares.TenantAuth,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user