Skip to content

Commit

Permalink
Merge pull request #8 from zhangxinghao/main
Browse files Browse the repository at this point in the history
fix: update to casbin v2.35.0
  • Loading branch information
nodece committed Aug 10, 2021
2 parents 965e7ec + 1d25a4f commit 87ecbe9
Show file tree
Hide file tree
Showing 11 changed files with 697 additions and 351 deletions.
246 changes: 174 additions & 72 deletions command/command.pb.go

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion command/command.proto
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,22 @@ message UpdatePoliciesRequest {
repeated StringArray oldRules = 4;
}

message UpdateFilteredPoliciesRequest {
string sec = 1;
string pType = 2;
repeated StringArray newRules = 3;
repeated StringArray oldRules = 4;
}

message Command {
enum Type {
COMMAND_TYPE_ADD_POLICIES = 0;
COMMAND_TYPE_REMOVE_POLICIES = 1;
COMMAND_TYPE_REMOVE_FILTERED_POLICY = 2;
COMMAND_TYPE_UPDATE_POLICY = 3;
COMMAND_TYPE_UPDATE_POLICIES = 4;

COMMAND_TYPE_CLEAR_POLICY = 5;
COMMAND_TYPE_UPDATE_FILTERED_POLICIES = 6;
}

Type type = 1;
Expand Down
25 changes: 24 additions & 1 deletion dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"context"
"crypto/tls"
"fmt"
"github.com/soheilhy/cmux"
"net"

"github.com/soheilhy/cmux"

"github.com/hashicorp/go-multierror"

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

// UpdateFilteredPolicies implements the persist.Dispatcher interface.
func (h *HRaftDispatcher) UpdateFilteredPolicies(sec string, pType string, oldRules, newRules [][]string) error {
var olds []*command.StringArray
for _, rule := range oldRules {
var item = &command.StringArray{Items: rule}
olds = append(olds, item)
}

var news []*command.StringArray
for _, rule := range newRules {
var item = &command.StringArray{Items: rule}
news = append(news, item)
}
request := &command.UpdateFilteredPoliciesRequest{
Sec: sec,
PType: pType,
OldRules: olds,
NewRules: news,
}
return h.httpService.DoUpdateFilteredPoliciesRequest(request)
}

// UpdatePolicies implements the persist.Dispatcher interface.
func (h *HRaftDispatcher) UpdatePolicies(sec string, pType string, oldRules, newRules [][]string) error {
var olds []*command.StringArray
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.15

require (
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
github.com/casbin/casbin/v2 v2.24.0
github.com/casbin/casbin/v2 v2.35.0
github.com/cenkalti/backoff/v4 v4.1.0
github.com/go-chi/chi v1.5.1
github.com/golang/mock v1.4.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/casbin/casbin/v2 v2.23.4 h1:izvAG3KA49C3/m1zpYfkLcZlkYQO5VeHj7dhwurwZ
github.com/casbin/casbin/v2 v2.23.4/go.mod h1:wUgota0cQbTXE6Vd+KWpg41726jFRi7upxio0sR+Xd0=
github.com/casbin/casbin/v2 v2.24.0 h1:peiFTw+PNpAMSz7hDDz768nEwzUsBMMZNV4yFGCayI8=
github.com/casbin/casbin/v2 v2.24.0/go.mod h1:wUgota0cQbTXE6Vd+KWpg41726jFRi7upxio0sR+Xd0=
github.com/casbin/casbin/v2 v2.35.0 h1:f0prVg9LgTJTihjAxWEZhfJptXvah1GpZh12sb5KXNA=
github.com/casbin/casbin/v2 v2.35.0/go.mod h1:vByNa/Fchek0KZUgG5wEsl7iFsiviAYKRtgrQfcJqHg=
github.com/cenkalti/backoff/v4 v4.1.0 h1:c8LkOFQTzuO0WBM/ae5HdGQuZPfPxp7lqBRwQRm4fSc=
github.com/cenkalti/backoff/v4 v4.1.0/go.mod h1:scbssz8iZGpm3xbr14ovlUdkxfGXNInqkPWOWmG2CLw=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down
153 changes: 84 additions & 69 deletions http/mocks/mock_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type Store interface {
UpdatePolicy(request *command.UpdatePolicyRequest) error
// UpdatePolicies updates a set of rules of policy.
UpdatePolicies(request *command.UpdatePoliciesRequest) error
// UpdateFilteredPolicies updates a set of rules of policy.
UpdateFilteredPolicies(request *command.UpdateFilteredPoliciesRequest) error
// ClearPolicy clears all policies.
ClearPolicy() error

Expand Down Expand Up @@ -290,6 +292,20 @@ func (s *Service) handleUpdatePolicy(w http.ResponseWriter, r *http.Request) {
}
err = s.store.UpdatePolicies(&cmd)
s.handleStoreResponse(err, w, r)
case "filtered":
data, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var cmd command.UpdateFilteredPoliciesRequest
err = jsoniter.Unmarshal(data, &cmd)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = s.store.UpdateFilteredPolicies(&cmd)
s.handleStoreResponse(err, w, r)
case "":
data, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand Down Expand Up @@ -448,6 +464,26 @@ func (s *Service) DoUpdatePolicyRequest(request *command.UpdatePolicyRequest) er
return nil
}

func (s *Service) DoUpdateFilteredPoliciesRequest(request *command.UpdateFilteredPoliciesRequest) error {
b, err := jsoniter.Marshal(request)
if err != nil {
return err
}
r, err := http.NewRequest(http.MethodPut, fmt.Sprintf("%s://%s/policies/update?type=filtered", s.GetScheme(), s.Addr()), bytes.NewBuffer(b))
if err != nil {
return err
}

resp, err := s.httpClient.Do(r)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return errors.New(http.StatusText(http.StatusServiceUnavailable))
}
return nil
}

func (s *Service) DoUpdatePoliciesRequest(request *command.UpdatePoliciesRequest) error {
b, err := jsoniter.Marshal(request)
if err != nil {
Expand Down
Loading

0 comments on commit 87ecbe9

Please sign in to comment.