-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdpr.go
51 lines (42 loc) · 1.1 KB
/
gdpr.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
package views
import (
"encoding/json"
"log"
"net/http"
"github.com/tnyie/tny-api/models"
"github.com/tnyie/tny-api/util"
)
// GetUserGDPR returns a gdpr dump to the user
func GetUserGDPR(w http.ResponseWriter, r *http.Request) {
log.Println("User fetching GDPR data")
userAuth, authenticated, _ := util.CheckLogin(r, "")
if !authenticated || userAuth.UID == "" {
w.WriteHeader(http.StatusUnauthorized)
log.Println("Failed to authenticate user")
return
}
user := &models.User{UID: userAuth.UID}
if err := user.Get(); err != nil {
w.WriteHeader(http.StatusUnauthorized)
log.Println("Failed to fetch user data")
return
}
links, err := models.GetLinksByUser(user.UID)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
log.Println("Failed to get user links\n", err)
return
}
gdprData := &models.GDPRData{
UserData: *user,
UserAuthData: *userAuth,
Links: *links,
}
encoded, err := json.Marshal(gdprData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
log.Println("Failed to encode gdpr data")
return
}
respondJSON(w, encoded, http.StatusOK)
}