admin&sync: insert format helper in database
This commit is contained in:
parent
971273a185
commit
2a6fbd4e32
6 changed files with 22 additions and 17 deletions
|
|
@ -160,6 +160,7 @@ CREATE TABLE IF NOT EXISTS exercice_keys(
|
|||
id_key INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||
id_exercice INTEGER NOT NULL,
|
||||
type VARCHAR(255) NOT NULL,
|
||||
help VARCHAR(255) NOT NULL,
|
||||
cksum BINARY(64) NOT NULL,
|
||||
FOREIGN KEY(id_exercice) REFERENCES exercices(id_exercice)
|
||||
) DEFAULT CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
|
||||
|
|
|
|||
|
|
@ -290,7 +290,7 @@ func (f EFile) GetDepends() ([]Key, error) {
|
|||
if err := rows.Scan(&d); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deps = append(deps, Key{d, f.IdExercice, "", []byte{}})
|
||||
deps = append(deps, Key{d, f.IdExercice, "", "", []byte{}})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -14,13 +14,15 @@ type Key struct {
|
|||
IdExercice int64 `json:"idExercice"`
|
||||
// Label is the title of the flag as displayed to players
|
||||
Label string `json:"label"`
|
||||
// Help is a small piece of text that aims to add useful information like flag format, ...
|
||||
Help string `json:"help"`
|
||||
// Checksum is the expected hashed flag
|
||||
Checksum []byte `json:"value"`
|
||||
}
|
||||
|
||||
// GetKeys returns a list of flags comming with the challenge.
|
||||
func (e Exercice) GetKeys() ([]Key, error) {
|
||||
if rows, err := DBQuery("SELECT id_key, id_exercice, type, cksum FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil {
|
||||
if rows, err := DBQuery("SELECT id_key, id_exercice, type, help, cksum FROM exercice_keys WHERE id_exercice = ?", e.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
defer rows.Close()
|
||||
|
|
@ -30,7 +32,7 @@ func (e Exercice) GetKeys() ([]Key, error) {
|
|||
var k Key
|
||||
k.IdExercice = e.Id
|
||||
|
||||
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Label, &k.Checksum); err != nil {
|
||||
if err := rows.Scan(&k.Id, &k.IdExercice, &k.Label, &k.Help, &k.Checksum); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys = append(keys, k)
|
||||
|
|
@ -50,25 +52,25 @@ func getHashedKey(raw_value string) [blake2b.Size]byte {
|
|||
}
|
||||
|
||||
// AddRawKey creates and fills a new struct Key, from a non-hashed flag, and registers it into the database.
|
||||
func (e Exercice) AddRawKey(name string, raw_value string) (Key, error) {
|
||||
func (e Exercice) AddRawKey(name string, help string, raw_value string) (Key, error) {
|
||||
hash := getHashedKey(raw_value)
|
||||
return e.AddKey(name, hash[:])
|
||||
return e.AddKey(name, help, hash[:])
|
||||
}
|
||||
|
||||
// AddKey creates and fills a new struct Key, from a hashed flag, and registers it into the database.
|
||||
func (e Exercice) AddKey(name string, checksum []byte) (Key, error) {
|
||||
if res, err := DBExec("INSERT INTO exercice_keys (id_exercice, type, cksum) VALUES (?, ?, ?)", e.Id, name, checksum); err != nil {
|
||||
func (e Exercice) AddKey(name string, help string, checksum []byte) (Key, error) {
|
||||
if res, err := DBExec("INSERT INTO exercice_keys (id_exercice, type, help, cksum) VALUES (?, ?, ?, ?)", e.Id, name, help, checksum); err != nil {
|
||||
return Key{}, err
|
||||
} else if kid, err := res.LastInsertId(); err != nil {
|
||||
return Key{}, err
|
||||
} else {
|
||||
return Key{kid, e.Id, name, checksum}, nil
|
||||
return Key{kid, e.Id, name, help, checksum}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// Update applies modifications back to the database.
|
||||
func (k Key) Update() (int64, error) {
|
||||
if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Label, k.Checksum, k.Id); err != nil {
|
||||
if res, err := DBExec("UPDATE exercice_keys SET id_exercice = ?, type = ?, help = ?, cksum = ? WHERE id_key = ?", k.IdExercice, k.Label, k.Help, k.Checksum, k.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
|
|
|
|||
Reference in a new issue