209 lines
5.7 KiB
Go
209 lines
5.7 KiB
Go
// Copyright 2023 GoEdge CDN goedge.cdn@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
|
//go:build plus
|
|
|
|
package services
|
|
|
|
import (
|
|
"context"
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
)
|
|
|
|
// UserScriptService 用户脚本服务
|
|
type UserScriptService struct {
|
|
BaseService
|
|
}
|
|
|
|
// FindUserScript 查找单个用户脚本信息
|
|
func (this *UserScriptService) FindUserScript(ctx context.Context, req *pb.FindUserScriptRequest) (*pb.FindUserScriptResponse, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
userScript, err := models.SharedUserScriptDAO.FindEnabledUserScript(tx, req.UserScriptId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if userScript == nil {
|
|
return &pb.FindUserScriptResponse{
|
|
UserScript: nil,
|
|
}, nil
|
|
}
|
|
|
|
// user
|
|
var pbUser *pb.User
|
|
if userScript.UserId > 0 {
|
|
user, err := models.SharedUserDAO.FindEnabledBasicUser(tx, int64(userScript.UserId))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if user != nil {
|
|
pbUser = &pb.User{
|
|
Id: int64(user.Id),
|
|
Fullname: user.Fullname,
|
|
Username: user.Username,
|
|
}
|
|
}
|
|
}
|
|
|
|
return &pb.FindUserScriptResponse{
|
|
UserScript: &pb.UserScript{
|
|
Id: int64(userScript.Id),
|
|
UserId: int64(userScript.UserId),
|
|
AdminId: int64(userScript.AdminId),
|
|
Code: userScript.Code,
|
|
CodeMD5: userScript.CodeMD5,
|
|
CreatedAt: int64(userScript.CreatedAt),
|
|
IsRejected: userScript.IsRejected,
|
|
RejectedAt: int64(userScript.RejectedAt),
|
|
RejectedReason: userScript.RejectedReason,
|
|
IsPassed: userScript.IsPassed,
|
|
PassedAt: int64(userScript.PassedAt),
|
|
User: pbUser,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// FindUserScriptWithMD5 根据代码MD5查找脚本
|
|
func (this *UserScriptService) FindUserScriptWithMD5(ctx context.Context, req *pb.FindUserScriptWithMD5Request) (*pb.FindUserScriptWithMD5Response, error) {
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
userScript, err := models.SharedUserScriptDAO.FindUserScriptWithCodeMD5(tx, req.CodeMD5)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if userScript == nil {
|
|
return &pb.FindUserScriptWithMD5Response{
|
|
UserScript: nil,
|
|
}, nil
|
|
}
|
|
|
|
// check user
|
|
if userId > 0 {
|
|
if userId != int64(userScript.UserId) {
|
|
return nil, this.PermissionError()
|
|
}
|
|
}
|
|
|
|
return &pb.FindUserScriptWithMD5Response{
|
|
UserScript: &pb.UserScript{
|
|
Id: int64(userScript.Id),
|
|
UserId: int64(userScript.UserId),
|
|
AdminId: int64(userScript.AdminId),
|
|
Code: userScript.Code,
|
|
CodeMD5: userScript.CodeMD5,
|
|
CreatedAt: int64(userScript.CreatedAt),
|
|
IsRejected: userScript.IsRejected,
|
|
RejectedAt: int64(userScript.RejectedAt),
|
|
RejectedReason: userScript.RejectedReason,
|
|
IsPassed: userScript.IsPassed,
|
|
PassedAt: int64(userScript.PassedAt),
|
|
User: nil,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// CountUserScripts 计算用户脚本数量
|
|
func (this *UserScriptService) CountUserScripts(ctx context.Context, req *pb.CountUserScriptsRequest) (*pb.RPCCountResponse, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
count, err := models.SharedUserScriptDAO.CountUserScripts(tx, req.UserId, req.IsAuditing)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return this.SuccessCount(count)
|
|
}
|
|
|
|
// ListUserScripts 列出单页用户脚本
|
|
func (this *UserScriptService) ListUserScripts(ctx context.Context, req *pb.ListUserScriptsRequest) (*pb.ListUserScriptsResponse, error) {
|
|
_, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
userScripts, err := models.SharedUserScriptDAO.ListUserScripts(tx, req.UserId, req.IsAuditing, req.Offset, req.Size)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var pbUserScripts []*pb.UserScript
|
|
for _, userScript := range userScripts {
|
|
// user
|
|
var pbUser *pb.User
|
|
if userScript.UserId > 0 {
|
|
user, err := models.SharedUserDAO.FindEnabledBasicUser(tx, int64(userScript.UserId))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if user != nil {
|
|
pbUser = &pb.User{
|
|
Id: int64(user.Id),
|
|
Fullname: user.Fullname,
|
|
Username: user.Username,
|
|
}
|
|
}
|
|
}
|
|
|
|
pbUserScripts = append(pbUserScripts, &pb.UserScript{
|
|
Id: int64(userScript.Id),
|
|
UserId: int64(userScript.UserId),
|
|
AdminId: int64(userScript.AdminId),
|
|
Code: userScript.Code,
|
|
CodeMD5: userScript.CodeMD5,
|
|
CreatedAt: int64(userScript.CreatedAt),
|
|
IsRejected: userScript.IsRejected,
|
|
RejectedAt: int64(userScript.RejectedAt),
|
|
RejectedReason: userScript.RejectedReason,
|
|
IsPassed: userScript.IsPassed,
|
|
PassedAt: int64(userScript.PassedAt),
|
|
User: pbUser,
|
|
})
|
|
}
|
|
|
|
return &pb.ListUserScriptsResponse{
|
|
UserScripts: pbUserScripts,
|
|
}, nil
|
|
}
|
|
|
|
// PassUserScript 审核并通过用户脚本
|
|
func (this *UserScriptService) PassUserScript(ctx context.Context, req *pb.PassUserScriptRequest) (*pb.RPCSuccess, error) {
|
|
adminId, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
err = models.SharedUserScriptDAO.PassUserScript(tx, req.UserScriptId, adminId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return this.Success()
|
|
}
|
|
|
|
// RejectUserScript 审核并驳回用户脚本
|
|
func (this *UserScriptService) RejectUserScript(ctx context.Context, req *pb.RejectUserScriptRequest) (*pb.RPCSuccess, error) {
|
|
adminId, err := this.ValidateAdmin(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var tx = this.NullTx()
|
|
err = models.SharedUserScriptDAO.RejectUserScript(tx, req.UserScriptId, adminId, req.Reason)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return this.Success()
|
|
}
|