From c3f5b609787fe53e0a610ead4a2ed00ba28a2ba4 Mon Sep 17 00:00:00 2001 From: Arvindh <30824765+arvindh123@users.noreply.github.com> Date: Thu, 12 Oct 2023 20:51:06 +0530 Subject: [PATCH] Add: List of user groups & removed repeating code in groups (#29) * removed repeating code in list groups Signed-off-by: Arvindh * add: list of user group Signed-off-by: Arvindh * fix: otel handler operator name for endpoints Signed-off-by: Arvindh --------- Signed-off-by: Arvindh --- internal/groups/service.go | 63 ++++++++++++++++++-------------------- things/api/channels.go | 6 ++-- things/api/clients.go | 2 +- users/api/clients.go | 4 +-- users/api/groups.go | 9 +++++- 5 files changed, 44 insertions(+), 40 deletions(-) diff --git a/internal/groups/service.go b/internal/groups/service.go index 4ccd1c72bb..f2a44bea71 100644 --- a/internal/groups/service.go +++ b/internal/groups/service.go @@ -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 @@ -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 { @@ -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 @@ -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") @@ -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, diff --git a/things/api/channels.go b/things/api/channels.go index b4b4d40388..2259f35e18 100644 --- a/things/api/channels.go +++ b/things/api/channels.go @@ -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 : @@ -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 @@ -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( diff --git a/things/api/clients.go b/things/api/clients.go index 7dac8705f7..2e88e8ca14 100644 --- a/things/api/clients.go +++ b/things/api/clients.go @@ -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 } diff --git a/users/api/clients.go b/users/api/clients.go index 5f298574a6..2786e15384 100644 --- a/users/api/clients.go +++ b/users/api/clients.go @@ -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 : @@ -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 } diff --git a/users/api/groups.go b/users/api/groups.go index 68edcc7397..9ff4ea455a 100644 --- a/users/api/groups.go +++ b/users/api/groups.go @@ -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 }