-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.go
88 lines (71 loc) · 1.54 KB
/
interface.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
package policy
import (
"context"
"time"
"github.com/go-playground/validator/v10"
request "github.com/infraboard/mcube/http/request"
"github.com/rs/xid"
)
const (
AppName = "policy"
)
var (
validate = validator.New()
)
type Service interface {
CreatePolicy(context.Context, *CreatePolicyRequest) (*Policy, error)
RPCServer
}
func NewQueryPolicyRequest() *QueryPolicyRequest {
return &QueryPolicyRequest{
Page: request.NewDefaultPageRequest(),
}
}
// role 名称的列表
func (s *PolicySet) Roles() (roles []string) {
for i := range s.Items {
roles = append(roles, s.Items[i].Spec.Role)
}
return
}
func (s *PolicySet) GetPolicyByRole(role string) *Policy {
for i := range s.Items {
if s.Items[i].Spec.Role == role {
return s.Items[i]
}
}
return nil
}
func NewPolicySet() *PolicySet {
return &PolicySet{
Items: []*Policy{},
}
}
func (s *PolicySet) Add(item *Policy) {
s.Items = append(s.Items, item)
}
func NewCreatePolicyRequest() *CreatePolicyRequest {
return &CreatePolicyRequest{}
}
func NewDefaultPolicy() *Policy {
return &Policy{
Spec: NewCreatePolicyRequest(),
}
}
//只检查参数的有无
func (req *CreatePolicyRequest) Validate() error {
return validate.Struct(req)
}
func NewPolicy(req *CreatePolicyRequest) (*Policy, error) {
if err := req.Validate(); err != nil {
return nil, err
}
return &Policy{
Id: xid.New().String(),
CreateAt: time.Now().UnixMilli(),
Spec: req,
}, nil
}
func NewValidatePermissionRequest() *ValidatePermissionRequest {
return &ValidatePermissionRequest{}
}