126 lines
3.2 KiB
Go
126 lines
3.2 KiB
Go
package models
|
|
|
|
import (
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/iwind/TeaGo/Tea"
|
|
"github.com/iwind/TeaGo/dbs"
|
|
"github.com/iwind/TeaGo/rands"
|
|
"github.com/iwind/TeaGo/types"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
HTTPDNSAppSecretStateEnabled = 1
|
|
HTTPDNSAppSecretStateDisabled = 0
|
|
)
|
|
|
|
type HTTPDNSAppSecretDAO dbs.DAO
|
|
|
|
func NewHTTPDNSAppSecretDAO() *HTTPDNSAppSecretDAO {
|
|
return dbs.NewDAO(&HTTPDNSAppSecretDAO{
|
|
DAOObject: dbs.DAOObject{
|
|
DB: Tea.Env,
|
|
Table: "edgeHTTPDNSAppSecrets",
|
|
Model: new(HTTPDNSAppSecret),
|
|
PkName: "id",
|
|
},
|
|
}).(*HTTPDNSAppSecretDAO)
|
|
}
|
|
|
|
var SharedHTTPDNSAppSecretDAO *HTTPDNSAppSecretDAO
|
|
|
|
func init() {
|
|
dbs.OnReady(func() {
|
|
SharedHTTPDNSAppSecretDAO = NewHTTPDNSAppSecretDAO()
|
|
})
|
|
}
|
|
|
|
func (this *HTTPDNSAppSecretDAO) InitAppSecret(tx *dbs.Tx, appDbId int64, signEnabled bool) (string, uint64, error) {
|
|
signSecret := "ss_" + rands.HexString(12)
|
|
now := uint64(time.Now().Unix())
|
|
var op = NewHTTPDNSAppSecretOperator()
|
|
op.AppId = appDbId
|
|
op.SignEnabled = signEnabled
|
|
op.SignSecret = signSecret
|
|
op.SignUpdatedAt = now
|
|
op.UpdatedAt = now
|
|
op.State = HTTPDNSAppSecretStateEnabled
|
|
err := this.Save(tx, op)
|
|
return signSecret, now, err
|
|
}
|
|
|
|
func (this *HTTPDNSAppSecretDAO) FindEnabledAppSecret(tx *dbs.Tx, appDbId int64) (*HTTPDNSAppSecret, error) {
|
|
one, err := this.Query(tx).
|
|
Attr("appId", appDbId).
|
|
State(HTTPDNSAppSecretStateEnabled).
|
|
Find()
|
|
if one == nil {
|
|
return nil, err
|
|
}
|
|
return one.(*HTTPDNSAppSecret), nil
|
|
}
|
|
|
|
func (this *HTTPDNSAppSecretDAO) UpdateSignEnabled(tx *dbs.Tx, appDbId int64, signEnabled bool) error {
|
|
_, err := this.Query(tx).
|
|
Attr("appId", appDbId).
|
|
State(HTTPDNSAppSecretStateEnabled).
|
|
Set("signEnabled", signEnabled).
|
|
Set("updatedAt", time.Now().Unix()).
|
|
Update()
|
|
return err
|
|
}
|
|
|
|
func (this *HTTPDNSAppSecretDAO) ResetSignSecret(tx *dbs.Tx, appDbId int64) (string, int64, error) {
|
|
signSecret := "ss_" + rands.HexString(12)
|
|
now := time.Now().Unix()
|
|
_, err := this.Query(tx).
|
|
Attr("appId", appDbId).
|
|
State(HTTPDNSAppSecretStateEnabled).
|
|
Set("signSecret", signSecret).
|
|
Set("signUpdatedAt", now).
|
|
Set("updatedAt", now).
|
|
Update()
|
|
if err != nil {
|
|
return "", 0, err
|
|
}
|
|
return signSecret, now, nil
|
|
}
|
|
|
|
func (this *HTTPDNSAppSecretDAO) FindSignEnabled(tx *dbs.Tx, appDbId int64) (bool, error) {
|
|
one, err := this.FindEnabledAppSecret(tx, appDbId)
|
|
if err != nil || one == nil {
|
|
return false, err
|
|
}
|
|
return one.SignEnabled, nil
|
|
}
|
|
|
|
func (this *HTTPDNSAppSecretDAO) FindSignSecretWithAppDbId(tx *dbs.Tx, appDbId int64) (string, error) {
|
|
return this.Query(tx).
|
|
Attr("appId", appDbId).
|
|
State(HTTPDNSAppSecretStateEnabled).
|
|
Result("signSecret").
|
|
FindStringCol("")
|
|
}
|
|
|
|
func (this *HTTPDNSAppSecretDAO) FindSignUpdatedAt(tx *dbs.Tx, appDbId int64) (int64, error) {
|
|
col, err := this.Query(tx).
|
|
Attr("appId", appDbId).
|
|
State(HTTPDNSAppSecretStateEnabled).
|
|
Result("signUpdatedAt").
|
|
FindCol(nil)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return types.Int64(col), nil
|
|
}
|
|
|
|
func (this *HTTPDNSAppSecretDAO) DisableAppSecret(tx *dbs.Tx, appDbId int64) error {
|
|
_, err := this.Query(tx).
|
|
Attr("appId", appDbId).
|
|
State(HTTPDNSAppSecretStateEnabled).
|
|
Set("state", HTTPDNSAppSecretStateDisabled).
|
|
Set("updatedAt", time.Now().Unix()).
|
|
Update()
|
|
return err
|
|
}
|