-
Notifications
You must be signed in to change notification settings - Fork 3
/
sqlite.go
77 lines (67 loc) · 2.3 KB
/
sqlite.go
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
70
71
72
73
74
75
76
77
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/glauth/glauth/v2/pkg/handler"
"github.com/glauth/glauth/v2/pkg/plugins"
)
type SqliteBackend struct {
}
func NewSQLiteHandler(opts ...handler.Option) handler.Handler {
backend := SqliteBackend{}
return plugins.NewDatabaseHandler(backend, opts...)
}
func (b SqliteBackend) GetDriverName() string {
return "sqlite3"
}
func (b SqliteBackend) GetPrepareSymbol() string {
return "?"
}
// Create db/schema if necessary
func (b SqliteBackend) CreateSchema(db *sql.DB) {
statement, _ := db.Prepare(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
uidnumber INTEGER NOT NULL,
primarygroup INTEGER NOT NULL,
othergroups TEXT DEFAULT '',
givenname TEXT DEFAULT '',
sn TEXT DEFAULT '',
mail TEXT DEFAULT '',
loginshell TYEXT DEFAULT '',
homedirectory TEXT DEFAULT '',
disabled SMALLINT DEFAULT 0,
passsha256 TEXT DEFAULT '',
passbcrypt TEXT DEFAULT '',
otpsecret TEXT DEFAULT '',
yubikey TEXT DEFAULT '',
sshkeys TEXT DEFAULT '',
custattr TEXT DEFAULT '{}')
`)
statement.Exec()
statement, _ = db.Prepare("CREATE UNIQUE INDEX IF NOT EXISTS idx_user_name on users(name)")
statement.Exec()
statement, _ = db.Prepare("CREATE TABLE IF NOT EXISTS ldapgroups (id INTEGER PRIMARY KEY, name TEXT NOT NULL, gidnumber INTEGER NOT NULL)")
statement.Exec()
statement, _ = db.Prepare("CREATE UNIQUE INDEX IF NOT EXISTS idx_group_name on ldapgroups(name)")
statement.Exec()
statement, _ = db.Prepare("CREATE TABLE IF NOT EXISTS includegroups (id INTEGER PRIMARY KEY, parentgroupid INTEGER NOT NULL, includegroupid INTEGER NOT NULL)")
statement.Exec()
statement, _ = db.Prepare("CREATE TABLE IF NOT EXISTS capabilities (id INTEGER PRIMARY KEY, userid INTEGER NOT NULL, action TEXT NOT NULL, object TEXT NOT NULL)")
statement.Exec()
}
// Migrate schema if necessary
func (b SqliteBackend) MigrateSchema(db *sql.DB, checker func(*sql.DB, string, string) bool) {
if !checker(db, "users", "sshkeys") {
statement, _ := db.Prepare("ALTER TABLE users ADD COLUMN sshkeys TEXT DEFAULT ''")
statement.Exec()
}
if checker(db, "groups", "name") {
statement, _ := db.Prepare("DROP TABLE ldapgroups")
statement.Exec()
statement, _ = db.Prepare("ALTER TABLE groups RENAME TO ldapgroups")
statement.Exec()
}
}
func main() {}