Skip to content

Commit

Permalink
Merge a775acf into bdf4688
Browse files Browse the repository at this point in the history
  • Loading branch information
Xianjie committed Mar 28, 2019
2 parents bdf4688 + a775acf commit 7f885ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
22 changes: 20 additions & 2 deletions ladon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
// A bunch of exemplary policies
var pols = []Policy{
&DefaultPolicy{
ID: "1",
ID: "0",
Description: `This policy allows max, peter, zac and ken to create, delete and get the listed resources,
but only if the client ip matches and the request states that they are the owner of those resources as well.`,
Subjects: []string{"max", "peter", "<zac|ken>"},
Expand All @@ -49,7 +49,7 @@ var pols = []Policy{
},
},
&DefaultPolicy{
ID: "2",
ID: "1",
Description: "This policy allows max to update any resource",
Subjects: []string{"max"},
Actions: []string{"update"},
Expand All @@ -64,6 +64,14 @@ var pols = []Policy{
Resources: []string{"<.*>"},
Effect: DenyAccess,
},
&DefaultPolicy{
ID: "2",
Description: "This policy denies max to broadcast any of the resources",
Subjects: []string{"max"},
Actions: []string{"random"},
Resources: []string{"<.*>"},
Effect: DenyAccess,
},
}

// Some test cases
Expand Down Expand Up @@ -157,6 +165,15 @@ func TestLadon(t *testing.T) {
require.Nil(t, warden.Manager.Create(pol))
}

for i := 0; i < len(pols); i++ {
polices, err := warden.Manager.GetAll(int64(1), int64(i))
require.NoError(t, err)
p, err := warden.Manager.Get(fmt.Sprintf("%d", i))
if err == nil {
AssertPolicyEqual(t, p, polices[0])
}
}

for k, c := range cases {
t.Run(fmt.Sprintf("case=%d-%s", k, c.description), func(t *testing.T) {

Expand All @@ -166,6 +183,7 @@ func TestLadon(t *testing.T) {
assert.Equal(t, c.expectErr, err != nil)
})
}

}

func TestLadonEmpty(t *testing.T) {
Expand Down
21 changes: 15 additions & 6 deletions manager/memory/manager_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

. "github.com/ory/ladon"
"github.com/ory/pagination"
"sort"
)

// MemoryManager is an in-memory (non-persistent) implementation of Manager.
Expand All @@ -52,16 +53,24 @@ func (m *MemoryManager) Update(policy Policy) error {

// GetAll returns all policies.
func (m *MemoryManager) GetAll(limit, offset int64) (Policies, error) {
ps := make(Policies, len(m.Policies))
keys := make([]string, len(m.Policies))
i := 0

for _, p := range m.Policies {
ps[i] = p
m.RLock()
for key := range m.Policies {
keys[i] = key
i++
}

start, end := pagination.Index(int(limit), int(offset), len(ps))
return ps[start:end], nil
start, end := pagination.Index(int(limit), int(offset), len(m.Policies))
sort.Strings(keys)
ps := make(Policies, len(keys[start:end]))
i = 0
for _, key := range keys[start:end] {
ps[i] = m.Policies[key]
i++
}
m.RUnlock()
return ps, nil
}

// Create a new pollicy to MemoryManager.
Expand Down

0 comments on commit 7f885ab

Please sign in to comment.