summaryrefslogtreecommitdiff
blob: 56e673c1b429072c493005f83ee300bcadcecdf2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Contains the model of the application data

package models

import (
	"glsamaker/pkg/config"
	"glsamaker/pkg/database/connection"
	"time"
)

type ApplicationSetting struct {
	Key        string `pg:",pk"`
	Value      string
	LastUpdate time.Time
	//LastBugUpdate time.Time
	//LastCVEUpdate time.Time
}

type GlobalSettings struct {
	LastBugUpdate       time.Time
	LastCVEUpdate       time.Time
	Version             string
	Force2FALogin       bool
	Force2FAGLSARelease bool
}

func GetApplicationKey(key string) *ApplicationSetting {
	applicationData := &ApplicationSetting{Key: key}
	connection.DB.Model(applicationData).WherePK().Select()
	return applicationData
}

func SetApplicationValue(key string, value string) {
	applicationData := &ApplicationSetting{
		Key:        key,
		Value:      value,
		LastUpdate: time.Now(),
	}

	connection.DB.Model(applicationData).WherePK().OnConflict("(key) DO Update").Insert()
}

func SeedApplicationValue(key string, value string) {
	applicationData := &ApplicationSetting{
		Key:        key,
		Value:      value,
		LastUpdate: time.Now(),
	}

	connection.DB.Model(applicationData).WherePK().OnConflict("(key) DO Nothing").Insert()
}

func GetDefaultGlobalSettings() *GlobalSettings {
	return &GlobalSettings{
		LastBugUpdate:       GetApplicationKey("LastBugUpdate").LastUpdate,
		LastCVEUpdate:       GetApplicationKey("LastCVEUpdate").LastUpdate,
		Version:             GetApplicationKey("Version").Value,
		Force2FALogin:       GetApplicationKey("Force2FALogin").Value == "1",
		Force2FAGLSARelease: GetApplicationKey("Force2FAGLSARelease").Value == "1",
	}
}

func SeedInitialApplicationData() {
	SeedApplicationValue("LastBugUpdate", "")
	SeedApplicationValue("LastCVEUpdate", "")
	SeedApplicationValue("Version", config.Version())
	SeedApplicationValue("Force2FALogin", "0")
	SeedApplicationValue("Force2FAGLSARelease", "0")
}