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>
Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
  • Loading branch information
arvindh123 committed Oct 16, 2023
1 parent ed513c9 commit 4ad6153
Show file tree
Hide file tree
Showing 5 changed files with 405 additions and 39 deletions.
63 changes: 30 additions & 33 deletions internal/groups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,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 @@ -173,14 +166,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 @@ -196,19 +184,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 @@ -222,15 +202,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 @@ -446,6 +426,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
Loading

0 comments on commit 4ad6153

Please sign in to comment.