-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.resolvers.go
150 lines (123 loc) · 4.53 KB
/
user.resolvers.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package graph
// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.
// Code generated by github.com/99designs/gqlgen version v0.17.44
import (
"context"
"strings"
"github.com/navikt/nada-backend/pkg/auth"
"github.com/navikt/nada-backend/pkg/graph/generated"
"github.com/navikt/nada-backend/pkg/graph/models"
)
// UserInfo is the resolver for the userInfo field.
func (r *queryResolver) UserInfo(ctx context.Context) (*models.UserInfo, error) {
user := auth.GetUser(ctx)
groups := []*models.Group{}
for _, g := range user.GoogleGroups {
groups = append(groups, &models.Group{
Name: g.Name,
Email: g.Email,
})
}
return &models.UserInfo{
Name: user.Name,
Email: user.Email,
Groups: groups,
LoginExpiration: user.Expiry,
}, nil
}
// GoogleGroups is the resolver for the googleGroups field.
func (r *userInfoResolver) GoogleGroups(ctx context.Context, obj *models.UserInfo) ([]*models.Group, error) {
return obj.Groups, nil
}
// AllGoogleGroups is the resolver for the allGoogleGroups field.
func (r *userInfoResolver) AllGoogleGroups(ctx context.Context, obj *models.UserInfo) ([]*models.Group, error) {
user := auth.GetUser(ctx)
groups := []*models.Group{}
for _, g := range user.AllGoogleGroups {
groups = append(groups, &models.Group{
Name: g.Name,
Email: g.Email,
})
}
return groups, nil
}
// AzureGroups is the resolver for the azureGroups field.
func (r *userInfoResolver) AzureGroups(ctx context.Context, obj *models.UserInfo) ([]*models.Group, error) {
user := auth.GetUser(ctx)
groups := []*models.Group{}
for _, g := range user.AzureGroups {
groups = append(groups, &models.Group{
Name: g.Name,
Email: g.Email,
})
}
return groups, nil
}
// GCPProjects is the resolver for the gcpProjects field.
func (r *userInfoResolver) GCPProjects(ctx context.Context, obj *models.UserInfo) ([]*models.GCPProject, error) {
user := auth.GetUser(ctx)
ret := []*models.GCPProject{}
for _, grp := range user.GoogleGroups {
proj, ok := r.gcpProjects.Get(grp.Email)
if !ok {
continue
}
ret = append(ret, &models.GCPProject{
ID: proj,
Group: &models.Group{
Name: grp.Name,
Email: grp.Email,
},
})
}
return ret, nil
}
// NadaTokens is the resolver for the nadaTokens field.
func (r *userInfoResolver) NadaTokens(ctx context.Context, obj *models.UserInfo) ([]*models.NadaToken, error) {
teams := teamNamesFromGroups(obj.Groups)
return r.repo.GetNadaTokensForTeams(ctx, teams)
}
// Dataproducts is the resolver for the dataproducts field.
func (r *userInfoResolver) Dataproducts(ctx context.Context, obj *models.UserInfo) ([]*models.Dataproduct, error) {
user := auth.GetUser(ctx)
return r.repo.GetDataproductsByGroups(ctx, user.GoogleGroups.Emails())
}
// Accessable is the resolver for the accessable field.
func (r *userInfoResolver) Accessable(ctx context.Context, obj *models.UserInfo) (*models.AccessibleDatasets, error) {
user := auth.GetUser(ctx)
ownedDatasets, err := r.repo.GetDatasetsForOwner(ctx, user.GoogleGroups.Emails())
if err != nil {
return nil, err
}
grantedDatasets, err := r.repo.GetDatasetsByUserAccess(ctx, "user:"+user.Email)
if err != nil {
return nil, err
}
return &models.AccessibleDatasets{
Owned: ownedDatasets,
Granted: grantedDatasets,
}, nil
}
// Stories is the resolver for the stories field.
func (r *userInfoResolver) Stories(ctx context.Context, obj *models.UserInfo) ([]*models.Story, error) {
user := auth.GetUser(ctx)
return r.repo.GetStoriesByGroups(ctx, user.GoogleGroups.Emails())
}
// InsightProducts is the resolver for the insightProducts field.
func (r *userInfoResolver) InsightProducts(ctx context.Context, obj *models.UserInfo) ([]*models.InsightProduct, error) {
user := auth.GetUser(ctx)
return r.repo.GetInsightProductsByGroups(ctx, user.GoogleGroups.Emails())
}
// AccessRequests is the resolver for the accessRequests field.
func (r *userInfoResolver) AccessRequests(ctx context.Context, obj *models.UserInfo) ([]*models.AccessRequest, error) {
user := auth.GetUser(ctx)
groups := []string{"user:" + strings.ToLower(user.Email)}
for _, g := range user.GoogleGroups {
groups = append(groups, "group:"+strings.ToLower(g.Email))
}
return r.repo.ListAccessRequestsForOwner(ctx, groups)
}
// UserInfo returns generated.UserInfoResolver implementation.
func (r *Resolver) UserInfo() generated.UserInfoResolver { return &userInfoResolver{r} }
type userInfoResolver struct{ *Resolver }