-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathacl.go
130 lines (109 loc) · 3.28 KB
/
acl.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
package directus
import (
"encoding/json"
"github.com/perimeterx/marshmallow"
)
type Role struct {
ID string `json:"id,omitempty"`
Icon Icon `json:"icon,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
AdminAccess bool `json:"admin_access"`
AppAccess bool `json:"app_access"`
Users []string `json:"users,omitempty"`
}
type User struct {
ID string `json:"id,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Email string `json:"email,omitempty"`
Role string `json:"role,omitempty"`
Policies []string `json:"policies,omitempty"`
Provider string `json:"provider,omitempty"`
ExternalIdentifier string `json:"external_identifier,omitempty"`
}
type PermissionAction string
const (
PermissionActionCreate PermissionAction = "create"
PermissionActionRead PermissionAction = "read"
PermissionActionUpdate PermissionAction = "update"
PermissionActionDelete PermissionAction = "delete"
)
func (action *PermissionAction) UnmarshalJSON(data []byte) error {
var str string
if err := json.Unmarshal(data, &str); err != nil {
return err
}
*action = PermissionAction(str)
return nil
}
func (action *PermissionAction) MarshalJSON() ([]byte, error) {
return json.Marshal(string(*action))
}
type Permission struct {
ID int64 `json:"id,omitempty"`
Policy Nullable[string] `json:"policy"`
Collection string `json:"collection"`
Action PermissionAction `json:"action"`
Fields Nullable[[]string] `json:"fields"`
System bool `json:"system,omitempty"`
Unknown map[string]any `json:"-"`
}
func (permission *Permission) UnmarshalJSON(data []byte) error {
values, err := marshmallow.Unmarshal(data, permission, marshmallow.WithExcludeKnownFieldsFromMap(true))
if err != nil {
return err
}
permission.Unknown = values
return nil
}
func (permission *Permission) MarshalJSON() ([]byte, error) {
type alias Permission
base, err := json.Marshal((*alias)(permission))
if err != nil {
return nil, err
}
m := make(map[string]any)
for k, v := range permission.Unknown {
m[k] = v
}
if err := json.Unmarshal(base, &m); err != nil {
return nil, err
}
return json.Marshal(m)
}
type Policy struct {
ID string `json:"id,omitempty"`
Icon Icon `json:"icon,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
AdminAccess bool `json:"admin_access"`
AppAccess bool `json:"app_access"`
Users []string `json:"users,omitempty"`
Roles []string `json:"roles,omitempty"`
Permissions []int64 `json:"permissions,omitempty"`
Unknown map[string]any `json:"-"`
}
func (policy *Policy) UnmarshalJSON(data []byte) error {
values, err := marshmallow.Unmarshal(data, policy, marshmallow.WithExcludeKnownFieldsFromMap(true))
if err != nil {
return err
}
policy.Unknown = values
return nil
}
func (policy *Policy) MarshalJSON() ([]byte, error) {
type alias Policy
base, err := json.Marshal((*alias)(policy))
if err != nil {
return nil, err
}
m := make(map[string]any)
for k, v := range policy.Unknown {
m[k] = v
}
if err := json.Unmarshal(base, &m); err != nil {
return nil, err
}
return json.Marshal(m)
}