Skip to content

Commit

Permalink
Resolves issues with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas committed Jan 18, 2018
1 parent 2bc33a2 commit 9592a00
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 88 deletions.
12 changes: 9 additions & 3 deletions Gopkg.lock

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

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@

[[constraint]]
name = "github.com/ory/ladon"
version = "0.8.4"
version = "0.8.8"

[[constraint]]
name = "github.com/pborman/uuid"
Expand Down
60 changes: 0 additions & 60 deletions pkg/paginator_test.go

This file was deleted.

5 changes: 3 additions & 2 deletions policy/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/ory/hydra/firewall"
"github.com/ory/hydra/pkg"
"github.com/ory/ladon"
"github.com/ory/pagination"
"github.com/pborman/uuid"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -102,8 +103,8 @@ func (h *Handler) List(w http.ResponseWriter, r *http.Request, _ httprouter.Para
return
}

limit, offset := pkg.ParsePagination(r, 500, 0, 1000)
policies, err := h.Manager.GetAll(limit, offset)
limit, offset := pagination.Parse(r, 500, 0, 1000)
policies, err := h.Manager.GetAll(int64(limit), int64(offset))
if err != nil {
h.H.WriteError(w, r, errors.WithStack(err))
return
Expand Down
8 changes: 4 additions & 4 deletions warden/group/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/ory/hydra/firewall"
"github.com/ory/hydra/pkg"
"github.com/ory/pagination"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -111,7 +111,7 @@ func (h *Handler) ListGroupsHandler(w http.ResponseWriter, r *http.Request, _ ht
return
}

limit, offset := pkg.ParsePagination(r, 100, 0, 500)
limit, offset := pagination.Parse(r, 100, 0, 500)
if member := r.URL.Query().Get("member"); member != "" {
h.FindGroupNames(w, r, member, limit, offset)
return
Expand All @@ -121,7 +121,7 @@ func (h *Handler) ListGroupsHandler(w http.ResponseWriter, r *http.Request, _ ht
}
}

func (h *Handler) ListGroups(w http.ResponseWriter, r *http.Request, limit, offset int64) {
func (h *Handler) ListGroups(w http.ResponseWriter, r *http.Request, limit, offset int) {
groups, err := h.Manager.ListGroups(limit, offset)
if err != nil {
h.H.WriteError(w, r, err)
Expand All @@ -131,7 +131,7 @@ func (h *Handler) ListGroups(w http.ResponseWriter, r *http.Request, limit, offs
h.H.Write(w, r, groups)
}

func (h *Handler) FindGroupNames(w http.ResponseWriter, r *http.Request, member string, limit, offset int64) {
func (h *Handler) FindGroupNames(w http.ResponseWriter, r *http.Request, member string, limit, offset int) {
groups, err := h.Manager.FindGroupsByMember(member, limit, offset)
if err != nil {
h.H.WriteError(w, r, err)
Expand Down
4 changes: 2 additions & 2 deletions warden/group/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ type Manager interface {
AddGroupMembers(group string, members []string) error
RemoveGroupMembers(group string, members []string) error

FindGroupsByMember(subject string, limit, offset int64) ([]Group, error)
ListGroups(limit, offset int64) ([]Group, error)
FindGroupsByMember(subject string, limit, offset int) ([]Group, error)
ListGroups(limit, offset int) ([]Group, error)
}
22 changes: 8 additions & 14 deletions warden/group/manager_memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"sync"

"github.com/ory/hydra/pkg"
"github.com/ory/pagination"
"github.com/pborman/uuid"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -91,7 +92,7 @@ func (m *MemoryManager) RemoveGroupMembers(group string, subjects []string) erro
return m.CreateGroup(g)
}

func (m *MemoryManager) FindGroupsByMember(subject string, limit, offset int64) ([]Group, error) {
func (m *MemoryManager) FindGroupsByMember(subject string, limit, offset int) ([]Group, error) {
if m.Groups == nil {
m.Groups = map[string]Group{}
}
Expand All @@ -106,29 +107,22 @@ func (m *MemoryManager) FindGroupsByMember(subject string, limit, offset int64)
}
}

if offset+limit > int64(len(res)) {
return []Group{}, nil
}

return res[offset:limit], nil
start, end := pagination.Index(limit, offset, len(res))
return res[start:end], nil
}

func (m *MemoryManager) ListGroups(limit, offset int64) ([]Group, error) {
func (m *MemoryManager) ListGroups(limit, offset int) ([]Group, error) {
if m.Groups == nil {
m.Groups = map[string]Group{}
}

if offset+limit > int64(len(m.Groups)) {
return []Group{}, nil
}

i := int64(0)
i := 0
res := make([]Group, len(m.Groups))
for _, g := range m.Groups {
res[i] = g
i++
}


return res[offset:limit], nil
start, end := pagination.Index(limit, offset, len(res))
return res[start:end], nil
}
4 changes: 2 additions & 2 deletions warden/group/manager_sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (m *SQLManager) RemoveGroupMembers(group string, subjects []string) error {
return nil
}

func (m *SQLManager) FindGroupsByMember(subject string, limit, offset int64) ([]Group, error) {
func (m *SQLManager) FindGroupsByMember(subject string, limit, offset int) ([]Group, error) {
var ids []string
if err := m.DB.Select(&ids, m.DB.Rebind("SELECT group_id from hydra_warden_group_member WHERE member = ? GROUP BY group_id ORDER BY group_id LIMIT ? OFFSET ?"), subject, limit, offset); err == sql.ErrNoRows {
return nil, errors.WithStack(pkg.ErrNotFound)
Expand All @@ -163,7 +163,7 @@ func (m *SQLManager) FindGroupsByMember(subject string, limit, offset int64) ([]
return groups, nil
}

func (m *SQLManager) ListGroups(limit, offset int64) ([]Group, error) {
func (m *SQLManager) ListGroups(limit, offset int) ([]Group, error) {
var ids []string
if err := m.DB.Select(&ids, m.DB.Rebind("SELECT group_id from hydra_warden_group_member GROUP BY group_id ORDER BY group_id LIMIT ? OFFSET ?"), limit, offset); err == sql.ErrNoRows {
return nil, errors.WithStack(pkg.ErrNotFound)
Expand Down

0 comments on commit 9592a00

Please sign in to comment.