This repository has been archived by the owner on Nov 16, 2020. It is now read-only.
/
casbin_entity_adapter.go
83 lines (70 loc) · 2.77 KB
/
casbin_entity_adapter.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
///////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
///////////////////////////////////////////////////////////////////////
package identitymanager
import (
"context"
"fmt"
casbinModel "github.com/casbin/casbin/model"
"github.com/casbin/casbin/persist"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/vmware/dispatch/pkg/entity-store"
)
// CasbinEntityAdapter type
type CasbinEntityAdapter struct {
store entitystore.EntityStore
}
// NewCasbinEntityAdapter creates a new casbin entity adapter
func NewCasbinEntityAdapter(store entitystore.EntityStore) *CasbinEntityAdapter {
return &CasbinEntityAdapter{store: store}
}
// LoadPolicy loads a policy into the casbin entity adapter
func (a *CasbinEntityAdapter) LoadPolicy(model casbinModel.Model) error {
var policies []*Policy
// We ignore policy status since the status is only meaningful to the controllers.
opts := entitystore.Options{
Filter: entitystore.FilterExists(),
}
log.Debug("Reloading policies")
// The entity adapter loads policies across all orgs into the casbin enforcer. During policy check, the user-specified org-id in header along with other request attributes are validated with the enforcer.
err := a.store.ListGlobal(context.TODO(), opts, &policies)
for _, policy := range policies {
// Casbin authorization rules are of the form (org, subject, resource, action) and hence the need to iterate over all rule fields.
log.Debugf("Loading policy %s", policy.Name)
var global string
if policy.Global {
global = "y"
} else {
global = "n"
}
for _, rule := range policy.Rules {
for _, subject := range rule.Subjects {
for _, resource := range rule.Resources {
for _, action := range rule.Actions {
lineText := fmt.Sprintf("p, %s, %s, %s, %s, %s", global, policy.OrganizationID, subject, resource, action)
persist.LoadPolicyLine(lineText, model)
}
}
}
}
}
return err
}
// SavePolicy saves all policy rules to the storage.
func (a *CasbinEntityAdapter) SavePolicy(model casbinModel.Model) error {
return errors.New("not implemented")
}
// AddPolicy adds a policy rule to the storage.
func (a *CasbinEntityAdapter) AddPolicy(sec string, ptype string, rule []string) error {
return errors.New("not implemented")
}
// RemovePolicy removes a policy rule from the storage.
func (a *CasbinEntityAdapter) RemovePolicy(sec string, ptype string, rule []string) error {
return errors.New("not implemented")
}
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
func (a *CasbinEntityAdapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
return errors.New("not implemented")
}