expire old consent records

This commit is contained in:
stremovsky
2019-12-23 16:56:25 +02:00
parent 4ad68e03d9
commit c7493f22d8
4 changed files with 38 additions and 1 deletions

View File

@@ -245,6 +245,7 @@ func (e mainEnv) dbCleanup() {
if exp > 0 {
e.db.deleteExpired0(TblName.Audit, exp)
}
e.db.expireConsentRecords()
case <-e.stopChan:
log.Printf("db cleanup closed\n")
ticker.Stop()

View File

@@ -148,3 +148,28 @@ func (dbobj dbcon) filterConsentRecords(brief string, offset int32, limit int32)
//fmt.Printf("Found multiple documents (array of pointers): %+v\n", results)
return resultJSON, count, nil
}
func (dbobj dbcon) expireConsentRecords() error {
records, err := dbobj.getExpiring(TblName.Consent, "status", "accept")
if err != nil {
return err
}
for _, rec := range records {
now := int32(time.Now().Unix())
// update date, status
bdoc := bson.M{}
bdoc["when"] = now
bdoc["status"] = "expired"
userTOKEN := rec["token"].(string)
brief := rec["brief"].(string)
fmt.Printf("This consent record is expired: %s - %s\n", userTOKEN, brief)
if len(userTOKEN) > 0 {
fmt.Printf("%s %s\n", userTOKEN, brief)
dbobj.updateRecord2(TblName.Consent, "token", userTOKEN, "brief", brief, &bdoc, nil)
} else {
usercode := rec["who"].(string)
dbobj.updateRecord2(TblName.Consent, "who", usercode, "brief", brief, &bdoc, nil)
}
}
return nil
}

View File

@@ -654,6 +654,13 @@ func (dbobj dbcon) cleanupRecord(t Tbl, keyName string, keyValue string, data in
return num, err
}
func (dbobj dbcon) getExpiring(t Tbl, keyName string, keyValue string) ([]bson.M, error) {
table := getTable(t)
now := int32(time.Now().Unix())
q := fmt.Sprintf("select * from %s WHERE endtime>0 AND endtime<%d AND %s=$1", table, now, escapeName(keyName))
return dbobj.getListDo(q, keyValue)
}
func (dbobj dbcon) getList(t Tbl, keyName string, keyValue string, start int32, limit int32) ([]bson.M, error) {
table := getTable(t)
if limit > 100 {
@@ -668,6 +675,10 @@ func (dbobj dbcon) getList(t Tbl, keyName string, keyValue string, start int32,
q = q + " LIMIT " + strconv.FormatInt(int64(limit), 10)
}
fmt.Printf("q: %s\n", q)
return dbobj.getListDo(q, keyValue)
}
func (dbobj dbcon) getListDo(q string, keyValue string) ([]bson.M, error) {
tx, err := dbobj.db.Begin()
if err != nil {
return nil, err

View File

@@ -288,7 +288,7 @@ func (e mainEnv) userLoginEnter(w http.ResponseWriter, r *http.Request, ps httpr
userTOKEN := userBson["token"].(string)
event.Record = userTOKEN
fmt.Printf("Found user record: %s\n", userTOKEN)
fmt.Printf("Found user record: %s\n", userBson)
tmpCode := userBson["tempcode"].(int32)
if tmp == tmpCode {
// user ented correct key