feat: acme 账号增加名称重复性校验

This commit is contained in:
zhengkunwang223 2023-02-20 17:04:59 +08:00 committed by zhengkunwang223
parent 8f3cfc3f25
commit 58ce2055fd
6 changed files with 26 additions and 8 deletions

View File

@ -1,13 +1,17 @@
package repo
import "github.com/1Panel-dev/1Panel/backend/app/model"
import (
"github.com/1Panel-dev/1Panel/backend/app/model"
"gorm.io/gorm"
)
type IAcmeAccountRepo interface {
Page(page, size int, opts ...DBOption) (int64, []model.WebsiteAcmeAccount, error)
GetFirst(opts ...DBOption) (model.WebsiteAcmeAccount, error)
GetFirst(opts ...DBOption) (*model.WebsiteAcmeAccount, error)
Create(account model.WebsiteAcmeAccount) error
Save(account model.WebsiteAcmeAccount) error
DeleteBy(opts ...DBOption) error
WithEmail(email string) DBOption
}
func NewIAcmeAccountRepo() IAcmeAccountRepo {
@ -17,6 +21,12 @@ func NewIAcmeAccountRepo() IAcmeAccountRepo {
type WebsiteAcmeAccountRepo struct {
}
func (w *WebsiteAcmeAccountRepo) WithEmail(email string) DBOption {
return func(db *gorm.DB) *gorm.DB {
return db.Where("email = ?", email)
}
}
func (w *WebsiteAcmeAccountRepo) Page(page, size int, opts ...DBOption) (int64, []model.WebsiteAcmeAccount, error) {
var accounts []model.WebsiteAcmeAccount
db := getDb(opts...).Model(&model.WebsiteAcmeAccount{})
@ -26,13 +36,13 @@ func (w *WebsiteAcmeAccountRepo) Page(page, size int, opts ...DBOption) (int64,
return count, accounts, err
}
func (w *WebsiteAcmeAccountRepo) GetFirst(opts ...DBOption) (model.WebsiteAcmeAccount, error) {
func (w *WebsiteAcmeAccountRepo) GetFirst(opts ...DBOption) (*model.WebsiteAcmeAccount, error) {
var account model.WebsiteAcmeAccount
db := getDb(opts...).Model(&model.WebsiteAcmeAccount{})
if err := db.First(&account).Error; err != nil {
return account, err
return nil, err
}
return account, nil
return &account, nil
}
func (w *WebsiteAcmeAccountRepo) Create(account model.WebsiteAcmeAccount) error {

View File

@ -25,6 +25,11 @@ func (w WebsiteAcmeAccountService) Page(search dto.PageInfo) (int64, []response.
}
func (w WebsiteAcmeAccountService) Create(create request.WebsiteAcmeAccountCreate) (response.WebsiteAcmeAccountDTO, error) {
exist, _ := websiteAcmeRepo.GetFirst(websiteAcmeRepo.WithEmail(create.Email))
if exist != nil {
return response.WebsiteAcmeAccountDTO{}, buserr.New(constant.ErrEmailIsExist)
}
client, err := ssl.NewAcmeClient(create.Email, "")
if err != nil {
return response.WebsiteAcmeAccountDTO{}, err

View File

@ -70,6 +70,7 @@ var (
ErrSSLCannotDelete = "ErrSSLCannotDelete"
ErrAccountCannotDelete = "ErrAccountCannotDelete"
ErrSSLApply = "ErrSSLApply"
ErrEmailIsExist = "ErrEmailIsExist"
)
//file

View File

@ -36,4 +36,5 @@ ErrAppDelete: 'Other Website use this App'
#ssl
ErrSSLCannotDelete: "The certificate is being used by the website and cannot be removed"
ErrAccountCannotDelete: "The certificate associated with the account cannot be deleted"
ErrSSLApply: "The certificate continues to be signed successfully, but openresty reload fails, please check the configuration"
ErrSSLApply: "The certificate continues to be signed successfully, but openresty reload fails, please check the configuration"
ErrEmailIsExist: 'Email is already exist'

View File

@ -36,4 +36,5 @@ ErrAppDelete: '其他网站使用此应用,不能删除'
#ssl
ErrSSLCannotDelete: "证书正在被网站使用,无法删除"
ErrAccountCannotDelete: "账号关联证书,无法删除"
ErrSSLApply: "证书续签成功openresty reload失败请检查配置"
ErrSSLApply: "证书续签成功openresty reload失败请检查配置"
ErrEmailIsExist: '邮箱已存在'

View File

@ -11,7 +11,7 @@
<el-col :span="22" :offset="1">
<el-form ref="accountForm" label-position="top" :model="account" :rules="rules" v-loading="loading">
<el-form-item :label="$t('website.email')" prop="email">
<el-input v-model="account.email"></el-input>
<el-input v-model.trim="account.email"></el-input>
</el-form-item>
</el-form>
</el-col>