feat: add manage apis for ApplicationPassword

pull/351/head
textworld 1 year ago
parent 56157ba32a
commit b77bcce0c6

@ -0,0 +1,51 @@
package wp
import (
"github.com/gin-gonic/gin"
"github.com/go-sonic/sonic/model/dto/wp"
"github.com/go-sonic/sonic/model/entity"
"github.com/go-sonic/sonic/model/param"
"github.com/go-sonic/sonic/service"
)
type CategoryHandler struct {
CategoryService service.CategoryService
}
func NewCategoryHandler(categoryService service.CategoryService) *CategoryHandler {
return &CategoryHandler{
CategoryService: categoryService,
}
}
func (c *CategoryHandler) List(ctx *gin.Context) (interface{}, error) {
sort := &param.Sort{
Fields: []string{"name,desc"},
}
categoryEntities, err := c.CategoryService.ListAll(ctx, sort)
if err != nil {
return nil, err
}
categoryDTOList := make([]*wp.CategoryDTO, len(categoryEntities))
for _, categoryEntity := range categoryEntities {
categoryDTOList = append(categoryDTOList, convertToCategoryDTO(categoryEntity))
}
return categoryDTOList, nil
}
func convertToCategoryDTO(categoryEntity *entity.Category) *wp.CategoryDTO {
categoryDTO := &wp.CategoryDTO{
ID: categoryEntity.ID,
Count: 0,
Description: categoryEntity.Description,
Link: "",
Name: categoryEntity.Name,
Slug: categoryEntity.Slug,
Taxonomy: "",
Parent: categoryEntity.ParentID,
Meta: nil,
}
return categoryDTO
}

@ -0,0 +1,11 @@
package wp
import "github.com/go-sonic/sonic/injection"
func init() {
injection.Provide(
NewPostHandler,
NewUserHandler,
NewCategoryHandler,
)
}

@ -0,0 +1,128 @@
package wp
import (
"encoding/json"
"github.com/gin-gonic/gin"
"github.com/go-sonic/sonic/consts"
"github.com/go-sonic/sonic/log"
"github.com/go-sonic/sonic/model/dto/wp"
"github.com/go-sonic/sonic/model/entity"
"github.com/go-sonic/sonic/model/param"
"github.com/go-sonic/sonic/service"
"github.com/go-sonic/sonic/util"
"strings"
"time"
)
type PostHandler struct {
PostService service.PostService
}
func NewPostHandler(postService service.PostService) *PostHandler {
return &PostHandler{
PostService: postService,
}
}
func (handler *PostHandler) Create(ctx *gin.Context) (interface{}, error) {
var wpPost param.WpPost
err := ctx.ShouldBindJSON(&wpPost)
if err != nil {
return nil, util.WrapJsonBindErr(err)
}
bytes, err := json.Marshal(wpPost)
if err != nil {
return nil, err
}
log.CtxInfo(ctx, "wpPost: "+string(bytes))
postParam := convertToPostParam(&wpPost)
create, err := handler.PostService.Create(ctx, postParam)
if err != nil {
return nil, err
}
return convertToWpPost(create), nil
}
func convertToPostParam(wpPost *param.WpPost) *param.Post {
var paramPostStatus = consts.PostStatusPublished
if strings.ToLower(wpPost.Content) == "draft" {
paramPostStatus = consts.PostStatusDraft
}
createTime := time.Now().Unix()
return &param.Post{
Title: wpPost.Title,
Status: paramPostStatus,
Slug: wpPost.Slug,
EditorType: nil,
OriginalContent: wpPost.Content,
Summary: "",
Thumbnail: "",
DisallowComment: false,
Password: wpPost.Password,
Template: "",
TopPriority: 0,
CreateTime: &createTime,
MetaKeywords: "",
MetaDescription: "",
TagIDs: make([]int32, 0),
CategoryIDs: make([]int32, 0),
MetaParam: nil,
Content: wpPost.Content,
EditTime: nil,
UpdateTime: nil,
}
}
func convertToWpPost(postEntity *entity.Post) *wp.PostDTO {
timeFormat := time.RFC3339
var wpStatus = "publish"
var wpCommentStatus = "open"
var wpContent = make(map[string]interface{})
if postEntity.Status == consts.PostStatusDraft {
wpStatus = "draft"
}
if postEntity.DisallowComment {
wpCommentStatus = "close"
}
wpContent["rendered"] = postEntity.OriginalContent
wpContent["protected"] = false
var postDTO = &wp.PostDTO{
Date: postEntity.CreateTime.Format(timeFormat),
DateGmt: postEntity.CreateTime.UTC().Format(timeFormat),
Guid: nil,
Id: postEntity.ID,
Link: "",
Modified: postEntity.UpdateTime.Format(timeFormat),
ModifiedGmt: postEntity.UpdateTime.UTC().Format(timeFormat),
Slug: "",
Status: wpStatus,
Type: "post",
Password: "standard",
PermalinkTemplate: "",
GeneratedSlug: "",
Title: "",
Content: wpContent,
Author: 0,
Excerpt: nil,
FeaturedMedia: 0,
CommentStatus: wpCommentStatus,
PingStatus: "open",
Format: "standard",
Meta: nil,
Sticky: false,
Template: "",
Categories: make([]int32, 0),
Tags: make([]int32, 0),
}
return postDTO
}

@ -0,0 +1,31 @@
package wp
import (
"github.com/gin-gonic/gin"
"github.com/go-sonic/sonic/model/dto"
"github.com/go-sonic/sonic/service"
)
type UserHandler struct {
UserService service.UserService
}
func NewUserHandler(userService service.UserService) *UserHandler {
return &UserHandler{
UserService: userService,
}
}
func (u *UserHandler) List(ctx *gin.Context) (interface{}, error) {
allUser, err := u.UserService.GetAllUser(ctx)
if err != nil {
return nil, err
}
userDTOList := make([]*dto.User, len(allUser))
for _, user := range allUser {
userDTO := u.UserService.ConvertToDTO(ctx, user)
userDTOList = append(userDTOList, userDTO)
}
return userDTOList, nil
}

@ -0,0 +1,96 @@
package middleware
import (
"encoding/base64"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-sonic/sonic/consts"
"github.com/go-sonic/sonic/model/dto"
"github.com/go-sonic/sonic/service"
"net/http"
"regexp"
"strings"
)
type ApplicationPasswordMiddleware struct {
PasswordService service.ApplicationPasswordService
UserService service.UserService
}
func NewApplicationPasswordMiddleware(passwordService service.ApplicationPasswordService, userService service.UserService) *ApplicationPasswordMiddleware {
m := &ApplicationPasswordMiddleware{
PasswordService: passwordService,
UserService: userService,
}
return m
}
func (a *ApplicationPasswordMiddleware) Get() error {
return nil
}
func (a *ApplicationPasswordMiddleware) GetWrapHandler() gin.HandlerFunc {
return func(ctx *gin.Context) {
header := ctx.GetHeader("Authorization")
if len(header) == 0 {
abortUnauthorized(ctx)
return
}
match := verifyHeader(header)
if !match {
abortUnauthorized(ctx)
return
}
bytes, err := base64.StdEncoding.DecodeString(header[6:])
if err != nil {
abortUnauthorized(ctx)
return
}
userPass := string(bytes)
if !strings.Contains(userPass, ":") {
abortUnauthorized(ctx)
return
}
splits := strings.SplitN(userPass, ":", 2)
userEntity, err := a.UserService.GetByUsername(ctx, splits[0])
if err != nil {
abortUnauthorized(ctx)
return
}
pwdEntity, err := a.PasswordService.Verify(ctx, userEntity.ID, splits[1])
if err != nil || pwdEntity == nil {
abortUnauthorized(ctx)
return
}
err = a.PasswordService.Update(ctx, *pwdEntity.ID, ctx.ClientIP())
if err != nil {
ctx.AbortWithStatusJSON(http.StatusInternalServerError, &dto.BaseDTO{
Status: http.StatusInternalServerError,
Message: fmt.Sprintf("Update application password entity error, err=%s", err),
})
return
}
ctx.Set(consts.AuthorizedUser, userEntity)
}
}
func abortUnauthorized(ctx *gin.Context) {
ctx.AbortWithStatusJSON(http.StatusUnauthorized, &dto.BaseDTO{
Status: http.StatusUnauthorized,
Message: "Unauthorized",
})
}
func verifyHeader(header string) bool {
compile, err := regexp.Compile("^Basic [a-z\\d/+]*={0,2}")
return err != nil || compile.MatchString(header)
}

@ -44,6 +44,13 @@ func (s *Server) RegisterRouters() {
StaticFS(consts.SonicUploadDir, gin.Dir(s.Config.Sonic.UploadDir, false))
staticRouter.StaticFS("/themes/", gin.Dir(s.Config.Sonic.ThemeDir, false))
}
{
wpCompatibleRouter := router.Group("/wp-json/wp/v2")
wpCompatibleRouter.Use(s.ApplicationPasswordMiddleware.GetWrapHandler())
wpCompatibleRouter.POST("/posts", s.wrapHandler(s.WpPostHandler.Create))
wpCompatibleRouter.GET("/users", s.wrapHandler(s.WpUserHandler.List))
wpCompatibleRouter.GET("/categories", s.wrapHandler(s.WpCategoryHandler.List))
}
{
adminAPIRouter := router.Group("/api/admin")
adminAPIRouter.Use(s.LogMiddleware.LoggerWithConfig(middleware.GinLoggerConfig{}), s.RecoveryMiddleware.RecoveryWithLogger(), s.InstallRedirectMiddleware.InstallRedirect())
@ -353,6 +360,7 @@ func (s *Server) RegisterRouters() {
contentAPIRouter.GET("/options/comment", s.wrapHandler(s.ContentAPIOptionHandler.Comment))
}
}
}

@ -3,6 +3,7 @@ package handler
import (
"context"
"fmt"
"github.com/go-sonic/sonic/handler/content/wp"
"net/http"
"os"
"strconv"
@ -25,115 +26,123 @@ import (
)
type Server struct {
logger *zap.Logger
Config *config.Config
HTTPServer *http.Server
Router *gin.Engine
Template *template.Template
AuthMiddleware *middleware.AuthMiddleware
LogMiddleware *middleware.GinLoggerMiddleware
RecoveryMiddleware *middleware.RecoveryMiddleware
InstallRedirectMiddleware *middleware.InstallRedirectMiddleware
OptionService service.OptionService
ThemeService service.ThemeService
SheetService service.SheetService
AdminHandler *admin.AdminHandler
AttachmentHandler *admin.AttachmentHandler
BackupHandler *admin.BackupHandler
CategoryHandler *admin.CategoryHandler
InstallHandler *admin.InstallHandler
JournalHandler *admin.JournalHandler
JournalCommentHandler *admin.JournalCommentHandler
LinkHandler *admin.LinkHandler
LogHandler *admin.LogHandler
MenuHandler *admin.MenuHandler
OptionHandler *admin.OptionHandler
PhotoHandler *admin.PhotoHandler
PostHandler *admin.PostHandler
PostCommentHandler *admin.PostCommentHandler
SheetHandler *admin.SheetHandler
SheetCommentHandler *admin.SheetCommentHandler
StatisticHandler *admin.StatisticHandler
TagHandler *admin.TagHandler
ThemeHandler *admin.ThemeHandler
UserHandler *admin.UserHandler
EmailHandler *admin.EmailHandler
IndexHandler *content.IndexHandler
FeedHandler *content.FeedHandler
ArchiveHandler *content.ArchiveHandler
ViewHandler *content.ViewHandler
ContentCategoryHandler *content.CategoryHandler
ContentSheetHandler *content.SheetHandler
ContentTagHandler *content.TagHandler
ContentLinkHandler *content.LinkHandler
ContentPhotoHandler *content.PhotoHandler
ContentJournalHandler *content.JournalHandler
ContentSearchHandler *content.SearchHandler
ContentAPIArchiveHandler *api.ArchiveHandler
ContentAPICategoryHandler *api.CategoryHandler
ContentAPIJournalHandler *api.JournalHandler
ContentAPILinkHandler *api.LinkHandler
ContentAPIPostHandler *api.PostHandler
ContentAPISheetHandler *api.SheetHandler
ContentAPIOptionHandler *api.OptionHandler
ContentAPIPhotoHandler *api.PhotoHandler
ApplicationPasswordHandler *admin.ApplicationPasswordHandler
logger *zap.Logger
Config *config.Config
HTTPServer *http.Server
Router *gin.Engine
Template *template.Template
AuthMiddleware *middleware.AuthMiddleware
LogMiddleware *middleware.GinLoggerMiddleware
RecoveryMiddleware *middleware.RecoveryMiddleware
InstallRedirectMiddleware *middleware.InstallRedirectMiddleware
ApplicationPasswordMiddleware *middleware.ApplicationPasswordMiddleware
OptionService service.OptionService
ThemeService service.ThemeService
SheetService service.SheetService
AdminHandler *admin.AdminHandler
AttachmentHandler *admin.AttachmentHandler
BackupHandler *admin.BackupHandler
CategoryHandler *admin.CategoryHandler
InstallHandler *admin.InstallHandler
JournalHandler *admin.JournalHandler
JournalCommentHandler *admin.JournalCommentHandler
LinkHandler *admin.LinkHandler
LogHandler *admin.LogHandler
MenuHandler *admin.MenuHandler
OptionHandler *admin.OptionHandler
PhotoHandler *admin.PhotoHandler
PostHandler *admin.PostHandler
PostCommentHandler *admin.PostCommentHandler
SheetHandler *admin.SheetHandler
SheetCommentHandler *admin.SheetCommentHandler
StatisticHandler *admin.StatisticHandler
TagHandler *admin.TagHandler
ThemeHandler *admin.ThemeHandler
UserHandler *admin.UserHandler
EmailHandler *admin.EmailHandler
ApplicationPasswordHandler *admin.ApplicationPasswordHandler
IndexHandler *content.IndexHandler
FeedHandler *content.FeedHandler
ArchiveHandler *content.ArchiveHandler
ViewHandler *content.ViewHandler
ContentCategoryHandler *content.CategoryHandler
ContentSheetHandler *content.SheetHandler
ContentTagHandler *content.TagHandler
ContentLinkHandler *content.LinkHandler
ContentPhotoHandler *content.PhotoHandler
ContentJournalHandler *content.JournalHandler
ContentSearchHandler *content.SearchHandler
ContentAPIArchiveHandler *api.ArchiveHandler
ContentAPICategoryHandler *api.CategoryHandler
ContentAPIJournalHandler *api.JournalHandler
ContentAPILinkHandler *api.LinkHandler
ContentAPIPostHandler *api.PostHandler
ContentAPISheetHandler *api.SheetHandler
ContentAPIOptionHandler *api.OptionHandler
ContentAPIPhotoHandler *api.PhotoHandler
WpPostHandler *wp.PostHandler
WpUserHandler *wp.UserHandler
WpCategoryHandler *wp.CategoryHandler
}
type ServerParams struct {
dig.In
Config *config.Config
Logger *zap.Logger
Event event.Bus
Template *template.Template
AuthMiddleware *middleware.AuthMiddleware
LogMiddleware *middleware.GinLoggerMiddleware
RecoveryMiddleware *middleware.RecoveryMiddleware
InstallRedirectMiddleware *middleware.InstallRedirectMiddleware
OptionService service.OptionService
ThemeService service.ThemeService
SheetService service.SheetService
AdminHandler *admin.AdminHandler
AttachmentHandler *admin.AttachmentHandler
BackupHandler *admin.BackupHandler
CategoryHandler *admin.CategoryHandler
InstallHandler *admin.InstallHandler
JournalHandler *admin.JournalHandler
JournalCommentHandler *admin.JournalCommentHandler
LinkHandler *admin.LinkHandler
LogHandler *admin.LogHandler
MenuHandler *admin.MenuHandler
OptionHandler *admin.OptionHandler
PhotoHandler *admin.PhotoHandler
PostHandler *admin.PostHandler
PostCommentHandler *admin.PostCommentHandler
SheetHandler *admin.SheetHandler
SheetCommentHandler *admin.SheetCommentHandler
StatisticHandler *admin.StatisticHandler
TagHandler *admin.TagHandler
ThemeHandler *admin.ThemeHandler
UserHandler *admin.UserHandler
EmailHandler *admin.EmailHandler
ApplicationPasswordHandler *admin.ApplicationPasswordHandler
IndexHandler *content.IndexHandler
FeedHandler *content.FeedHandler
ArchiveHandler *content.ArchiveHandler
ViewHandler *content.ViewHandler
ContentCategoryHandler *content.CategoryHandler
ContentSheetHandler *content.SheetHandler
ContentTagHandler *content.TagHandler
ContentLinkHandler *content.LinkHandler
ContentPhotoHandler *content.PhotoHandler
ContentJournalHandler *content.JournalHandler
ContentSearchHandler *content.SearchHandler
ContentAPIArchiveHandler *api.ArchiveHandler
ContentAPICategoryHandler *api.CategoryHandler
ContentAPIJournalHandler *api.JournalHandler
ContentAPILinkHandler *api.LinkHandler
ContentAPIPostHandler *api.PostHandler
ContentAPISheetHandler *api.SheetHandler
ContentAPIOptionHandler *api.OptionHandler
ContentAPIPhotoHandler *api.PhotoHandler
Config *config.Config
Logger *zap.Logger
Event event.Bus
Template *template.Template
AuthMiddleware *middleware.AuthMiddleware
LogMiddleware *middleware.GinLoggerMiddleware
RecoveryMiddleware *middleware.RecoveryMiddleware
InstallRedirectMiddleware *middleware.InstallRedirectMiddleware
ApplicationPasswordMiddleware *middleware.ApplicationPasswordMiddleware
OptionService service.OptionService
ThemeService service.ThemeService
SheetService service.SheetService
AdminHandler *admin.AdminHandler
AttachmentHandler *admin.AttachmentHandler
BackupHandler *admin.BackupHandler
CategoryHandler *admin.CategoryHandler
InstallHandler *admin.InstallHandler
JournalHandler *admin.JournalHandler
JournalCommentHandler *admin.JournalCommentHandler
LinkHandler *admin.LinkHandler
LogHandler *admin.LogHandler
MenuHandler *admin.MenuHandler
OptionHandler *admin.OptionHandler
PhotoHandler *admin.PhotoHandler
PostHandler *admin.PostHandler
PostCommentHandler *admin.PostCommentHandler
SheetHandler *admin.SheetHandler
SheetCommentHandler *admin.SheetCommentHandler
StatisticHandler *admin.StatisticHandler
TagHandler *admin.TagHandler
ThemeHandler *admin.ThemeHandler
UserHandler *admin.UserHandler
EmailHandler *admin.EmailHandler
ApplicationPasswordHandler *admin.ApplicationPasswordHandler
IndexHandler *content.IndexHandler
FeedHandler *content.FeedHandler
ArchiveHandler *content.ArchiveHandler
ViewHandler *content.ViewHandler
ContentCategoryHandler *content.CategoryHandler
ContentSheetHandler *content.SheetHandler
ContentTagHandler *content.TagHandler
ContentLinkHandler *content.LinkHandler
ContentPhotoHandler *content.PhotoHandler
ContentJournalHandler *content.JournalHandler
ContentSearchHandler *content.SearchHandler
ContentAPIArchiveHandler *api.ArchiveHandler
ContentAPICategoryHandler *api.CategoryHandler
ContentAPIJournalHandler *api.JournalHandler
ContentAPILinkHandler *api.LinkHandler
ContentAPIPostHandler *api.PostHandler
ContentAPISheetHandler *api.SheetHandler
ContentAPIOptionHandler *api.OptionHandler
ContentAPIPhotoHandler *api.PhotoHandler
WpPostHandler *wp.PostHandler
WpUserHandler *wp.UserHandler
WpCategoryHandler *wp.CategoryHandler
}
func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server {
@ -147,59 +156,63 @@ func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server {
}
s := &Server{
logger: param.Logger,
Config: param.Config,
HTTPServer: httpServer,
Router: router,
Template: param.Template,
AuthMiddleware: param.AuthMiddleware,
LogMiddleware: param.LogMiddleware,
RecoveryMiddleware: param.RecoveryMiddleware,
InstallRedirectMiddleware: param.InstallRedirectMiddleware,
AdminHandler: param.AdminHandler,
AttachmentHandler: param.AttachmentHandler,
BackupHandler: param.BackupHandler,
CategoryHandler: param.CategoryHandler,
InstallHandler: param.InstallHandler,
JournalHandler: param.JournalHandler,
JournalCommentHandler: param.JournalCommentHandler,
LinkHandler: param.LinkHandler,
LogHandler: param.LogHandler,
MenuHandler: param.MenuHandler,
OptionHandler: param.OptionHandler,
PhotoHandler: param.PhotoHandler,
PostHandler: param.PostHandler,
PostCommentHandler: param.PostCommentHandler,
SheetHandler: param.SheetHandler,
SheetCommentHandler: param.SheetCommentHandler,
StatisticHandler: param.StatisticHandler,
TagHandler: param.TagHandler,
ThemeHandler: param.ThemeHandler,
UserHandler: param.UserHandler,
EmailHandler: param.EmailHandler,
OptionService: param.OptionService,
ThemeService: param.ThemeService,
SheetService: param.SheetService,
IndexHandler: param.IndexHandler,
FeedHandler: param.FeedHandler,
ArchiveHandler: param.ArchiveHandler,
ViewHandler: param.ViewHandler,
ContentCategoryHandler: param.ContentCategoryHandler,
ContentSheetHandler: param.ContentSheetHandler,
ContentTagHandler: param.ContentTagHandler,
ContentLinkHandler: param.ContentLinkHandler,
ContentPhotoHandler: param.ContentPhotoHandler,
ContentJournalHandler: param.ContentJournalHandler,
ContentAPIArchiveHandler: param.ContentAPIArchiveHandler,
ContentAPICategoryHandler: param.ContentAPICategoryHandler,
ContentAPIJournalHandler: param.ContentAPIJournalHandler,
ContentAPILinkHandler: param.ContentAPILinkHandler,
ContentAPIPostHandler: param.ContentAPIPostHandler,
ContentAPISheetHandler: param.ContentAPISheetHandler,
ContentAPIOptionHandler: param.ContentAPIOptionHandler,
ContentSearchHandler: param.ContentSearchHandler,
ContentAPIPhotoHandler: param.ContentAPIPhotoHandler,
ApplicationPasswordHandler: param.ApplicationPasswordHandler,
logger: param.Logger,
Config: param.Config,
HTTPServer: httpServer,
Router: router,
Template: param.Template,
AuthMiddleware: param.AuthMiddleware,
LogMiddleware: param.LogMiddleware,
RecoveryMiddleware: param.RecoveryMiddleware,
InstallRedirectMiddleware: param.InstallRedirectMiddleware,
ApplicationPasswordMiddleware: param.ApplicationPasswordMiddleware,
AdminHandler: param.AdminHandler,
AttachmentHandler: param.AttachmentHandler,
BackupHandler: param.BackupHandler,
CategoryHandler: param.CategoryHandler,
InstallHandler: param.InstallHandler,
JournalHandler: param.JournalHandler,
JournalCommentHandler: param.JournalCommentHandler,
LinkHandler: param.LinkHandler,
LogHandler: param.LogHandler,
MenuHandler: param.MenuHandler,
OptionHandler: param.OptionHandler,
PhotoHandler: param.PhotoHandler,
PostHandler: param.PostHandler,
PostCommentHandler: param.PostCommentHandler,
SheetHandler: param.SheetHandler,
SheetCommentHandler: param.SheetCommentHandler,
StatisticHandler: param.StatisticHandler,
TagHandler: param.TagHandler,
ThemeHandler: param.ThemeHandler,
UserHandler: param.UserHandler,
EmailHandler: param.EmailHandler,
OptionService: param.OptionService,
ThemeService: param.ThemeService,
SheetService: param.SheetService,
IndexHandler: param.IndexHandler,
FeedHandler: param.FeedHandler,
ArchiveHandler: param.ArchiveHandler,
ViewHandler: param.ViewHandler,
ContentCategoryHandler: param.ContentCategoryHandler,
ContentSheetHandler: param.ContentSheetHandler,
ContentTagHandler: param.ContentTagHandler,
ContentLinkHandler: param.ContentLinkHandler,
ContentPhotoHandler: param.ContentPhotoHandler,
ContentJournalHandler: param.ContentJournalHandler,
ContentAPIArchiveHandler: param.ContentAPIArchiveHandler,
ContentAPICategoryHandler: param.ContentAPICategoryHandler,
ContentAPIJournalHandler: param.ContentAPIJournalHandler,
ContentAPILinkHandler: param.ContentAPILinkHandler,
ContentAPIPostHandler: param.ContentAPIPostHandler,
ContentAPISheetHandler: param.ContentAPISheetHandler,
ContentAPIOptionHandler: param.ContentAPIOptionHandler,
ContentSearchHandler: param.ContentSearchHandler,
ContentAPIPhotoHandler: param.ContentAPIPhotoHandler,
ApplicationPasswordHandler: param.ApplicationPasswordHandler,
WpPostHandler: param.WpPostHandler,
WpUserHandler: param.WpUserHandler,
WpCategoryHandler: param.WpCategoryHandler,
}
lifecycle.Append(fx.Hook{
OnStop: httpServer.Shutdown,

@ -47,6 +47,7 @@ func InitApp() *fx.App {
middleware.NewGinLoggerMiddleware,
middleware.NewRecoveryMiddleware,
middleware.NewInstallRedirectMiddleware,
middleware.NewApplicationPasswordMiddleware,
),
fx.Populate(&dal.DB),
fx.Populate(&eventBus),

@ -0,0 +1,13 @@
package wp
type CategoryDTO struct {
ID int32 `json:"id"`
Count int32 `json:"count"`
Description string `json:"description"`
Link string `json:"link"`
Name string `json:"name"`
Slug string `json:"slug"`
Taxonomy string `json:"taxonomy"`
Parent int32 `json:"parent"`
Meta map[string]interface{} `json:"meta"`
}

@ -0,0 +1,30 @@
package wp
type PostDTO struct {
Date string `json:"date"`
DateGmt string `json:"date_gmt"`
Guid map[string]interface{} `json:"guid"`
Id int32 `json:"id"`
Link string `json:"link"`
Modified string `json:"modified"`
ModifiedGmt string `json:"modified_gmt"`
Slug string `json:"slug"`
Status string `json:"status"`
Type string `json:"type"`
Password string `json:"password"`
PermalinkTemplate string `json:"permalink_template"`
GeneratedSlug string `json:"generated_slug"`
Title string `json:"title"`
Content map[string]interface{} `json:"content"`
Author int32 `json:"author"`
Excerpt map[string]interface{} `json:"excerpt"`
FeaturedMedia int32 `json:"featured_media"`
CommentStatus string `json:"comment_status"`
PingStatus string `json:"ping_status"`
Format string `json:"format"`
Meta map[string]interface{} `json:"meta"`
Sticky bool `json:"sticky"`
Template string `json:"template"`
Categories []int32 `json:"categories"`
Tags []int32 `json:"tags"`
}

@ -0,0 +1,4 @@
package wp
type UserDTO struct {
}

@ -0,0 +1,30 @@
package param
type WpPost struct {
Date string `json:"date"`
DateGmt string `json:"date_gmt"`
Guid map[string]interface{} `json:"guid"`
Id int32 `json:"id"`
Link string `json:"link"`
Modified string `json:"modified"`
ModifiedGmt string `json:"modified_gmt"`
Slug string `json:"slug"`
Status string `json:"status"`
Type string `json:"type"`
Password string `json:"password"`
PermalinkTemplate string `json:"permalink_template"`
GeneratedSlug string `json:"generated_slug"`
Title string `json:"title"`
Content string `json:"content"`
Author int32 `json:"author"`
Excerpt map[string]interface{} `json:"excerpt"`
FeaturedMedia int32 `json:"featured_media"`
CommentStatus string `json:"comment_status"`
PingStatus string `json:"ping_status"`
Format string `json:"format"`
Meta map[string]interface{} `json:"meta"`
Sticky bool `json:"sticky"`
Template string `json:"template"`
Categories []int32 `json:"categories"`
Tags []int32 `json:"tags"`
}

@ -3,6 +3,7 @@ package service
import (
"context"
"github.com/go-sonic/sonic/model/dto"
"github.com/go-sonic/sonic/model/entity"
"github.com/go-sonic/sonic/model/param"
)
@ -10,4 +11,6 @@ type ApplicationPasswordService interface {
CreatePwd(ctx context.Context, appPwdParam *param.ApplicationPasswordParam) (*dto.ApplicationPasswordDTO, error)
DeletePwd(ctx context.Context, appPwdParam *param.ApplicationPasswordParam) error
List(ctx context.Context) ([]*dto.ApplicationPasswordDTO, error)
Verify(ctx context.Context, userId int32, pwd string) (*entity.ApplicationPassword, error)
Update(ctx context.Context, entityId int32, ip string) error
}

@ -117,18 +117,56 @@ func (a *applicationPasswordServiceImpl) List(ctx context.Context) ([]*dto.Appli
appPwdDTOList := make([]*dto.ApplicationPasswordDTO, len(entities))
for _, appPwdEntity := range entities {
var lastActivateTime int64
if appPwdEntity.LastActivateTime == nil {
lastActivateTime = appPwdEntity.LastActivateTime.Unix()
}
appPwdDTO := &dto.ApplicationPasswordDTO{
Name: appPwdEntity.Name,
LastActivateIp: appPwdEntity.LastActivateIP,
LastActivateTime: lastActivateTime,
CreateTime: appPwdEntity.CreateTime.Unix(),
}
appPwdDTOList = append(appPwdDTOList, appPwdDTO)
appPwdDTOList = append(appPwdDTOList, a.ConvertToDTO(appPwdEntity))
}
return appPwdDTOList, nil
}
func (a *applicationPasswordServiceImpl) Verify(ctx context.Context, userId int32, pwd string) (*entity.ApplicationPassword, error) {
appPwdDAL := dal.GetQueryByCtx(ctx).ApplicationPassword
entityList, err := appPwdDAL.WithContext(ctx).Where(appPwdDAL.UserID.Eq(userId)).Find()
if err != nil {
return nil, WrapDBErr(err)
}
pwdMd5 := util.Md5(pwd)
for _, appPwdEntity := range entityList {
if appPwdEntity.Password == pwdMd5 {
return appPwdEntity, nil
}
}
return nil, nil
}
func (a *applicationPasswordServiceImpl) Update(ctx context.Context, entityId int32, ip string) error {
appPwdDAL := dal.GetQueryByCtx(ctx).ApplicationPassword
now := time.Now()
_, err := appPwdDAL.WithContext(ctx).Where(appPwdDAL.ID.Eq(entityId)).Updates(entity.ApplicationPassword{
LastActivateIP: ip,
LastActivateTime: &now,
})
if err != nil {
return WrapDBErr(err)
}
return nil
}
func (a *applicationPasswordServiceImpl) ConvertToDTO(appPwdEntity *entity.ApplicationPassword) *dto.ApplicationPasswordDTO {
var lastActivateTime int64
if appPwdEntity.LastActivateTime == nil {
lastActivateTime = appPwdEntity.LastActivateTime.Unix()
}
appPwdDTO := &dto.ApplicationPasswordDTO{
Name: appPwdEntity.Name,
LastActivateIp: appPwdEntity.LastActivateIP,
LastActivateTime: lastActivateTime,
CreateTime: appPwdEntity.CreateTime.Unix(),
}
return appPwdDTO
}

@ -1,6 +1,10 @@
package util
import (
"errors"
"github.com/go-playground/validator/v10"
"github.com/go-sonic/sonic/handler/trans"
"github.com/go-sonic/sonic/util/xerr"
"regexp"
"strings"
"unicode/utf8"
@ -45,3 +49,11 @@ func HTMLFormatWordCount(html string) int64 {
text := CleanHTMLTag(html)
return int64(utf8.RuneCountInString(text) - len(blankRegexp.FindSubmatchIndex(StringToBytes(text))))
}
func WrapJsonBindErr(err error) error {
e := validator.ValidationErrors{}
if errors.As(err, &e) {
return xerr.WithStatus(e, xerr.StatusBadRequest).WithMsg(trans.Translate(e))
}
return xerr.WithStatus(err, xerr.StatusBadRequest)
}

Loading…
Cancel
Save