Files
cloudpods/pkg/notify/models/mod_verify.go
Rain 61ff025886 Fix(notify): Unified the api params and fix some bug.
1. Config Verify 和 Contact 的 InitalizeData 写得过于直接了。
会导致正常的数据被覆盖。实际上,数据的Init只需要更换数据
库的时候更新,如何判断数据库要被更新了,就是判断新出现的
column 有没有非零值的,如果有,就说明数据库不是第一次更新。

2. 前端的请求经过yunionapi,最终会调用climc里面注册的ResourceManager,
所以这个地方一定要和后端的参数处理统一。之前因为添加 uname
的支持破坏了这个地方的统一,虽然climc可以调用成功但是前端接口调用会失败。

3. Resend 的bug,会导致任务的阻塞。

4. 为了兼容kapacitor对climc
notify的调用,分成了notify和notify-batch两个命令。
2019-12-20 11:39:17 +08:00

135 lines
3.6 KiB
Go

// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package models
import (
"context"
"fmt"
"time"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/sqlchemy"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/notify/utils"
)
type SVerifyManager struct {
SStatusStandaloneResourceBaseManager
}
var VerifyManager *SVerifyManager
func init() {
VerifyManager = &SVerifyManager{
SStatusStandaloneResourceBaseManager: NewStatusStandaloneResourceBaseManager(
SVerify{},
"notify_t_verify",
"verification",
"verifications",
),
}
VerifyManager.SetVirtualObject(VerifyManager)
}
type SVerify struct {
SStatusStandaloneResourceBase
CID string `width:"128" nullable:"false" create:"required" list:"user"`
Token string `width:"200" nullable:"false" create:"required" list:"user"`
SendAt time.Time `nullable:"true" create:"optional"`
ExpireAt time.Time `create:"required" list:"user"`
}
// NewSVerify Generate a SVerify instance which implement a Verification Token.
func NewSVerify(contactType string, cid string) *SVerify {
var token string
var expireAt time.Time
now := time.Now()
if contactType == EMAIL {
token = utils.GenerateEmailToken(32)
expireAt = now.Add(12 * time.Hour)
} else {
token = utils.GenerateMobileToken()
expireAt = now.Add(2 * time.Minute)
}
ret := &SVerify{
CID: cid,
Token: token,
ExpireAt: expireAt,
SendAt: now,
}
ret.ID = DefaultUUIDGenerator()
ret.SetModelManager(VerifyManager, ret)
return ret
}
func (self *SVerifyManager) InitializeData() error {
q := self.Query()
q = q.Filter(sqlchemy.OR(sqlchemy.IsNotNull(q.Field("updated_at")), sqlchemy.IsTrue(q.Field("deleted"))))
n, err := q.CountWithError()
if err != nil {
return err
}
if n > 0 {
log.Debugf("no need to init data for %s", self.TableSpec().Name())
// no need to init data
return nil
}
log.Debugf("need to init data for %s", self.TableSpec().Name())
sql := fmt.Sprintf("update %s set updated_at=update_at, deleted=is_deleted", self.TableSpec().Name())
q = sqlchemy.NewRawQuery(sql, "")
q.Row()
return nil
}
func (self *SVerifyManager) FetchByCID(cid string, filter func(q *sqlchemy.SQuery) *sqlchemy.SQuery) ([]SVerify, error) {
q := self.Query()
q.Filter(sqlchemy.Equals(q.Field("cid"), cid))
q = filter(q)
records := make([]SVerify, 0, 1)
err := db.FetchModelObjects(self, q, &records)
if err != nil {
return nil, err
}
return records, nil
}
func (self *SVerifyManager) FetchByID(id string) ([]SVerify, error) {
q := self.Query()
q.Filter(sqlchemy.Equals(q.Field("id"), id))
records := make([]SVerify, 0, 1)
err := db.FetchModelObjects(self, q, &records)
if err != nil {
return nil, err
}
return records, nil
}
func (self *SVerifyManager) Create(ctx context.Context, userCred mcclient.TokenCredential, verify *SVerify) error {
data := jsonutils.Marshal(verify)
ownerID, err := utils.FetchOwnerId(ctx, self, userCred, data)
if err != nil {
return err
}
_, err = db.DoCreate(self, ctx, userCred, jsonutils.JSONNull, data, ownerID)
if err != nil {
return err
}
return nil
}