Skip to content

Commit

Permalink
Add: List of user groups & removed repeating code in groups (absmach#29)
Browse files Browse the repository at this point in the history
* removed repeating code in list groups

Signed-off-by: Arvindh <arvindh91@gmail.com>

* add: list of user group

Signed-off-by: Arvindh <arvindh91@gmail.com>

* fix: otel handler operator name for endpoints

Signed-off-by: Arvindh <arvindh91@gmail.com>

---------

Signed-off-by: Arvindh <arvindh91@gmail.com>
  • Loading branch information
arvindh123 committed Oct 12, 2023
1 parent d2e1b27 commit c3f5b60
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 40 deletions.
63 changes: 30 additions & 33 deletions internal/groups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,10 @@ func (svc service) ListGroups(ctx context.Context, token string, memberKind, mem
if err != nil {
return groups.Page{}, err
}
allowedIDs, err := svc.listAllGroupsOfUserID(ctx, userID, gm.Permission)
ids, err = svc.filterAllowedGroupIDsOfUserID(ctx, userID, gm.Permission, cids.Policies)
if err != nil {
return groups.Page{}, err
}
for _, cid := range cids.Policies {
for _, id := range allowedIDs {
if id == cid {
ids = append(ids, id)
}
}
}
case groupsKind:
if _, err := svc.authorizeKind(ctx, userType, usersKind, userID, gm.Permission, groupType, memberID); err != nil {
return groups.Page{}, err
Expand All @@ -183,14 +176,9 @@ func (svc service) ListGroups(ctx context.Context, token string, memberKind, mem
if err != nil {
return groups.Page{}, err
}

allowedIDs, err := svc.listAllGroupsOfUserID(ctx, userID, gm.Permission)
for _, gid := range gids.Policies {
for _, id := range allowedIDs {
if id == gid {
ids = append(ids, id)
}
}
ids, err = svc.filterAllowedGroupIDsOfUserID(ctx, userID, gm.Permission, gids.Policies)
if err != nil {
return groups.Page{}, err
}
case channelsKind:
if _, err := svc.authorizeKind(ctx, userType, usersKind, userID, viewPermission, groupType, memberID); err != nil {
Expand All @@ -206,19 +194,11 @@ func (svc service) ListGroups(ctx context.Context, token string, memberKind, mem
return groups.Page{}, err
}

allowedIDs, err := svc.listAllGroupsOfUserID(ctx, userID, gm.Permission)
for _, gid := range gids.Policies {
for _, id := range allowedIDs {
if id == gid {
ids = append(ids, id)
}
}
}
case usersKind:
allowedIDs, err := svc.listAllGroupsOfUserID(ctx, userID, gm.Permission)
ids, err = svc.filterAllowedGroupIDsOfUserID(ctx, userID, gm.Permission, gids.Policies)
if err != nil {
return groups.Page{}, err
}
case usersKind:
if memberID != "" && userID != memberID {
if _, err := svc.authorizeKind(ctx, userType, usersKind, userID, ownerRelation, userType, memberID); err != nil {
return groups.Page{}, err
Expand All @@ -233,15 +213,15 @@ func (svc service) ListGroups(ctx context.Context, token string, memberKind, mem
if err != nil {
return groups.Page{}, err
}
for _, gid := range gids.Policies {
for _, id := range allowedIDs {
if id == gid {
ids = append(ids, id)
}
}
ids, err = svc.filterAllowedGroupIDsOfUserID(ctx, userID, gm.Permission, gids.Policies)
if err != nil {
return groups.Page{}, err
}
} else {
ids = allowedIDs
ids, err = svc.listAllGroupsOfUserID(ctx, userID, gm.Permission)
if err != nil {
return groups.Page{}, err
}
}
default:
return groups.Page{}, fmt.Errorf("invalid member kind")
Expand Down Expand Up @@ -462,6 +442,23 @@ func (svc service) Unassign(ctx context.Context, token, groupID, relation, membe
return nil
}

func (svc service) filterAllowedGroupIDsOfUserID(ctx context.Context, userID string, permission string, groupIDs []string) ([]string, error) {
var ids []string
allowedIDs, err := svc.listAllGroupsOfUserID(ctx, userID, permission)
if err != nil {
return []string{}, err
}

for _, gid := range groupIDs {
for _, id := range allowedIDs {
if id == gid {
ids = append(ids, id)
}
}
}
return ids, nil
}

func (svc service) listAllGroupsOfUserID(ctx context.Context, userID string, permission string) ([]string, error) {
allowedIDs, err := svc.auth.ListAllObjects(ctx, &mainflux.ListObjectsReq{
SubjectType: userType,
Expand Down
6 changes: 3 additions & 3 deletions things/api/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func groupsHandler(svc groups.Service, r *chi.Mux, logger logger.Logger) http.Ha
gapi.DecodeListGroupsRequest,
api.EncodeResponse,
opts...,
), "list_channel_by_things").ServeHTTP)
), "list_channel_by_thing_id").ServeHTTP)

// Ideal location: users service, users endpoint
// Reason for placing here :
Expand All @@ -160,7 +160,7 @@ func groupsHandler(svc groups.Service, r *chi.Mux, logger logger.Logger) http.Ha
gapi.DecodeListGroupsRequest,
api.EncodeResponse,
opts...,
), "list_channel_by_things").ServeHTTP)
), "list_channel_by_user_id").ServeHTTP)

// Ideal location: users service, groups endpoint
// SpiceDB provides list of channel ids attached to given user_group id
Expand All @@ -171,7 +171,7 @@ func groupsHandler(svc groups.Service, r *chi.Mux, logger logger.Logger) http.Ha
gapi.DecodeListGroupsRequest,
api.EncodeResponse,
opts...,
), "list_channel_by_things").ServeHTTP)
), "list_channel_by_user_group_id").ServeHTTP)

// Connect channel and thing
r.Post("/connect", otelhttp.NewHandler(kithttp.NewServer(
Expand Down
2 changes: 1 addition & 1 deletion things/api/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func clientsHandler(svc things.Service, r *chi.Mux, logger mflog.Logger) http.Ha
decodeListMembersRequest,
api.EncodeResponse,
opts...,
), "list_things_by_channel").ServeHTTP)
), "list_things_by_channel_id").ServeHTTP)

return r
}
Expand Down
4 changes: 2 additions & 2 deletions users/api/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func clientsHandler(svc users.Service, r *chi.Mux, logger mflog.Logger) http.Han
decodeListMembersRequest,
api.EncodeResponse,
opts...,
), "list_users").ServeHTTP)
), "list_users_by_user_group_id").ServeHTTP)

// Ideal location: things service, channels endpoint.
// Reason for placing here :
Expand All @@ -156,7 +156,7 @@ func clientsHandler(svc users.Service, r *chi.Mux, logger mflog.Logger) http.Han
decodeListMembersRequest,
api.EncodeResponse,
opts...,
), "list_users_of_a_channel").ServeHTTP)
), "list_users_by_channel_id").ServeHTTP)

return r
}
Expand Down
9 changes: 8 additions & 1 deletion users/api/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,14 @@ func groupsHandler(svc groups.Service, r *chi.Mux, logger logger.Logger) http.Ha
gapi.DecodeListGroupsRequest,
api.EncodeResponse,
opts...,
), "list_groups_of_a_channel").ServeHTTP)
), "list_groups_by_channel_id").ServeHTTP)

r.Get("/users/{memberID}/groups", otelhttp.NewHandler(kithttp.NewServer(
gapi.ListGroupsEndpoint(svc, "users"),
gapi.DecodeListGroupsRequest,
api.EncodeResponse,
opts...,
), "list_groups_by_user_id").ServeHTTP)
return r
}

Expand Down

0 comments on commit c3f5b60

Please sign in to comment.