Skip to content

Commit b3c25e7

Browse files
authored
Generate helpers in ServicePermissions (#193)
1 parent 9fc9757 commit b3c25e7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+338
-48
lines changed

generate/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ go-mocks:
1717
--user $$(id -u):$$(id -g) \
1818
-w /work \
1919
-v $(PWD):/work \
20-
vektra/mockery:v2.52.3 --keeptree --inpackage --dir go --output go/tests/mocks --all --log-level debug
20+
vektra/mockery:v2.53.0 --keeptree --inpackage --dir go --output go/tests/mocks --all --log-level debug

generate/generate.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@ func servicePermissions(root string) (*permissions.ServicePermissions, error) {
112112
serverReflectionInfov1alpha1: true,
113113
serverReflectionInfo: true,
114114
},
115-
Self: map[string]bool{},
115+
Self: map[string]bool{},
116+
Admin: map[string]bool{},
117+
Tenant: map[string]bool{},
118+
Project: map[string]bool{},
116119
}
117120
chargeable = permissions.Chargeable{}
118121
auditable = permissions.Auditable{}
@@ -149,28 +152,37 @@ func servicePermissions(root string) (*permissions.ServicePermissions, error) {
149152
switch *methodOpt.IdentifierValue {
150153
case v1.TenantRole_TENANT_ROLE_OWNER.String():
151154
roles.Tenant[v1.TenantRole_TENANT_ROLE_OWNER.String()] = append(roles.Tenant[v1.TenantRole_TENANT_ROLE_OWNER.String()], methodName)
155+
visibility.Tenant[methodName] = true
152156
case v1.TenantRole_TENANT_ROLE_EDITOR.String():
153157
roles.Tenant[v1.TenantRole_TENANT_ROLE_EDITOR.String()] = append(roles.Tenant[v1.TenantRole_TENANT_ROLE_EDITOR.String()], methodName)
158+
visibility.Tenant[methodName] = true
154159
case v1.TenantRole_TENANT_ROLE_VIEWER.String():
155160
roles.Tenant[v1.TenantRole_TENANT_ROLE_VIEWER.String()] = append(roles.Tenant[v1.TenantRole_TENANT_ROLE_VIEWER.String()], methodName)
161+
visibility.Tenant[methodName] = true
156162
case v1.TenantRole_TENANT_ROLE_GUEST.String():
157163
roles.Tenant[v1.TenantRole_TENANT_ROLE_GUEST.String()] = append(roles.Tenant[v1.TenantRole_TENANT_ROLE_GUEST.String()], methodName)
164+
visibility.Tenant[methodName] = true
158165
case v1.TenantRole_TENANT_ROLE_UNSPECIFIED.String():
159166
// noop
160167
// Project
161168
case v1.ProjectRole_PROJECT_ROLE_OWNER.String():
162169
roles.Project[v1.ProjectRole_PROJECT_ROLE_OWNER.String()] = append(roles.Project[v1.ProjectRole_PROJECT_ROLE_OWNER.String()], methodName)
170+
visibility.Project[methodName] = true
163171
case v1.ProjectRole_PROJECT_ROLE_EDITOR.String():
164172
roles.Project[v1.ProjectRole_PROJECT_ROLE_EDITOR.String()] = append(roles.Project[v1.ProjectRole_PROJECT_ROLE_EDITOR.String()], methodName)
173+
visibility.Project[methodName] = true
165174
case v1.ProjectRole_PROJECT_ROLE_VIEWER.String():
166175
roles.Project[v1.ProjectRole_PROJECT_ROLE_VIEWER.String()] = append(roles.Project[v1.ProjectRole_PROJECT_ROLE_VIEWER.String()], methodName)
176+
visibility.Project[methodName] = true
167177
case v1.ProjectRole_PROJECT_ROLE_UNSPECIFIED.String():
168178
// noop
169179
// Admin
170180
case v1.AdminRole_ADMIN_ROLE_EDITOR.String():
171181
roles.Admin[v1.AdminRole_ADMIN_ROLE_EDITOR.String()] = append(roles.Admin[v1.AdminRole_ADMIN_ROLE_EDITOR.String()], methodName)
182+
visibility.Admin[methodName] = true
172183
case v1.AdminRole_ADMIN_ROLE_VIEWER.String():
173184
roles.Admin[v1.AdminRole_ADMIN_ROLE_VIEWER.String()] = append(roles.Admin[v1.AdminRole_ADMIN_ROLE_VIEWER.String()], methodName)
185+
visibility.Admin[methodName] = true
174186
case v1.AdminRole_ADMIN_ROLE_UNSPECIFIED.String():
175187
// noop
176188
// Visibility

generate/go_servicepermissions.tpl

+76
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Code generated discover.go. DO NOT EDIT.
22
package permissions
33

4+
import (
5+
"connectrpc.com/connect"
6+
)
7+
48
func GetServices() []string {
59
return []string{
610
{{- range $s := .Services }}
@@ -54,6 +58,21 @@ func GetServicePermissions() *ServicePermissions {
5458
Self: map[string]bool{
5559
{{- range $key, $value := .Visibility.Self }}
5660
"{{ $key }}": {{ $value }} ,
61+
{{- end }}
62+
},
63+
Admin: map[string]bool{
64+
{{- range $key, $value := .Visibility.Admin }}
65+
"{{ $key }}": {{ $value }} ,
66+
{{- end }}
67+
},
68+
Tenant: map[string]bool{
69+
{{- range $key, $value := .Visibility.Tenant }}
70+
"{{ $key }}": {{ $value }} ,
71+
{{- end }}
72+
},
73+
Project: map[string]bool{
74+
{{- range $key, $value := .Visibility.Project }}
75+
"{{ $key }}": {{ $value }} ,
5776
{{- end }}
5877
},
5978
},
@@ -69,3 +88,60 @@ func GetServicePermissions() *ServicePermissions {
6988
},
7089
}
7190
}
91+
92+
func IsPublicScope(req connect.AnyRequest) bool {
93+
_, ok := GetServicePermissions().Visibility.Public[req.Spec().Procedure]
94+
return ok
95+
}
96+
97+
func IsSelfScope(req connect.AnyRequest) bool {
98+
_, ok := GetServicePermissions().Visibility.Self[req.Spec().Procedure]
99+
return ok
100+
}
101+
102+
func IsAdminScope(req connect.AnyRequest) bool {
103+
_, ok := GetServicePermissions().Visibility.Admin[req.Spec().Procedure]
104+
return ok
105+
}
106+
107+
func IsTenantScope(req connect.AnyRequest) bool {
108+
_, ok := GetServicePermissions().Visibility.Tenant[req.Spec().Procedure]
109+
return ok
110+
}
111+
112+
func IsProjectScope(req connect.AnyRequest) bool {
113+
_, ok := GetServicePermissions().Visibility.Project[req.Spec().Procedure]
114+
return ok
115+
}
116+
117+
func IsChargeable(req connect.AnyRequest) bool {
118+
_, ok := GetServicePermissions().Chargeable[req.Spec().Procedure]
119+
return ok
120+
}
121+
122+
func IsAuditable(req connect.AnyRequest) bool {
123+
_, ok := GetServicePermissions().Auditable[req.Spec().Procedure]
124+
return ok
125+
}
126+
127+
func GetTenantFromRequest(req connect.AnyRequest) (string, bool) {
128+
if !IsTenantScope(req) {
129+
return "", false
130+
}
131+
switch rq := req.Any().(type) {
132+
case interface{ GetLogin() string }:
133+
return rq.GetLogin(), true
134+
}
135+
return "", false
136+
}
137+
138+
func GetProjectFromRequest(req connect.AnyRequest) (string, bool) {
139+
if !IsProjectScope(req) {
140+
return "", false
141+
}
142+
switch rq := req.Any().(type) {
143+
case interface{ GetProject() string }:
144+
return rq.GetProject(), true
145+
}
146+
return "", false
147+
}

go/permissions/permissions.go

+3
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,7 @@ type Roles struct {
3333
type Visibility struct {
3434
Public map[string]bool `json:"public,omitempty"`
3535
Self map[string]bool `json:"self,omitempty"`
36+
Admin map[string]bool `json:"admin,omitempty"`
37+
Tenant map[string]bool `json:"tenant,omitempty"`
38+
Project map[string]bool `json:"project,omitempty"`
3639
}

go/permissions/servicepermissions.go

+130
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/ClusterServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/ClusterServiceHandler.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/PaymentServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/PaymentServiceHandler.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/ProjectServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/ProjectServiceHandler.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/StorageServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/StorageServiceHandler.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/TenantServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/TenantServiceHandler.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/TokenServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/admin/v1/adminv1connect/TokenServiceHandler.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/api/v1/apiv1connect/AssetServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/api/v1/apiv1connect/AssetServiceHandler.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go/tests/mocks/api/v1/apiv1connect/ClusterServiceClient.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)