This repository has been archived by the owner on Aug 28, 2023. It is now read-only.
/
tCloseSession.go
97 lines (86 loc) · 1.63 KB
/
tCloseSession.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package dgraph
import (
"context"
"encoding/json"
"github.com/dgraph-io/dgo/protos/api"
"github.com/romshark/dgraph_graphql_go/api/graph/auth"
strerr "github.com/romshark/dgraph_graphql_go/store/errors"
)
// CloseSession closes the given session
func (str *impl) CloseSession(
ctx context.Context,
key string,
) (
result bool,
err error,
) {
result = true
// Begin transaction
txn, close := str.txn(&err)
if err != nil {
return
}
defer close()
// Find the session, its owner and the global reference node
var qr struct {
Session []Session `json:"session"`
}
err = txn.QueryVars(
ctx,
`query Session(
$key: string
) {
session(func: eq(Session.key, $key)) {
uid
Session.user {
uid
User.id
}
~sessions {
uid
}
}
}`,
map[string]string{
"$key": key,
},
&qr,
)
if err != nil {
return
}
if len(qr.Session) < 1 {
err = strerr.Newf(
strerr.ErrInvalidInput,
"session not found",
)
return
}
sess := qr.Session[0]
// Authorize client
if err = auth.Authorize(ctx, auth.IsOwner{
Owner: sess.User[0].ID,
}); err != nil {
return
}
var deleteJSON []byte
deleteJSON, err = json.Marshal([]interface{}{
// Delete the global "sessions" reference
sess.RSessions[0],
// Delete the "User.sessions" reference
struct {
UID string `json:"uid"`
UserSessions []UID `json:"User.sessions"`
}{
UID: sess.User[0].UID,
UserSessions: []UID{UID{NodeID: sess.UID}},
},
// Delete the actual Session node
UID{NodeID: sess.UID},
})
if err != nil {
return
}
_, err = txn.Mutation(ctx, &api.Mutation{DeleteJson: deleteJSON})
return
}