From 8ff62dc55a9b21db47b005970564fdd1b636bfaf Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Mon, 23 May 2022 14:04:01 +0700 Subject: [PATCH 01/13] Authentication module --- .gitignore | 2 ++ authentication.go | 71 ++++++++++++++++++++++++++++++++++++++++++ constants.go | 6 ++++ go.mod | 14 +++++++++ go.sum | 78 +++++++++++++++++++++++++++++++++++++++++++++++ request.go | 15 +++++++++ struct.go | 48 +++++++++++++++++++++++++++++ 7 files changed, 234 insertions(+) create mode 100644 authentication.go create mode 100644 constants.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 request.go create mode 100644 struct.go diff --git a/.gitignore b/.gitignore index 66fd13c..398baf2 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ + +.idea diff --git a/authentication.go b/authentication.go new file mode 100644 index 0000000..4e747f5 --- /dev/null +++ b/authentication.go @@ -0,0 +1,71 @@ +package authentication + +import ( + "encoding/json" + "errors" + "fmt" + "github.com/Selly-Modules/natsio" +) + +// Client ... +type Client struct { + Config Config + natsServer natsio.Server + natsJetStream natsio.JetStream + Request Request +} + +var ( + client *Client +) + +// NewClient +// Init client elasticsearch +func NewClient(config Config) (*Client, error) { + if config.ApiKey == "" { + return nil, errors.New("api key is required") + } + if config.Nats.URL == "" { + return nil, errors.New("nats url is required") + } + if err := natsio.Connect(config.Nats); err != nil { + return nil, fmt.Errorf("nats connect failed: %v", err) + } + + client = &Client{ + Config: config, + natsServer: natsio.GetServer(), + natsJetStream: natsio.GetJetStream(), + Request: Request{}, + } + + return client, nil +} + +// requestNats +// publish message to nats and waiting response +func requestNats(subject string, data []byte) (*Response, error) { + var ( + req = RequestBody{ + ApiKey: client.Config.ApiKey, + Body: data, + } + res *Response + ) + msg, err := client.natsServer.Request(subject, toBytes(req)) + if err != nil { + return nil, err + } + if err = json.Unmarshal(msg.Data, &res); err != nil { + return nil, err + } + if res.Message != "" { + return nil, errors.New(res.Message) + } + return res, nil +} + +func toBytes(data interface{}) []byte { + b, _ := json.Marshal(data) + return b +} diff --git a/constants.go b/constants.go new file mode 100644 index 0000000..f3c95f9 --- /dev/null +++ b/constants.go @@ -0,0 +1,6 @@ +package authentication + +const ( + SubjectRequestNatsCheckPermission = "authentication.request.staff.check_permission" + SubjectRequestNatsGetPermission = "authentication.request.staff.list_permission" +) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..146903a --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module github.com/Selly-Modules/authentication + +go 1.17 + +require github.com/Selly-Modules/natsio v0.0.0-20220419025827-e72bc82991bf + +require ( + github.com/logrusorgru/aurora v2.0.3+incompatible // indirect + github.com/nats-io/nats.go v1.13.0 // indirect + github.com/nats-io/nkeys v0.3.0 // indirect + github.com/nats-io/nuid v1.0.1 // indirect + github.com/thoas/go-funk v0.9.1 // indirect + golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a6ef717 --- /dev/null +++ b/go.sum @@ -0,0 +1,78 @@ +github.com/Selly-Modules/natsio v0.0.0-20220419025827-e72bc82991bf h1:EryKoDKmVTGHLBIUFGYqDxSZ/WtzyGAUK6skbGBcbG8= +github.com/Selly-Modules/natsio v0.0.0-20220419025827-e72bc82991bf/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= +github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= +github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= +github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= +github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= +github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= +github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= +github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= +github.com/nats-io/jwt v1.2.2 h1:w3GMTO969dFg+UOKTmmyuu7IGdusK+7Ytlt//OYH/uU= +github.com/nats-io/jwt v1.2.2/go.mod h1:/xX356yQA6LuXI9xWW7mZNpxgF2mBmGecH+Fj34sP5Q= +github.com/nats-io/jwt/v2 v2.0.3 h1:i/O6cmIsjpcQyWDYNcq2JyZ3/VTF8SJ4JWluI5OhpvI= +github.com/nats-io/jwt/v2 v2.0.3/go.mod h1:VRP+deawSXyhNjXmxPCHskrR6Mq50BqpEI5SEcNiGlY= +github.com/nats-io/nats-server/v2 v2.6.1 h1:cJy+ia7/4EaJL+ZYDmIy2rD1mDWTfckhtPBU0GYo8xM= +github.com/nats-io/nats-server/v2 v2.6.1/go.mod h1:Az91TbZiV7K4a6k/4v6YYdOKEoxCXj+iqhHVf/MlrKo= +github.com/nats-io/nats.go v1.12.3/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= +github.com/nats-io/nats.go v1.13.0 h1:LvYqRB5epIzZWQp6lmeltOOZNLqCvm4b+qfvzZO03HE= +github.com/nats-io/nats.go v1.13.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= +github.com/nats-io/nkeys v0.2.0/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s= +github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8= +github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4= +github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= +github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= +github.com/thoas/go-funk v0.9.1/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= +golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI= +golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= +google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= +google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM= +google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= +google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= +google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/request.go b/request.go new file mode 100644 index 0000000..88426ed --- /dev/null +++ b/request.go @@ -0,0 +1,15 @@ +package authentication + +// Request ... +type Request struct { +} + +// CheckPermission ... +func (Request) CheckPermission(payload StaffCheckPermissionBody) (*Response, error) { + return requestNats(SubjectRequestNatsCheckPermission, toBytes(payload)) +} + +// GetPermission ... +func (Request) GetPermission(payload StaffGetPermissions) (*Response, error) { + return requestNats(SubjectRequestNatsGetPermission, toBytes(payload)) +} diff --git a/struct.go b/struct.go new file mode 100644 index 0000000..83ed60e --- /dev/null +++ b/struct.go @@ -0,0 +1,48 @@ +package authentication + +import "github.com/Selly-Modules/natsio" + +// Config int client elasticsearch +type Config struct { + ApiKey string + Nats natsio.Config +} + +// StaffCheckPermissionBody ... +type StaffCheckPermissionBody struct { + StaffID string `json:"staffId"` + Permission string `json:"permission"` + Source string `json:"source"` + Code string `json:"code,omitempty"` +} + +// StaffCheckPermissionResponse ... +type StaffCheckPermissionResponse struct { + Message string `json:"message"` + IsValid bool `json:"isValid"` +} + +// StaffGetPermissions ... +type StaffGetPermissions struct { + StaffID string `json:"staffId"` + Source string `json:"source"` +} + +// StaffGetPermissionsResponse ... +type StaffGetPermissionsResponse struct { + Permissions []string `json:"permissions"` +} + +// RequestBody ... +type RequestBody struct { + Body []byte `json:"body"` + ApiKey string `json:"apiKey"` +} + +// Response +// response to service es +type Response struct { + Success bool `json:"success"` + Data []byte `json:"data,omitempty"` + Message string `json:"message"` +} From 2bbb1aec3f0a1be20ce5f812b7b2d5693893f97e Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Tue, 24 May 2022 10:14:51 +0700 Subject: [PATCH 02/13] [Update] add request sync staff --- constants.go | 1 + request.go | 5 +++++ struct.go | 25 ++++++++++++++++++++++++- 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/constants.go b/constants.go index f3c95f9..17ee75b 100644 --- a/constants.go +++ b/constants.go @@ -3,4 +3,5 @@ package authentication const ( SubjectRequestNatsCheckPermission = "authentication.request.staff.check_permission" SubjectRequestNatsGetPermission = "authentication.request.staff.list_permission" + SubjectRequestNatsSyncStaff = "authentication.request.staff.sync_data" ) diff --git a/request.go b/request.go index 88426ed..dc67886 100644 --- a/request.go +++ b/request.go @@ -13,3 +13,8 @@ func (Request) CheckPermission(payload StaffCheckPermissionBody) (*Response, err func (Request) GetPermission(payload StaffGetPermissions) (*Response, error) { return requestNats(SubjectRequestNatsGetPermission, toBytes(payload)) } + +// SyncData ... +func (Request) SyncData(payload ListStaff) (*Response, error) { + return requestNats(SubjectRequestNatsSyncStaff, toBytes(payload)) +} diff --git a/struct.go b/struct.go index 83ed60e..22fd526 100644 --- a/struct.go +++ b/struct.go @@ -1,6 +1,29 @@ package authentication -import "github.com/Selly-Modules/natsio" +import ( + "github.com/Selly-Modules/natsio" +) + +// Staff ... +type Staff struct { + ID string `json:"_id"` // ObjectID + Name string `json:"name"` + Phone string `json:"phone"` + Active bool `json:"active"` + Role string `json:"role,omitempty"` // ObjectID + Avatar []byte `json:"avatar,omitempty"` // FilePhoto + CreatedAt string `json:"createdAt"` // ISOString + UpdatedAt string `json:"updatedAt"` // ISOString + IsRoot bool `json:"isRoot"` + Permissions []string `json:"permissions"` + NotAllowedLoginAdmin bool `json:"notAllowedLoginAdmin"` + Source []string `json:"source"` +} + +// ListStaff ... +type ListStaff struct { + Staffs []Staff `json:"staffs"` +} // Config int client elasticsearch type Config struct { From 29bdeb7d40610ae6d202ff08db89aa16cd547b99 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Tue, 24 May 2022 14:31:45 +0700 Subject: [PATCH 03/13] [Update] add request sync roles --- constants.go | 1 + request.go | 5 +++++ struct.go | 14 ++++++++++++++ 3 files changed, 20 insertions(+) diff --git a/constants.go b/constants.go index 17ee75b..df612b2 100644 --- a/constants.go +++ b/constants.go @@ -4,4 +4,5 @@ const ( SubjectRequestNatsCheckPermission = "authentication.request.staff.check_permission" SubjectRequestNatsGetPermission = "authentication.request.staff.list_permission" SubjectRequestNatsSyncStaff = "authentication.request.staff.sync_data" + SubjectRequestNatsSyncRole = "authentication.request.role.sync_data" ) diff --git a/request.go b/request.go index dc67886..0dc0589 100644 --- a/request.go +++ b/request.go @@ -18,3 +18,8 @@ func (Request) GetPermission(payload StaffGetPermissions) (*Response, error) { func (Request) SyncData(payload ListStaff) (*Response, error) { return requestNats(SubjectRequestNatsSyncStaff, toBytes(payload)) } + +// SyncRoleData ... +func (Request) SyncRoleData(payload ListRole) (*Response, error) { + return requestNats(SubjectRequestNatsSyncRole, toBytes(payload)) +} diff --git a/struct.go b/struct.go index 22fd526..71bf10f 100644 --- a/struct.go +++ b/struct.go @@ -4,6 +4,20 @@ import ( "github.com/Selly-Modules/natsio" ) +// Role ... +type Role struct { + ID string `bson:"_id"` + Name string `bson:"name"` + CreatedAt string `bson:"createdAt"` + UpdatedAt string `bson:"updatedAt"` + Permissions []string `bson:"permissions"` +} + +// ListRole ... +type ListRole struct { + Roles []Role `json:"roles"` +} + // Staff ... type Staff struct { ID string `json:"_id"` // ObjectID From c5ebaa41ecf2101c9ac3c5e49fa9f732494d8441 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Wed, 25 May 2022 08:58:48 +0700 Subject: [PATCH 04/13] [Update] add payload request check permission --- struct.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/struct.go b/struct.go index 71bf10f..107c4fa 100644 --- a/struct.go +++ b/struct.go @@ -47,10 +47,18 @@ type Config struct { // StaffCheckPermissionBody ... type StaffCheckPermissionBody struct { - StaffID string `json:"staffId"` - Permission string `json:"permission"` - Source string `json:"source"` - Code string `json:"code,omitempty"` + StaffID string `json:"staffId"` + Permission string `json:"permission"` + Source string `json:"source"` + Code string `json:"code,omitempty"` + Payload PayloadCheckPermission `json:"payload"` +} + +// PayloadCheckPermission ... +type PayloadCheckPermission struct { + URL string `json:"url"` + Body string `json:"body"` + Method string `json:"method"` } // StaffCheckPermissionResponse ... From 853fd1be76d2b53c4bfecf2be0cc15496f209366 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Wed, 25 May 2022 10:46:34 +0700 Subject: [PATCH 05/13] [Update] add request create/update staff --- constants.go | 4 +++- go.mod | 5 ++++- go.sum | 39 +++++++++++++++++++++++++++++++++++---- request.go | 11 ++++++++--- struct.go | 35 +++++++++-------------------------- 5 files changed, 59 insertions(+), 35 deletions(-) diff --git a/constants.go b/constants.go index df612b2..ecf6372 100644 --- a/constants.go +++ b/constants.go @@ -4,5 +4,7 @@ const ( SubjectRequestNatsCheckPermission = "authentication.request.staff.check_permission" SubjectRequestNatsGetPermission = "authentication.request.staff.list_permission" SubjectRequestNatsSyncStaff = "authentication.request.staff.sync_data" - SubjectRequestNatsSyncRole = "authentication.request.role.sync_data" + + SubjectRequestNatsStaffCreate = "authentication.request.staff.create" + SubjectRequestNatsStaffUpdate = "authentication.request.staff.update" ) diff --git a/go.mod b/go.mod index 146903a..8c45610 100644 --- a/go.mod +++ b/go.mod @@ -2,9 +2,12 @@ module github.com/Selly-Modules/authentication go 1.17 -require github.com/Selly-Modules/natsio v0.0.0-20220419025827-e72bc82991bf +require ( + github.com/Selly-Modules/natsio v0.0.0-20220419025827-e72bc82991bf +) require ( + github.com/go-stack/stack v1.8.0 // indirect github.com/logrusorgru/aurora v2.0.3+incompatible // indirect github.com/nats-io/nats.go v1.13.0 // indirect github.com/nats-io/nkeys v0.3.0 // indirect diff --git a/go.sum b/go.sum index a6ef717..480d98d 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,10 @@ github.com/Selly-Modules/natsio v0.0.0-20220419025827-e72bc82991bf h1:EryKoDKmVTGHLBIUFGYqDxSZ/WtzyGAUK6skbGBcbG8= github.com/Selly-Modules/natsio v0.0.0-20220419025827-e72bc82991bf/go.mod h1:NG55g9ip18nvN5tfP6PcSEKec10/lOeIOZC8HqBVNlQ= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8= github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA= github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs= @@ -11,17 +14,25 @@ github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= +github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc= +github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= +github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/nats-io/jwt v1.2.2 h1:w3GMTO969dFg+UOKTmmyuu7IGdusK+7Ytlt//OYH/uU= github.com/nats-io/jwt v1.2.2/go.mod h1:/xX356yQA6LuXI9xWW7mZNpxgF2mBmGecH+Fj34sP5Q= github.com/nats-io/jwt/v2 v2.0.3 h1:i/O6cmIsjpcQyWDYNcq2JyZ3/VTF8SJ4JWluI5OhpvI= @@ -36,32 +47,50 @@ github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8= github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4= github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/thoas/go-funk v0.9.1 h1:O549iLZqPpTUQ10ykd26sZhzD+rmR5pWhuElrhbC20M= github.com/thoas/go-funk v0.9.1/go.mod h1:+IWnUfUmFO1+WVYQWQtIJHeRRdaIyyYglZN7xzUPe4Q= +github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= +github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= +github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= +github.com/xdg-go/scram v1.0.2/go.mod h1:1WAq6h33pAW+iRreB34OORO2Nf7qel3VV3fjBj+hCSs= +github.com/xdg-go/stringprep v1.0.2/go.mod h1:8F9zXuvzgwmyT5DUm4GUfZGDdT3W+LCvS6+da4O5kxM= +github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= +go.mongodb.org/mongo-driver v1.9.1 h1:m078y9v7sBItkt1aaoe2YlvWEXcD263e1a4E1fBrJ1c= +go.mongodb.org/mongo-driver v1.9.1/go.mod h1:0sQWfOeY63QTntERDJJ/0SuKK0T1uVSgKCuAROlKEPY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20201216223049-8b5274cf687f/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e h1:gsTQYXdTw2Gq7RBsWvlQ91b+aEQ6bXFUngBGuR8sPpI= golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI= golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190531172133-b3315ee88b7d/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8= google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0= @@ -74,5 +103,7 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/request.go b/request.go index 0dc0589..0add8ea 100644 --- a/request.go +++ b/request.go @@ -19,7 +19,12 @@ func (Request) SyncData(payload ListStaff) (*Response, error) { return requestNats(SubjectRequestNatsSyncStaff, toBytes(payload)) } -// SyncRoleData ... -func (Request) SyncRoleData(payload ListRole) (*Response, error) { - return requestNats(SubjectRequestNatsSyncRole, toBytes(payload)) +// CreateStaff ... +func (Request) CreateStaff(payload Staff) (*Response, error) { + return requestNats(SubjectRequestNatsStaffCreate, toBytes(payload)) +} + +// UpdateStaff ... +func (Request) UpdateStaff(payload Staff) (*Response, error) { + return requestNats(SubjectRequestNatsStaffUpdate, toBytes(payload)) } diff --git a/struct.go b/struct.go index 107c4fa..61eab5b 100644 --- a/struct.go +++ b/struct.go @@ -4,34 +4,17 @@ import ( "github.com/Selly-Modules/natsio" ) -// Role ... -type Role struct { - ID string `bson:"_id"` - Name string `bson:"name"` - CreatedAt string `bson:"createdAt"` - UpdatedAt string `bson:"updatedAt"` - Permissions []string `bson:"permissions"` -} - -// ListRole ... -type ListRole struct { - Roles []Role `json:"roles"` -} - // Staff ... type Staff struct { - ID string `json:"_id"` // ObjectID - Name string `json:"name"` - Phone string `json:"phone"` - Active bool `json:"active"` - Role string `json:"role,omitempty"` // ObjectID - Avatar []byte `json:"avatar,omitempty"` // FilePhoto - CreatedAt string `json:"createdAt"` // ISOString - UpdatedAt string `json:"updatedAt"` // ISOString - IsRoot bool `json:"isRoot"` - Permissions []string `json:"permissions"` - NotAllowedLoginAdmin bool `json:"notAllowedLoginAdmin"` - Source []string `json:"source"` + ID string `json:"_id"` // ObjectID + Name string `json:"name"` + Phone string `json:"phone"` + Active bool `json:"active"` + CreatedAt string `json:"createdAt"` // ISOString + UpdatedAt string `json:"updatedAt"` // ISOString + IsRoot bool `json:"isRoot"` + Permissions []string `json:"permissions"` + Source string `json:"source"` } // ListStaff ... From 0162caddba99ed24361f64b662a5f8893b42adf9 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Wed, 25 May 2022 13:57:11 +0700 Subject: [PATCH 06/13] [Update] add request create/update staff --- struct.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/struct.go b/struct.go index 61eab5b..6ce5c9b 100644 --- a/struct.go +++ b/struct.go @@ -6,12 +6,10 @@ import ( // Staff ... type Staff struct { - ID string `json:"_id"` // ObjectID + Reference string `json:"reference"` // ObjectID Name string `json:"name"` Phone string `json:"phone"` Active bool `json:"active"` - CreatedAt string `json:"createdAt"` // ISOString - UpdatedAt string `json:"updatedAt"` // ISOString IsRoot bool `json:"isRoot"` Permissions []string `json:"permissions"` Source string `json:"source"` From f79d7d53b1268c7ae3f04d4f82e09c1b7c90a2d8 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Thu, 26 May 2022 09:30:35 +0700 Subject: [PATCH 07/13] [Update] Payload staff sync data --- struct.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/struct.go b/struct.go index 6ce5c9b..6335567 100644 --- a/struct.go +++ b/struct.go @@ -8,6 +8,7 @@ import ( type Staff struct { Reference string `json:"reference"` // ObjectID Name string `json:"name"` + Email string `json:"email"` Phone string `json:"phone"` Active bool `json:"active"` IsRoot bool `json:"isRoot"` @@ -33,6 +34,7 @@ type StaffCheckPermissionBody struct { Source string `json:"source"` Code string `json:"code,omitempty"` Payload PayloadCheckPermission `json:"payload"` + IsRoot bool `json:"isRoot"` } // PayloadCheckPermission ... @@ -52,6 +54,7 @@ type StaffCheckPermissionResponse struct { type StaffGetPermissions struct { StaffID string `json:"staffId"` Source string `json:"source"` + Prefix string `json:"prefix"` } // StaffGetPermissionsResponse ... From 97147e9095049eb4f7b08dbf924c0cc0f66df221 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Thu, 26 May 2022 10:25:43 +0700 Subject: [PATCH 08/13] [Update] Payload staff sync data --- struct.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/struct.go b/struct.go index 6335567..158a355 100644 --- a/struct.go +++ b/struct.go @@ -30,11 +30,12 @@ type Config struct { // StaffCheckPermissionBody ... type StaffCheckPermissionBody struct { StaffID string `json:"staffId"` + Token string `json:"token"` Permission string `json:"permission"` Source string `json:"source"` Code string `json:"code,omitempty"` Payload PayloadCheckPermission `json:"payload"` - IsRoot bool `json:"isRoot"` + IsRoot string `json:"isRoot"` } // PayloadCheckPermission ... From 7dd77f3e317658d49db3cb7973fcab42e33c3177 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Thu, 26 May 2022 21:33:30 +0700 Subject: [PATCH 09/13] [Update] Payload staff sync data --- struct.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/struct.go b/struct.go index 158a355..e4b0bff 100644 --- a/struct.go +++ b/struct.go @@ -31,7 +31,8 @@ type Config struct { type StaffCheckPermissionBody struct { StaffID string `json:"staffId"` Token string `json:"token"` - Permission string `json:"permission"` + Permission []string `json:"permission"` + Action string `json:"action"` Source string `json:"source"` Code string `json:"code,omitempty"` Payload PayloadCheckPermission `json:"payload"` From cc8184fe69f003f6be7e35d2518e70d584f69a4d Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Fri, 27 May 2022 14:45:10 +0700 Subject: [PATCH 10/13] [Update] Source --- constants.go | 2 ++ request.go | 5 +++++ struct.go | 26 +++++++++++++++++--------- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/constants.go b/constants.go index ecf6372..e2974f5 100644 --- a/constants.go +++ b/constants.go @@ -7,4 +7,6 @@ const ( SubjectRequestNatsStaffCreate = "authentication.request.staff.create" SubjectRequestNatsStaffUpdate = "authentication.request.staff.update" + + SubjectRequestNatsAddLogs = "authentication.request.logs.create" ) diff --git a/request.go b/request.go index 0add8ea..f79a6db 100644 --- a/request.go +++ b/request.go @@ -28,3 +28,8 @@ func (Request) CreateStaff(payload Staff) (*Response, error) { func (Request) UpdateStaff(payload Staff) (*Response, error) { return requestNats(SubjectRequestNatsStaffUpdate, toBytes(payload)) } + +// SaveLog ... +func (Request) SaveLog(payload Log) (*Response, error) { + return requestNats(SubjectRequestNatsAddLogs, toBytes(payload)) +} diff --git a/struct.go b/struct.go index e4b0bff..d60db39 100644 --- a/struct.go +++ b/struct.go @@ -4,6 +4,15 @@ import ( "github.com/Selly-Modules/natsio" ) +// Log ... +type Log struct { + Reference string `json:"reference"` + Payload PayloadCheckPermission `json:"payload"` + Source string `json:"source"` + Permission []string `json:"permission"` + Action string `json:"action"` +} + // Staff ... type Staff struct { Reference string `json:"reference"` // ObjectID @@ -13,7 +22,7 @@ type Staff struct { Active bool `json:"active"` IsRoot bool `json:"isRoot"` Permissions []string `json:"permissions"` - Source string `json:"source"` + Source []string `json:"source"` } // ListStaff ... @@ -29,14 +38,13 @@ type Config struct { // StaffCheckPermissionBody ... type StaffCheckPermissionBody struct { - StaffID string `json:"staffId"` - Token string `json:"token"` - Permission []string `json:"permission"` - Action string `json:"action"` - Source string `json:"source"` - Code string `json:"code,omitempty"` - Payload PayloadCheckPermission `json:"payload"` - IsRoot string `json:"isRoot"` + StaffID string `json:"staffId"` + Token string `json:"token"` + Permission []string `json:"permission"` + Action string `json:"action"` + Source string `json:"source"` + Code string `json:"code,omitempty"` + IsRoot string `json:"isRoot"` } // PayloadCheckPermission ... From 4007238604e74bc6047c81981cbaf2b27a26a8a0 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Mon, 30 May 2022 10:10:33 +0700 Subject: [PATCH 11/13] [Update] Get list logs --- go.mod | 1 + struct.go | 28 +++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 8c45610..8c63f56 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.17 require ( github.com/Selly-Modules/natsio v0.0.0-20220419025827-e72bc82991bf + go.mongodb.org/mongo-driver v1.9.1 ) require ( diff --git a/struct.go b/struct.go index d60db39..d5e5c4a 100644 --- a/struct.go +++ b/struct.go @@ -2,8 +2,18 @@ package authentication import ( "github.com/Selly-Modules/natsio" + "go.mongodb.org/mongo-driver/bson" ) +// CommonQuery ... +type CommonQuery struct { + Page int64 `json:"page"` + Limit int64 `json:"limit"` + Keyword string `json:"keyword"` + Sort bson.D `json:"sort"` + StaffID string `json:"staffID"` +} + // Log ... type Log struct { Reference string `json:"reference"` @@ -13,6 +23,23 @@ type Log struct { Action string `json:"action"` } +// LogsResponse ... +type LogsResponse struct { + ID string `json:"_id"` + Staff string `json:"staff"` + Payload PayloadCheckPermission `json:"payload"` + Action string `json:"action"` + CreatedAt string `json:"createdAt"` +} + +// ListPageResponse ... +type ListPageResponse struct { + Data []interface{} `json:"data"` + Total int64 `json:"total"` + Page int64 `json:"page"` + Limit int64 `json:"limit"` +} + // Staff ... type Staff struct { Reference string `json:"reference"` // ObjectID @@ -41,7 +68,6 @@ type StaffCheckPermissionBody struct { StaffID string `json:"staffId"` Token string `json:"token"` Permission []string `json:"permission"` - Action string `json:"action"` Source string `json:"source"` Code string `json:"code,omitempty"` IsRoot string `json:"isRoot"` From fdaf53d56c4a50bc4366619c0583d3089bfbfe63 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Mon, 30 May 2022 10:21:34 +0700 Subject: [PATCH 12/13] [Update] Get list logs --- constants.go | 3 ++- request.go | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/constants.go b/constants.go index e2974f5..eefc318 100644 --- a/constants.go +++ b/constants.go @@ -8,5 +8,6 @@ const ( SubjectRequestNatsStaffCreate = "authentication.request.staff.create" SubjectRequestNatsStaffUpdate = "authentication.request.staff.update" - SubjectRequestNatsAddLogs = "authentication.request.logs.create" + SubjectRequestNatsAddLogs = "authentication.request.logs.create" + SubjectRequestNatsLogsGetList = "authentication.request.logs.list" ) diff --git a/request.go b/request.go index f79a6db..d24f6b0 100644 --- a/request.go +++ b/request.go @@ -33,3 +33,8 @@ func (Request) UpdateStaff(payload Staff) (*Response, error) { func (Request) SaveLog(payload Log) (*Response, error) { return requestNats(SubjectRequestNatsAddLogs, toBytes(payload)) } + +// GetListLogs ... +func (Request) GetListLogs(payload Log) (*Response, error) { + return requestNats(SubjectRequestNatsLogsGetList, toBytes(payload)) +} From d0fd48667d2276be74eb80c35c795877bae62b0d Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Mon, 30 May 2022 10:34:44 +0700 Subject: [PATCH 13/13] [Update] Get list logs --- request.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/request.go b/request.go index d24f6b0..1bddcf1 100644 --- a/request.go +++ b/request.go @@ -35,6 +35,6 @@ func (Request) SaveLog(payload Log) (*Response, error) { } // GetListLogs ... -func (Request) GetListLogs(payload Log) (*Response, error) { +func (Request) GetListLogs(payload *CommonQuery) (*Response, error) { return requestNats(SubjectRequestNatsLogsGetList, toBytes(payload)) }