Skip to content

Commit 87ecbe9

Browse files
authored
Merge pull request #8 from zhangxinghao/main
fix: update to casbin v2.35.0
2 parents 965e7ec + 1d25a4f commit 87ecbe9

File tree

11 files changed

+697
-351
lines changed

11 files changed

+697
-351
lines changed

command/command.pb.go

Lines changed: 174 additions & 72 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

command/command.proto

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,22 @@ message UpdatePoliciesRequest {
4141
repeated StringArray oldRules = 4;
4242
}
4343

44+
message UpdateFilteredPoliciesRequest {
45+
string sec = 1;
46+
string pType = 2;
47+
repeated StringArray newRules = 3;
48+
repeated StringArray oldRules = 4;
49+
}
50+
4451
message Command {
4552
enum Type {
4653
COMMAND_TYPE_ADD_POLICIES = 0;
4754
COMMAND_TYPE_REMOVE_POLICIES = 1;
4855
COMMAND_TYPE_REMOVE_FILTERED_POLICY = 2;
4956
COMMAND_TYPE_UPDATE_POLICY = 3;
5057
COMMAND_TYPE_UPDATE_POLICIES = 4;
51-
5258
COMMAND_TYPE_CLEAR_POLICY = 5;
59+
COMMAND_TYPE_UPDATE_FILTERED_POLICIES = 6;
5360
}
5461

5562
Type type = 1;

dispatcher.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"context"
55
"crypto/tls"
66
"fmt"
7-
"github.com/soheilhy/cmux"
87
"net"
98

9+
"github.com/soheilhy/cmux"
10+
1011
"github.com/hashicorp/go-multierror"
1112

1213
"github.com/casbin/casbin/v2/persist"
@@ -253,6 +254,28 @@ func (h *HRaftDispatcher) UpdatePolicy(sec string, pType string, oldRule, newRul
253254
return h.httpService.DoUpdatePolicyRequest(request)
254255
}
255256

257+
// UpdateFilteredPolicies implements the persist.Dispatcher interface.
258+
func (h *HRaftDispatcher) UpdateFilteredPolicies(sec string, pType string, oldRules, newRules [][]string) error {
259+
var olds []*command.StringArray
260+
for _, rule := range oldRules {
261+
var item = &command.StringArray{Items: rule}
262+
olds = append(olds, item)
263+
}
264+
265+
var news []*command.StringArray
266+
for _, rule := range newRules {
267+
var item = &command.StringArray{Items: rule}
268+
news = append(news, item)
269+
}
270+
request := &command.UpdateFilteredPoliciesRequest{
271+
Sec: sec,
272+
PType: pType,
273+
OldRules: olds,
274+
NewRules: news,
275+
}
276+
return h.httpService.DoUpdateFilteredPoliciesRequest(request)
277+
}
278+
256279
// UpdatePolicies implements the persist.Dispatcher interface.
257280
func (h *HRaftDispatcher) UpdatePolicies(sec string, pType string, oldRules, newRules [][]string) error {
258281
var olds []*command.StringArray

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.15
44

55
require (
66
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
7-
github.com/casbin/casbin/v2 v2.24.0
7+
github.com/casbin/casbin/v2 v2.35.0
88
github.com/cenkalti/backoff/v4 v4.1.0
99
github.com/go-chi/chi v1.5.1
1010
github.com/golang/mock v1.4.4

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ github.com/casbin/casbin/v2 v2.23.4 h1:izvAG3KA49C3/m1zpYfkLcZlkYQO5VeHj7dhwurwZ
1313
github.com/casbin/casbin/v2 v2.23.4/go.mod h1:wUgota0cQbTXE6Vd+KWpg41726jFRi7upxio0sR+Xd0=
1414
github.com/casbin/casbin/v2 v2.24.0 h1:peiFTw+PNpAMSz7hDDz768nEwzUsBMMZNV4yFGCayI8=
1515
github.com/casbin/casbin/v2 v2.24.0/go.mod h1:wUgota0cQbTXE6Vd+KWpg41726jFRi7upxio0sR+Xd0=
16+
github.com/casbin/casbin/v2 v2.35.0 h1:f0prVg9LgTJTihjAxWEZhfJptXvah1GpZh12sb5KXNA=
17+
github.com/casbin/casbin/v2 v2.35.0/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
1618
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
1719
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
1820
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=

http/mocks/mock_store.go

Lines changed: 84 additions & 69 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

http/service.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type Store interface {
4141
UpdatePolicy(request *command.UpdatePolicyRequest) error
4242
// UpdatePolicies updates a set of rules of policy.
4343
UpdatePolicies(request *command.UpdatePoliciesRequest) error
44+
// UpdateFilteredPolicies updates a set of rules of policy.
45+
UpdateFilteredPolicies(request *command.UpdateFilteredPoliciesRequest) error
4446
// ClearPolicy clears all policies.
4547
ClearPolicy() error
4648

@@ -290,6 +292,20 @@ func (s *Service) handleUpdatePolicy(w http.ResponseWriter, r *http.Request) {
290292
}
291293
err = s.store.UpdatePolicies(&cmd)
292294
s.handleStoreResponse(err, w, r)
295+
case "filtered":
296+
data, err := ioutil.ReadAll(r.Body)
297+
if err != nil {
298+
http.Error(w, err.Error(), http.StatusBadRequest)
299+
return
300+
}
301+
var cmd command.UpdateFilteredPoliciesRequest
302+
err = jsoniter.Unmarshal(data, &cmd)
303+
if err != nil {
304+
http.Error(w, err.Error(), http.StatusBadRequest)
305+
return
306+
}
307+
err = s.store.UpdateFilteredPolicies(&cmd)
308+
s.handleStoreResponse(err, w, r)
293309
case "":
294310
data, err := ioutil.ReadAll(r.Body)
295311
if err != nil {
@@ -448,6 +464,26 @@ func (s *Service) DoUpdatePolicyRequest(request *command.UpdatePolicyRequest) er
448464
return nil
449465
}
450466

467+
func (s *Service) DoUpdateFilteredPoliciesRequest(request *command.UpdateFilteredPoliciesRequest) error {
468+
b, err := jsoniter.Marshal(request)
469+
if err != nil {
470+
return err
471+
}
472+
r, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s://%s/policies/update?type=filtered", s.GetScheme(), s.Addr()), bytes.NewBuffer(b))
473+
if err != nil {
474+
return err
475+
}
476+
477+
resp, err := s.httpClient.Do(r)
478+
if err != nil {
479+
return err
480+
}
481+
if resp.StatusCode != http.StatusOK {
482+
return errors.New(http.StatusText(http.StatusServiceUnavailable))
483+
}
484+
return nil
485+
}
486+
451487
func (s *Service) DoUpdatePoliciesRequest(request *command.UpdatePoliciesRequest) error {
452488
b, err := jsoniter.Marshal(request)
453489
if err != nil {

0 commit comments

Comments
 (0)