2022-11-11 17:41:39 +08:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
2022-12-14 15:26:51 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/request"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto/response"
|
2022-11-11 17:41:39 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/model"
|
2022-12-05 15:50:53 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/buserr"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
2022-11-11 17:41:39 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/utils/ssl"
|
|
|
|
)
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
type WebsiteAcmeAccountService struct {
|
2022-11-11 17:41:39 +08:00
|
|
|
}
|
|
|
|
|
2023-03-28 18:00:06 +08:00
|
|
|
type IWebsiteAcmeAccountService interface {
|
|
|
|
Page(search dto.PageInfo) (int64, []response.WebsiteAcmeAccountDTO, error)
|
|
|
|
Create(create request.WebsiteAcmeAccountCreate) (response.WebsiteAcmeAccountDTO, error)
|
|
|
|
Delete(id uint) error
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIWebsiteAcmeAccountService() IWebsiteAcmeAccountService {
|
|
|
|
return &WebsiteAcmeAccountService{}
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:26:51 +08:00
|
|
|
func (w WebsiteAcmeAccountService) Page(search dto.PageInfo) (int64, []response.WebsiteAcmeAccountDTO, error) {
|
2022-11-11 17:41:39 +08:00
|
|
|
total, accounts, err := websiteAcmeRepo.Page(search.Page, search.PageSize, commonRepo.WithOrderBy("created_at desc"))
|
2022-12-14 15:26:51 +08:00
|
|
|
var accountDTOs []response.WebsiteAcmeAccountDTO
|
2022-11-11 17:41:39 +08:00
|
|
|
for _, account := range accounts {
|
2022-12-14 15:26:51 +08:00
|
|
|
accountDTOs = append(accountDTOs, response.WebsiteAcmeAccountDTO{
|
2022-11-11 17:41:39 +08:00
|
|
|
WebsiteAcmeAccount: account,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return total, accountDTOs, err
|
|
|
|
}
|
|
|
|
|
2022-12-14 15:26:51 +08:00
|
|
|
func (w WebsiteAcmeAccountService) Create(create request.WebsiteAcmeAccountCreate) (response.WebsiteAcmeAccountDTO, error) {
|
2023-02-20 17:04:59 +08:00
|
|
|
exist, _ := websiteAcmeRepo.GetFirst(websiteAcmeRepo.WithEmail(create.Email))
|
|
|
|
if exist != nil {
|
|
|
|
return response.WebsiteAcmeAccountDTO{}, buserr.New(constant.ErrEmailIsExist)
|
|
|
|
}
|
|
|
|
|
2023-11-14 18:44:09 +08:00
|
|
|
if create.Type == "google" && (create.EabKid == "" || create.EabHmacKey == "") {
|
|
|
|
return response.WebsiteAcmeAccountDTO{}, buserr.New(constant.ErrEabKidOrEabHmacKeyCannotBlank)
|
|
|
|
} else {
|
|
|
|
create.EabKid = ""
|
|
|
|
create.EabHmacKey = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
acmeAccount := &model.WebsiteAcmeAccount{
|
|
|
|
Email: create.Email,
|
|
|
|
Type: create.Type,
|
|
|
|
}
|
|
|
|
client, err := ssl.NewAcmeClient(acmeAccount)
|
2022-11-11 17:41:39 +08:00
|
|
|
if err != nil {
|
2022-12-14 15:26:51 +08:00
|
|
|
return response.WebsiteAcmeAccountDTO{}, err
|
2022-11-11 17:41:39 +08:00
|
|
|
}
|
2023-11-14 18:44:09 +08:00
|
|
|
acmeAccount.PrivateKey = string(ssl.GetPrivateKey(client.User.GetPrivateKey()))
|
|
|
|
acmeAccount.URL = client.User.Registration.URI
|
|
|
|
|
|
|
|
if err := websiteAcmeRepo.Create(*acmeAccount); err != nil {
|
2022-12-14 15:26:51 +08:00
|
|
|
return response.WebsiteAcmeAccountDTO{}, err
|
2022-11-11 17:41:39 +08:00
|
|
|
}
|
2023-11-14 18:44:09 +08:00
|
|
|
return response.WebsiteAcmeAccountDTO{WebsiteAcmeAccount: *acmeAccount}, nil
|
2022-11-11 17:41:39 +08:00
|
|
|
}
|
|
|
|
|
2022-12-13 17:20:13 +08:00
|
|
|
func (w WebsiteAcmeAccountService) Delete(id uint) error {
|
2022-12-05 15:50:53 +08:00
|
|
|
if ssls, _ := websiteSSLRepo.List(websiteSSLRepo.WithByAcmeAccountId(id)); len(ssls) > 0 {
|
|
|
|
return buserr.New(constant.ErrAccountCannotDelete)
|
|
|
|
}
|
2022-11-11 17:41:39 +08:00
|
|
|
return websiteAcmeRepo.DeleteBy(commonRepo.WithByID(id))
|
|
|
|
}
|