Skip to content
This repository has been archived by the owner on Oct 11, 2023. It is now read-only.

Commit

Permalink
Implement pagination (closes #17)
Browse files Browse the repository at this point in the history
  • Loading branch information
gerson24 committed Sep 13, 2016
1 parent 545b2fd commit d9e7877
Show file tree
Hide file tree
Showing 35 changed files with 1,757 additions and 541 deletions.
4 changes: 2 additions & 2 deletions api/authz.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (api AuthAPI) getRestrictions(externalID string, action string, resource st
}

func (api AuthAPI) getGroupsByUser(userID string) ([]Group, error) {
groups, err := api.UserRepo.GetGroupsByUserID(userID)
groups, _, err := api.UserRepo.GetGroupsByUserID(userID, &Filter{})
if err != nil {
//Transform to DB error
dbError := err.(*database.Error)
Expand All @@ -244,7 +244,7 @@ func (api AuthAPI) getPoliciesByGroups(groups []Group) ([]Policy, error) {
// Retrieve per each group its attached policies
for _, group := range groups {
// Retrieve policies for this group
policiesAttached, err := api.GroupRepo.GetAttachedPolicies(group.ID)
policiesAttached, _, err := api.GroupRepo.GetAttachedPolicies(group.ID, &Filter{})
if err != nil {
//Transform to DB error
dbError := err.(*database.Error)
Expand Down
8 changes: 4 additions & 4 deletions api/authz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1004,10 +1004,10 @@ func TestGetRestrictions(t *testing.T) {
testRepo.ArgsOut[GetUserByExternalIDMethod][1] = test.getUserByExternalIDError

testRepo.ArgsOut[GetGroupsByUserIDMethod][0] = test.getGroupsByUserIDResult
testRepo.ArgsOut[GetGroupsByUserIDMethod][1] = test.getGroupsByUserIDError
testRepo.ArgsOut[GetGroupsByUserIDMethod][2] = test.getGroupsByUserIDError

testRepo.ArgsOut[GetAttachedPoliciesMethod][0] = test.getAttachedPoliciesResult
testRepo.ArgsOut[GetAttachedPoliciesMethod][1] = test.getAttachedPoliciesError
testRepo.ArgsOut[GetAttachedPoliciesMethod][2] = test.getAttachedPoliciesError

restrictions, err := testAPI.getRestrictions(test.authUserID, test.action, test.resourceUrn)
checkMethodResponse(t, n, test.wantError, err, test.expectedRestrictions, restrictions)
Expand Down Expand Up @@ -1080,7 +1080,7 @@ func TestGetGroupsByUser(t *testing.T) {
testAPI := makeTestAPI(testRepo)

testRepo.ArgsOut[GetGroupsByUserIDMethod][0] = test.getGroupsByUserIDResult
testRepo.ArgsOut[GetGroupsByUserIDMethod][1] = test.getGroupsByUserIDError
testRepo.ArgsOut[GetGroupsByUserIDMethod][2] = test.getGroupsByUserIDError

groups, err := testAPI.getGroupsByUser(test.userID)
checkMethodResponse(t, n, test.wantError, err, test.expectedGroups, groups)
Expand Down Expand Up @@ -1165,7 +1165,7 @@ func TestGetPoliciesByGroups(t *testing.T) {
testAPI := makeTestAPI(testRepo)

testRepo.ArgsOut[GetAttachedPoliciesMethod][0] = test.getAttachedPoliciesResult
testRepo.ArgsOut[GetAttachedPoliciesMethod][1] = test.getAttachedPoliciesError
testRepo.ArgsOut[GetAttachedPoliciesMethod][2] = test.getAttachedPoliciesError

policies, err := testAPI.getPoliciesByGroups(test.groups)
checkMethodResponse(t, n, test.wantError, err, test.expectedPolicies, policies)
Expand Down
88 changes: 62 additions & 26 deletions api/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,33 +171,45 @@ func (api AuthAPI) GetGroupByName(requestInfo RequestInfo, org string, name stri
}
}

func (api AuthAPI) ListGroups(requestInfo RequestInfo, org string, pathPrefix string) ([]GroupIdentity, error) {
func (api AuthAPI) ListGroups(requestInfo RequestInfo, org string, filter *Filter) ([]GroupIdentity, int, error) {
// Validate fields
var total int
if len(org) > 0 && !IsValidOrg(org) {
return nil, &Error{
return nil, total, &Error{
Code: INVALID_PARAMETER_ERROR,
Message: fmt.Sprintf("Invalid parameter: org %v", org),
}
}
if len(pathPrefix) > 0 && !IsValidPath(pathPrefix) {
return nil, &Error{
if len(filter.PathPrefix) > 0 && !IsValidPath(filter.PathPrefix) {
return nil, total, &Error{
Code: INVALID_PARAMETER_ERROR,
Message: fmt.Sprintf("Invalid parameter: PathPrefix %v", filter.PathPrefix),
}
}

if len(filter.PathPrefix) == 0 {
filter.PathPrefix = "/"
}

if filter.Limit > MAX_LIMIT_SIZE {
return nil, total, &Error{
Code: INVALID_PARAMETER_ERROR,
Message: fmt.Sprintf("Invalid parameter: PathPrefix %v", pathPrefix),
Message: fmt.Sprintf("Invalid parameter: Limit %v, max limit allowed: %v", filter.Limit, MAX_LIMIT_SIZE),
}
}

if len(pathPrefix) == 0 {
pathPrefix = "/"
if filter.Limit == 0 {
filter.Limit = DEFAULT_LIMIT_SIZE
}

// Call repo to retrieve the groups
groups, err := api.GroupRepo.GetGroupsFiltered(org, pathPrefix)
groups, total, err := api.GroupRepo.GetGroupsFiltered(org, filter)

// Error handling
if err != nil {
//Transform to DB error
dbError := err.(*database.Error)
return nil, &Error{
return nil, total, &Error{
Code: UNKNOWN_API_ERROR,
Message: dbError.Message,
}
Expand All @@ -208,11 +220,11 @@ func (api AuthAPI) ListGroups(requestInfo RequestInfo, org string, pathPrefix st
if len(org) == 0 {
urnPrefix = "*"
} else {
urnPrefix = GetUrnPrefix(org, RESOURCE_GROUP, pathPrefix)
urnPrefix = GetUrnPrefix(org, RESOURCE_GROUP, filter.PathPrefix)
}
filteredGroups, err := api.GetAuthorizedGroups(requestInfo, urnPrefix, GROUP_ACTION_LIST_GROUPS, groups)
if err != nil {
return nil, err
return nil, total, err
}

// Transform to identifiers
Expand All @@ -224,7 +236,7 @@ func (api AuthAPI) ListGroups(requestInfo RequestInfo, org string, pathPrefix st
})
}

return groupIDs, nil
return groupIDs, total, nil
}

func (api AuthAPI) UpdateGroup(requestInfo RequestInfo, org string, name string, newName string, newPath string) (*Group, error) {
Expand Down Expand Up @@ -476,35 +488,47 @@ func (api AuthAPI) RemoveMember(requestInfo RequestInfo, externalId string, name
return nil
}

func (api AuthAPI) ListMembers(requestInfo RequestInfo, org string, name string) ([]string, error) {
func (api AuthAPI) ListMembers(requestInfo RequestInfo, org string, name string, filter *Filter) ([]string, int, error) {
// Validate fields
var total int
if filter.Limit > MAX_LIMIT_SIZE {
return nil, total, &Error{
Code: INVALID_PARAMETER_ERROR,
Message: fmt.Sprintf("Invalid parameter: Limit %v, max limit allowed: %v", filter.Limit, MAX_LIMIT_SIZE),
}
}

if filter.Limit == 0 {
filter.Limit = DEFAULT_LIMIT_SIZE
}

// Call repo to retrieve the group
group, err := api.GetGroupByName(requestInfo, org, name)
if err != nil {
return nil, err
return nil, total, err
}

// Check restrictions
groupsFiltered, err := api.GetAuthorizedGroups(requestInfo, group.Urn, GROUP_ACTION_LIST_MEMBERS, []Group{*group})
if err != nil {
return nil, err
return nil, total, err
}
if len(groupsFiltered) < 1 {
return nil, &Error{
return nil, total, &Error{
Code: UNAUTHORIZED_RESOURCES_ERROR,
Message: fmt.Sprintf("User with externalId %v is not allowed to access to resource %v",
requestInfo.Identifier, group.Urn),
}
}

// Get Members
members, err := api.GroupRepo.GetGroupMembers(group.ID)
members, total, err := api.GroupRepo.GetGroupMembers(group.ID, filter)

// Error handling
if err != nil {
//Transform to DB error
dbError := err.(*database.Error)
return nil, &Error{
return nil, total, &Error{
Code: UNKNOWN_API_ERROR,
Message: dbError.Message,
}
Expand All @@ -515,7 +539,7 @@ func (api AuthAPI) ListMembers(requestInfo RequestInfo, org string, name string)
externalIDs = append(externalIDs, m.ExternalID)
}

return externalIDs, nil
return externalIDs, total, nil
}

func (api AuthAPI) AttachPolicyToGroup(requestInfo RequestInfo, org string, name string, policyName string) error {
Expand Down Expand Up @@ -639,35 +663,47 @@ func (api AuthAPI) DetachPolicyToGroup(requestInfo RequestInfo, org string, name
return nil
}

func (api AuthAPI) ListAttachedGroupPolicies(requestInfo RequestInfo, org string, name string) ([]string, error) {
func (api AuthAPI) ListAttachedGroupPolicies(requestInfo RequestInfo, org string, name string, filter *Filter) ([]string, int, error) {
// Validate fields
var total int
if filter.Limit > MAX_LIMIT_SIZE {
return nil, total, &Error{
Code: INVALID_PARAMETER_ERROR,
Message: fmt.Sprintf("Invalid parameter: Limit %v, max limit allowed: %v", filter.Limit, MAX_LIMIT_SIZE),
}
}

if filter.Limit == 0 {
filter.Limit = DEFAULT_LIMIT_SIZE
}

// Check if group exists
group, err := api.GetGroupByName(requestInfo, org, name)
if err != nil {
return nil, err
return nil, total, err
}

// Check restrictions
groupsFiltered, err := api.GetAuthorizedGroups(requestInfo, group.Urn, GROUP_ACTION_LIST_ATTACHED_GROUP_POLICIES, []Group{*group})
if err != nil {
return nil, err
return nil, total, err
}
if len(groupsFiltered) < 1 {
return nil, &Error{
return nil, total, &Error{
Code: UNAUTHORIZED_RESOURCES_ERROR,
Message: fmt.Sprintf("User with externalId %v is not allowed to access to resource %v",
requestInfo.Identifier, group.Urn),
}
}

// Call repo to retrieve the GroupPolicyRelations
attachedPolicies, err := api.GroupRepo.GetAttachedPolicies(group.ID)
attachedPolicies, total, err := api.GroupRepo.GetAttachedPolicies(group.ID, filter)

// Error handling
if err != nil {
//Transform to DB error
dbError := err.(*database.Error)
return nil, &Error{
return nil, total, &Error{
Code: UNKNOWN_API_ERROR,
Message: dbError.Message,
}
Expand All @@ -677,7 +713,7 @@ func (api AuthAPI) ListAttachedGroupPolicies(requestInfo RequestInfo, org string
for _, p := range attachedPolicies {
policyIDs = append(policyIDs, p.Name)
}
return policyIDs, nil
return policyIDs, total, nil
}

// PRIVATE HELPER METHODS
Expand Down
Loading

0 comments on commit d9e7877

Please sign in to comment.