1 parent
76bc24d2bd
commit
01cbdc9875
73 files changed
+4722
-538
No files matched your search
@@ -6,7 +6,6 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"pmail/dto/response"
|
||||
"pmail/services/auth"
|
||||
"pmail/services/detail"
|
||||
"pmail/utils/context"
|
||||
)
|
||||
@@ -37,13 +36,6 @@ func EmailDetail(ctx *context.Context, w http.ResponseWriter, req *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
// 检查是否有权限
|
||||
hasAuth := auth.HasAuth(ctx, email)
|
||||
if !hasAuth {
|
||||
response.NewErrorResponse(response.ParamsError, "", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
response.NewSuccessResponse(email).FPrint(w)
|
||||
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"pmail/dto"
|
||||
"pmail/dto/response"
|
||||
"pmail/services/list"
|
||||
"pmail/utils/context"
|
||||
@@ -61,7 +62,14 @@ func EmailList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
retData.PageSize = 15
|
||||
}
|
||||
|
||||
emailList, total := list.GetEmailList(ctx, retData.Tag, retData.Keyword, offset, retData.PageSize)
|
||||
var tagInfo dto.SearchTag = dto.SearchTag{
|
||||
Type: -1,
|
||||
Status: -1,
|
||||
GroupId: -1,
|
||||
}
|
||||
_ = json.Unmarshal([]byte(retData.Tag), &tagInfo)
|
||||
|
||||
emailList, total := list.GetEmailList(ctx, tagInfo, retData.Keyword, false, offset, retData.PageSize)
|
||||
|
||||
for _, email := range emailList {
|
||||
var sender User
|
||||
|
||||
@@ -64,6 +64,11 @@ func Send(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if !ctx.IsAdmin && reqData.From.Name != ctx.UserAccount {
|
||||
response.NewErrorResponse(response.ParamsError, "params error", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData.From.Email == "" && reqData.From.Name != "" {
|
||||
reqData.From.Email = reqData.From.Name + "@" + config.Instance.Domain
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"pmail/models"
|
||||
"pmail/session"
|
||||
"pmail/utils/context"
|
||||
"pmail/utils/errors"
|
||||
"pmail/utils/password"
|
||||
)
|
||||
|
||||
@@ -35,17 +36,25 @@ func Login(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
var user models.User
|
||||
|
||||
encodePwd := password.Encode(reqData.Password)
|
||||
|
||||
_, err = db.Instance.Where("account =? and password =?", reqData.Account, encodePwd).Get(&user)
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
_, err = db.Instance.Where("account =? and password =? and disabled=0", reqData.Account, encodePwd).Get(&user)
|
||||
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
if user.ID != 0 {
|
||||
userStr, _ := json.Marshal(user)
|
||||
session.Instance.Put(req.Context(), "user", string(userStr))
|
||||
response.NewSuccessResponse("").FPrint(w)
|
||||
response.NewSuccessResponse(map[string]any{
|
||||
"account": user.Account,
|
||||
"name": user.Name,
|
||||
"is_admin": user.IsAdmin,
|
||||
}).FPrint(w)
|
||||
} else {
|
||||
response.NewErrorResponse(response.ParamsError, i18n.GetText(ctx.Lang, "aperror"), "").FPrint(w)
|
||||
}
|
||||
}
|
||||
|
||||
func Logout(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
session.Instance.Clear(ctx.Context)
|
||||
response.NewSuccessResponse("Success").FPrint(w)
|
||||
}
|
||||
@@ -1,13 +1,10 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/http"
|
||||
"pmail/dto/response"
|
||||
"pmail/utils/context"
|
||||
)
|
||||
|
||||
func Ping(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
func Ping(w http.ResponseWriter, req *http.Request) {
|
||||
response.NewSuccessResponse("pong").FPrint(w)
|
||||
log.WithContext(ctx).Info("pong")
|
||||
}
|
||||
@@ -18,7 +18,7 @@ import (
|
||||
)
|
||||
|
||||
func GetRule(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
res := rule.GetAllRules(ctx)
|
||||
res := rule.GetAllRules(ctx, ctx.UserID)
|
||||
response.NewSuccessResponse(res).FPrint(w)
|
||||
}
|
||||
|
||||
|
||||
@@ -134,14 +134,39 @@ func Setup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["step"] == "ssl" && reqData["action"] == "getParams" {
|
||||
params, err := ssl.GetServerParamsList(reqData["serverName"])
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(params).FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["step"] == "ssl" && reqData["action"] == "setParams" {
|
||||
for key, v := range reqData {
|
||||
if key != "step" && key != "action" {
|
||||
ssl.SetDomainServerParams(key, v)
|
||||
}
|
||||
}
|
||||
response.NewSuccessResponse("Succ").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["step"] == "ssl" && reqData["action"] == "set" {
|
||||
err := ssl.SetSSL(reqData["ssl_type"], reqData["key_path"], reqData["crt_path"])
|
||||
|
||||
serviceName, ok := reqData["serviceName"]
|
||||
if !ok {
|
||||
serviceName = ""
|
||||
}
|
||||
err := ssl.SetSSL(reqData["ssl_type"], reqData["key_path"], reqData["crt_path"], serviceName)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
if reqData["ssl_type"] == config.SSLTypeAuto {
|
||||
if reqData["ssl_type"] == config.SSLTypeAutoHTTP || reqData["ssl_type"] == config.SSLTypeAutoDNS {
|
||||
err = ssl.GenSSL(false)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
@@ -150,7 +175,10 @@ func Setup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
response.NewSuccessResponse("Succ").FPrint(w)
|
||||
setup.Finish(ctx)
|
||||
|
||||
if reqData["ssl_type"] == config.SSLTypeUser {
|
||||
setup.Finish()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cast"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"pmail/db"
|
||||
"pmail/dto/response"
|
||||
"pmail/models"
|
||||
"pmail/utils/context"
|
||||
"pmail/utils/password"
|
||||
)
|
||||
|
||||
type userCreateRequest struct {
|
||||
Id int `json:"id"`
|
||||
Account string `json:"account"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Disabled int `json:"disabled"`
|
||||
}
|
||||
|
||||
func CreateUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
if !ctx.IsAdmin {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "No Access Privileges", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
reqBytes, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
var reqData userCreateRequest
|
||||
err = json.Unmarshal(reqBytes, &reqData)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
if reqData.Username == "" || reqData.Password == "" || reqData.Account == "" {
|
||||
response.NewErrorResponse(response.ParamsError, "Params Error", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
var user models.User
|
||||
user.Name = reqData.Username
|
||||
user.Password = password.Encode(reqData.Password)
|
||||
user.Account = reqData.Account
|
||||
|
||||
_, err = db.Instance.Insert(&user)
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
response.NewSuccessResponse(user).FPrint(w)
|
||||
}
|
||||
|
||||
type userListRequest struct {
|
||||
CurrentPage int `json:"current_page"`
|
||||
PageSize int `json:"page_size"`
|
||||
}
|
||||
|
||||
func UserList(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if !ctx.IsAdmin {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "No Access Privileges", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
reqBytes, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
var reqData userListRequest
|
||||
err = json.Unmarshal(reqBytes, &reqData)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
offset := 0
|
||||
if reqData.CurrentPage >= 1 {
|
||||
offset = (reqData.CurrentPage - 1) * reqData.PageSize
|
||||
}
|
||||
|
||||
if reqData.PageSize == 0 {
|
||||
reqData.PageSize = 15
|
||||
}
|
||||
|
||||
var users []models.User
|
||||
|
||||
totalNum, err := db.Instance.Table(&models.User{}).Limit(reqData.PageSize, offset).FindAndCount(&users)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
response.NewSuccessResponse(map[string]any{
|
||||
"current_page": reqData.CurrentPage,
|
||||
"total_page": cast.ToInt(math.Ceil(cast.ToFloat64(totalNum) / cast.ToFloat64(reqData.PageSize))),
|
||||
"list": users,
|
||||
}).FPrint(w)
|
||||
|
||||
}
|
||||
|
||||
func Info(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
response.NewSuccessResponse(map[string]any{
|
||||
"account": ctx.UserAccount,
|
||||
"name": ctx.UserName,
|
||||
"is_admin": ctx.IsAdmin,
|
||||
}).FPrint(w)
|
||||
}
|
||||
|
||||
func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
|
||||
if !ctx.IsAdmin {
|
||||
response.NewErrorResponse(response.NoAccessPrivileges, "No Access Privileges", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
reqBytes, err := io.ReadAll(req.Body)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
var reqData userCreateRequest
|
||||
err = json.Unmarshal(reqBytes, &reqData)
|
||||
if err != nil {
|
||||
log.Errorf("%+v", err)
|
||||
}
|
||||
|
||||
if reqData.Id == 0 && reqData.Account == "" {
|
||||
response.NewErrorResponse(response.ParamsError, "Params Error", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
var user models.User
|
||||
if reqData.Id != 0 {
|
||||
_, err = db.Instance.Where("id=?", reqData.Id).Get(&user)
|
||||
if err != nil {
|
||||
log.Errorf("SQL Error: %+v", err)
|
||||
}
|
||||
} else {
|
||||
_, err = db.Instance.Where("account=?", reqData.Account).Get(&user)
|
||||
if err != nil {
|
||||
log.Errorf("SQL Error: %+v", err)
|
||||
}
|
||||
}
|
||||
if user.ID == 0 {
|
||||
response.NewErrorResponse(response.ParamsError, "User not found", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if reqData.Username != "" && reqData.Username != user.Name {
|
||||
user.Name = reqData.Username
|
||||
}
|
||||
|
||||
if reqData.Disabled != user.Disabled {
|
||||
user.Disabled = reqData.Disabled
|
||||
}
|
||||
if reqData.Password != "" {
|
||||
user.Password = password.Encode(reqData.Password)
|
||||
}
|
||||
|
||||
num, err := db.Instance.ID(user.ID).Cols("name", "password", "disabled").Update(&user)
|
||||
|
||||
if err != nil {
|
||||
response.NewErrorResponse(response.ServerError, err.Error(), "").FPrint(w)
|
||||
return
|
||||
}
|
||||
if num == 0 {
|
||||
response.NewErrorResponse(response.ServerError, "No Data Update", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
|
||||
response.NewSuccessResponse(user).FPrint(w)
|
||||
}
|
||||
Reference in new issue
Block a user