-
Notifications
You must be signed in to change notification settings - Fork 126
/
schema_funcs.go
73 lines (64 loc) · 1.96 KB
/
schema_funcs.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
package types
import (
"net/http"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types/slice"
)
func (s *Schema) MustCustomizeField(name string, f func(f Field) Field) *Schema {
field, ok := s.ResourceFields[name]
if !ok {
panic("Failed to find field " + name + " on schema " + s.ID)
}
s.ResourceFields[name] = f(field)
return s
}
func (v *APIVersion) Equals(other *APIVersion) bool {
return v.Version == other.Version &&
v.Group == other.Group &&
v.Path == other.Path
}
func (s *Schema) CanList(context *APIContext) error {
if context == nil {
if slice.ContainsString(s.CollectionMethods, http.MethodGet) {
return nil
}
return httperror.NewAPIError(httperror.PermissionDenied, "can not list "+s.ID)
}
return context.AccessControl.CanList(context, s)
}
func (s *Schema) CanGet(context *APIContext) error {
if context == nil {
if slice.ContainsString(s.ResourceMethods, http.MethodGet) {
return nil
}
return httperror.NewAPIError(httperror.PermissionDenied, "can not get "+s.ID)
}
return context.AccessControl.CanGet(context, s)
}
func (s *Schema) CanCreate(context *APIContext) error {
if context == nil {
if slice.ContainsString(s.CollectionMethods, http.MethodPost) {
return nil
}
return httperror.NewAPIError(httperror.PermissionDenied, "can not create "+s.ID)
}
return context.AccessControl.CanCreate(context, s)
}
func (s *Schema) CanUpdate(context *APIContext) error {
if context == nil {
if slice.ContainsString(s.ResourceMethods, http.MethodPut) {
return nil
}
return httperror.NewAPIError(httperror.PermissionDenied, "can not update "+s.ID)
}
return context.AccessControl.CanUpdate(context, nil, s)
}
func (s *Schema) CanDelete(context *APIContext) error {
if context == nil {
if slice.ContainsString(s.ResourceMethods, http.MethodDelete) {
return nil
}
return httperror.NewAPIError(httperror.PermissionDenied, "can not delete "+s.ID)
}
return context.AccessControl.CanDelete(context, nil, s)
}