-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
127 lines (112 loc) · 2.92 KB
/
util.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"context"
_ "embed"
"encoding/json"
"net/http"
"os"
"github.com/charmbracelet/log"
"github.com/clerk/clerk-sdk-go/v2"
"github.com/clerk/clerk-sdk-go/v2/user"
_ "github.com/jackc/pgx/v5"
"github.com/joshtenorio/glassypdm-server/internal/dal"
)
func IsServerOpen() bool {
if os.Getenv("OPEN_TEAMS") == "1" {
return true
} else {
return false
}
}
func getVersion(w http.ResponseWriter, r *http.Request) {
data := struct {
Version string `json:"version"`
}{}
data.Version = os.Getenv("CLIENT_VERSION")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
func getConfig(w http.ResponseWriter, r *http.Request) {
data := struct {
Key string `json:"clerk_publickey"`
Name string `json:"name"`
}{}
data.Key = os.Getenv("CLERK_PUBLICKEY")
data.Name = os.Getenv("SERVER_NAME")
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
func GetUserIDByEmail(email string) string {
ctx := context.Background()
param := user.ListParams{EmailAddresses: []string{email}}
// we expect only one user per email
res, err := user.List(ctx, ¶m)
// FIXME handle error
if err != nil || len(res.Users) > 1 {
return ""
} else if len(res.Users) == 0 {
return ""
}
userid := ""
for _, user := range res.Users {
userid = user.ID
}
return userid
}
// checks if a user can generally upload files
// doesn't check permission for specific projects/teams
func canUserUpload(userId string) bool {
ctx := context.Background()
// check team permission
// TODO: ensure that a team has at least one project for this to be a valid check
teampermissions, err := dal.Queries.FindTeamPermissions(ctx, userId)
if err != nil {
return false
}
for _, level := range teampermissions {
if level >= 2 {
return true
}
}
// check permission groups
groups, err := dal.Queries.FindUserInPermissionGroup(ctx, userId)
if err != nil {
return false
}
if len(groups) > 0 {
return true
}
return false
}
type User struct {
UserId string `json:"user_id"`
Name string `json:"name"`
EmailId string `json:"email_id"`
}
func GetUserByID(userId string) (User, bool) {
ctx := context.Background()
var output User
usr, err := user.Get(ctx, userId)
if err != nil {
log.Error("couldn't find user", "user", userId, "error", err.Error())
return output, false
}
output.UserId = userId
output.Name = *usr.FirstName + " " + *usr.LastName
output.EmailId = *usr.PrimaryEmailAddressID
return output, true
}
func FindUserInList(userId string, list []*clerk.User) (User, bool) {
var output User
for _, user := range list {
//log.Info("searching in list:", "supplied", userId, "current", user.ID)
if userId == user.ID {
output.UserId = userId
output.Name = *user.FirstName + " " + *user.LastName
output.EmailId = *user.PrimaryEmailAddressID
return output, true
}
}
log.Error("couldn't find user in list", "id", userId)
return output, false
}