This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
adapter.go
77 lines (71 loc) · 1.91 KB
/
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
package casbin
import (
"errors"
"github.com/casbin/casbin/model"
"github.com/orange-cloudfoundry/gobis"
"net/http"
)
type GobisAdapter struct {
policies []CasbinPolicy
}
func NewGobisAdapter() *GobisAdapter {
return &GobisAdapter{policies: make([]CasbinPolicy, 0)}
}
func (a *GobisAdapter) LoadPolicy(model model.Model) error {
for _, policy := range a.policies {
err := a.loadPolicy(policy, model)
if err != nil {
return err
}
}
return nil
}
func (a *GobisAdapter) loadPolicy(policy CasbinPolicy, model model.Model) error {
tokens := []string{policy.Sub, policy.Obj}
if policy.Act != "" {
tokens = append(tokens, policy.Act)
}
key := policy.Type
sec := key[:1]
model[sec][key].Policy = append(
model[sec][key].Policy,
tokens,
)
return nil
}
func (a GobisAdapter) SavePolicy(model model.Model) error {
return nil
}
func (a *GobisAdapter) AddPolicies(policies ...CasbinPolicy) {
a.policies = append(a.policies, policies...)
}
func (a *GobisAdapter) AddPoliciesFromRequest(req *http.Request) {
user := gobis.Username(req)
groups := gobis.Groups(req)
for _, group := range groups {
a.AddPolicies(CasbinPolicy{
Type: "g",
Sub: user,
Obj: group,
})
}
var ctxPolicies *[]CasbinPolicy
gobis.InjectContextValue(req, PolicyContextKey, &ctxPolicies)
if ctxPolicies == nil {
return
}
for _, ctxPolicy := range *ctxPolicies {
a.AddPolicies(ctxPolicy)
}
}
func (a *GobisAdapter) AddPolicy(sec string, ptype string, rule []string) error {
return errors.New("not implemented")
}
// RemovePolicy removes a policy rule from the storage.
func (a *GobisAdapter) 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 *GobisAdapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
return errors.New("not implemented")
}