github.com/infraboard/keyauth@v0.8.1/apps/policy/impl/query.go (about) 1 package impl 2 3 import ( 4 "fmt" 5 6 "github.com/infraboard/mcube/exception" 7 "go.mongodb.org/mongo-driver/bson" 8 "go.mongodb.org/mongo-driver/mongo/options" 9 10 "github.com/infraboard/keyauth/apps/policy" 11 ) 12 13 func newDescribePolicyRequest(req *policy.DescribePolicyRequest) (*describePolicyRequest, error) { 14 if err := req.Validate(); err != nil { 15 return nil, exception.NewBadRequest(err.Error()) 16 } 17 return &describePolicyRequest{req}, nil 18 } 19 20 type describePolicyRequest struct { 21 *policy.DescribePolicyRequest 22 } 23 24 func (req *describePolicyRequest) String() string { 25 return fmt.Sprintf("policy: %s", req.Id) 26 } 27 28 func (req *describePolicyRequest) FindFilter() bson.M { 29 filter := bson.M{} 30 if req.Id != "" { 31 filter["_id"] = req.Id 32 } 33 return filter 34 } 35 36 func newQueryPolicyRequest(req *policy.QueryPolicyRequest) (*queryPolicyRequest, error) { 37 if err := req.Validate(); err != nil { 38 return nil, exception.NewBadRequest(err.Error()) 39 } 40 41 return &queryPolicyRequest{ 42 QueryPolicyRequest: req, 43 }, nil 44 } 45 46 type queryPolicyRequest struct { 47 *policy.QueryPolicyRequest 48 } 49 50 func (r *queryPolicyRequest) FindOptions() *options.FindOptions { 51 pageSize := int64(r.Page.PageSize) 52 skip := int64(r.Page.PageSize) * int64(r.Page.PageNumber-1) 53 54 opt := &options.FindOptions{ 55 Sort: bson.D{{Key: "create_at", Value: -1}}, 56 Limit: &pageSize, 57 Skip: &skip, 58 } 59 60 return opt 61 } 62 63 func (r *queryPolicyRequest) FindFilter() bson.M { 64 filter := bson.M{} 65 if r.Domain != "" { 66 filter["domain"] = r.Domain 67 } 68 69 if r.NamespaceId != "" { 70 filter["namespace_id"] = r.NamespaceId 71 } 72 if r.RoleId != "" { 73 filter["role_id"] = r.RoleId 74 } 75 if r.Account != "" { 76 filter["account"] = r.Account 77 } 78 if r.Type != policy.PolicyType_NULL { 79 filter["type"] = r.Type 80 } 81 82 return filter 83 } 84 85 func newDeletePolicyRequest(req *policy.DeletePolicyRequest) (*deletePolicyRequest, error) { 86 return &deletePolicyRequest{ 87 DeletePolicyRequest: req, 88 }, nil 89 } 90 91 type deletePolicyRequest struct { 92 *policy.DeletePolicyRequest 93 } 94 95 func (r *deletePolicyRequest) FindFilter() bson.M { 96 filter := bson.M{} 97 filter["domain"] = r.Domain 98 99 if r.Id != "" { 100 filter["_id"] = r.Id 101 } 102 if r.Account != "" { 103 filter["account"] = r.Account 104 } 105 if r.RoleId != "" { 106 filter["role_id"] = r.RoleId 107 } 108 if r.NamespaceId != "" { 109 filter["namespace_id"] = r.NamespaceId 110 } 111 if r.Type != policy.PolicyType_NULL { 112 filter["type"] = r.Type 113 } 114 115 return filter 116 }