-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathallowed_services.go
61 lines (47 loc) · 1.55 KB
/
allowed_services.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
package honuadb
func (hdb *HonuaDB) AllowService(identity string, deviceId, serviceId int32) error {
const query = "INSERT INTO allowed_services(identity, device_id, service_id) VALUES ($1, $2, $3)"
_, err := hdb.psqlDB.Exec(query, identity, deviceId, serviceId)
return err
}
func (hdb *HonuaDB) ForbidService(identity string, deviceId, serviceId int32) error {
const query = "DELETE FROM allowed_services WHERE identity=$1 AND device_id=$2 AND service_id=$3;"
_, err := hdb.psqlDB.Exec(query, identity, deviceId, serviceId)
return err
}
func (hdb *HonuaDB) IsServiceAllowed(identity string, deviceId, serviceId int32) (bool, error) {
const query = "SELECT CASE WHEN EXISTS ( SELECT * FROM allowed_services WHERE identity = $1 AND device_id = $2 AND service_id = $3) THEN true ELSE false END"
rows, err := hdb.psqlDB.Query(query, identity, deviceId, serviceId)
if err != nil {
return false, err
}
var state bool = false
for rows.Next() {
err = rows.Scan(&state)
if err != nil {
rows.Close()
return false, err
}
}
rows.Close()
return state, nil
}
func (hdb *HonuaDB) GetIDsOfAllowedServices(identity string, deviceId int32) ([]int32, error) {
const query = "SELECT service_id FROM allowed_services WHERE identity=$1 AND device_id=$2 ORDER BY id;"
rows, err := hdb.psqlDB.Query(query, identity, deviceId)
if err != nil {
return nil, err
}
var ids []int32 = []int32{}
for rows.Next() {
var sID int32
err = rows.Scan(&sID)
if err != nil {
rows.Close()
return nil, err
}
ids = append(ids, sID)
}
rows.Close()
return ids, nil
}