Skip to content

Commit

Permalink
NOISSUE - Get group role actions from Spicedb schema & list groups by…
Browse files Browse the repository at this point in the history
… user, clients, channels (#2506)

Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 authored Nov 22, 2024
1 parent af70ccc commit 4f27511
Show file tree
Hide file tree
Showing 63 changed files with 1,705 additions and 738 deletions.
30 changes: 30 additions & 0 deletions channels/api/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

grpcChannelsV1 "github.com/absmach/magistrala/internal/grpc/channels/v1"
grpcCommonV1 "github.com/absmach/magistrala/internal/grpc/common/v1"
"github.com/absmach/magistrala/pkg/connections"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
Expand All @@ -28,6 +29,7 @@ type grpcClient struct {
authorize endpoint.Endpoint
removeClientConnections endpoint.Endpoint
unsetParentGroupFromChannels endpoint.Endpoint
retrieveEntity endpoint.Endpoint
}

// NewClient returns new gRPC client instance.
Expand Down Expand Up @@ -57,6 +59,14 @@ func NewClient(conn *grpc.ClientConn, timeout time.Duration) grpcChannelsV1.Chan
decodeUnsetParentGroupFromChannelsResponse,
grpcChannelsV1.UnsetParentGroupFromChannelsRes{},
).Endpoint(),
retrieveEntity: kitgrpc.NewClient(
conn,
svcName,
"RetrieveEntity",
encodeRetrieveEntityRequest,
decodeRetrieveEntityResponse,
grpcCommonV1.RetrieveEntityRes{},
).Endpoint(),
timeout: timeout,
}
}
Expand Down Expand Up @@ -137,6 +147,26 @@ func decodeUnsetParentGroupFromChannelsResponse(_ context.Context, grpcRes inter
return grpcRes.(*grpcChannelsV1.UnsetParentGroupFromChannelsRes), nil
}

func (client grpcClient) RetrieveEntity(ctx context.Context, req *grpcCommonV1.RetrieveEntityReq, _ ...grpc.CallOption) (r *grpcCommonV1.RetrieveEntityRes, err error) {
ctx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()

res, err := client.retrieveEntity(ctx, req.GetId())
if err != nil {
return &grpcCommonV1.RetrieveEntityRes{}, decodeError(err)
}

return res.(*grpcCommonV1.RetrieveEntityRes), nil
}

func encodeRetrieveEntityRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
return grpcReq.(*grpcCommonV1.RetrieveEntityReq), nil
}

func decodeRetrieveEntityResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
return grpcRes.(*grpcCommonV1.RetrieveEntityRes), nil
}

func decodeError(err error) error {
if st, ok := status.FromError(err); ok {
switch st.Code() {
Expand Down
12 changes: 12 additions & 0 deletions channels/api/grpc/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,15 @@ func unsetParentGroupFromChannelsEndpoint(svc channels.Service) endpoint.Endpoin
return unsetParentGroupFromChannelsRes{}, nil
}
}

func retrieveEntityEndpoint(svc channels.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(retrieveEntityReq)
channel, err := svc.RetrieveByID(ctx, req.Id)

if err != nil {
return retrieveEntityRes{}, err
}
return retrieveEntityRes{id: channel.ID, domain: channel.Domain, parentGroup: channel.ParentGroup, status: uint8(channel.Status)}, nil
}
}
4 changes: 4 additions & 0 deletions channels/api/grpc/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ type removeClientConnectionsReq struct {
type unsetParentGroupFromChannelsReq struct {
parentGroupID string
}

type retrieveEntityReq struct {
Id string
}
9 changes: 9 additions & 0 deletions channels/api/grpc/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ type authorizeRes struct {
type removeClientConnectionsRes struct{}

type unsetParentGroupFromChannelsRes struct{}

type channelBasic struct {
id string
domain string
parentGroup string
status uint8
}

type retrieveEntityRes channelBasic
35 changes: 35 additions & 0 deletions channels/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
mgauth "github.com/absmach/magistrala/auth"
channels "github.com/absmach/magistrala/channels/private"
grpcChannelsV1 "github.com/absmach/magistrala/internal/grpc/channels/v1"
grpcCommonV1 "github.com/absmach/magistrala/internal/grpc/common/v1"
"github.com/absmach/magistrala/pkg/apiutil"
"github.com/absmach/magistrala/pkg/connections"
"github.com/absmach/magistrala/pkg/errors"
Expand All @@ -26,6 +27,7 @@ type grpcServer struct {
authorize kitgrpc.Handler
removeClientConnections kitgrpc.Handler
unsetParentGroupFromChannels kitgrpc.Handler
retrieveEntity kitgrpc.Handler
}

// NewServer returns new AuthServiceServer instance.
Expand All @@ -46,6 +48,11 @@ func NewServer(svc channels.Service) grpcChannelsV1.ChannelsServiceServer {
decodeUnsetParentGroupFromChannelsRequest,
encodeUnsetParentGroupFromChannelsResponse,
),
retrieveEntity: kitgrpc.NewServer(
retrieveEntityEndpoint(svc),
decodeRetrieveEntityRequest,
encodeRetrieveEntityResponse,
),
}
}

Expand Down Expand Up @@ -120,6 +127,34 @@ func encodeUnsetParentGroupFromChannelsResponse(_ context.Context, grpcRes inter
return &grpcChannelsV1.UnsetParentGroupFromChannelsRes{}, nil
}

func (s *grpcServer) RetrieveEntity(ctx context.Context, req *grpcCommonV1.RetrieveEntityReq) (*grpcCommonV1.RetrieveEntityRes, error) {
_, res, err := s.retrieveEntity.ServeGRPC(ctx, req)
if err != nil {
return nil, encodeError(err)
}
return res.(*grpcCommonV1.RetrieveEntityRes), nil
}

func decodeRetrieveEntityRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(*grpcCommonV1.RetrieveEntityReq)
return retrieveEntityReq{
Id: req.GetId(),
}, nil
}

func encodeRetrieveEntityResponse(_ context.Context, grpcRes interface{}) (interface{}, error) {
res := grpcRes.(retrieveEntityRes)

return &grpcCommonV1.RetrieveEntityRes{
Entity: &grpcCommonV1.EntityBasic{
Id: res.id,
DomainId: res.domain,
ParentGroupId: res.parentGroup,
Status: uint32(res.status),
},
}, nil
}

func encodeError(err error) error {
switch {
case errors.Contains(err, nil):
Expand Down
10 changes: 6 additions & 4 deletions channels/middleware/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,19 @@ func AuthorizationMiddleware(svc channels.Service, repo channels.Repository, aut
if err := opp.Validate(); err != nil {
return nil, err
}
ram, err := rmMW.NewRoleManagerAuthorizationMiddleware(policies.ChannelType, svc, authz, rolesOpPerm)
if err != nil {
return nil, err
}

extOpp := channels.NewExternalOperationPerm()
if err := extOpp.AddOperationPermissionMap(extOpPerm); err != nil {
return nil, err
}
if err := extOpp.Validate(); err != nil {
return nil, err
}
ram, err := rmMW.NewRoleManagerAuthorizationMiddleware(policies.ChannelType, svc, authz, rolesOpPerm)
if err != nil {
return nil, err
}

return &authorizationMiddleware{
svc: svc,
repo: repo,
Expand Down
76 changes: 76 additions & 0 deletions channels/mocks/channels_client.go

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

5 changes: 5 additions & 0 deletions channels/private/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Service interface {
Authorize(ctx context.Context, req channels.AuthzReq) error
UnsetParentGroupFromChannels(ctx context.Context, parentGroupID string) error
RemoveClientConnections(ctx context.Context, clientID string) error
RetrieveByID(ctx context.Context, id string) (channels.Channel, error)
}

type service struct {
Expand Down Expand Up @@ -94,3 +95,7 @@ func (svc service) UnsetParentGroupFromChannels(ctx context.Context, parentGroup
}
return nil
}

func (svc service) RetrieveByID(ctx context.Context, id string) (channels.Channel, error) {
return svc.repo.RetrieveByID(ctx, id)
}
14 changes: 8 additions & 6 deletions clients/api/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ func decodeRetrieveEntityResponse(_ context.Context, grpcRes interface{}) (inter
res := grpcRes.(*grpcCommonV1.RetrieveEntityRes)

return retrieveEntityRes{
id: res.Entity.GetId(),
domain: res.Entity.GetDomainId(),
status: uint8(res.Entity.GetStatus()),
id: res.Entity.GetId(),
domain: res.Entity.GetDomainId(),
parentGroup: res.Entity.GetParentGroupId(),
status: uint8(res.Entity.GetStatus()),
}, nil
}

Expand Down Expand Up @@ -202,9 +203,10 @@ func decodeRetrieveEntitiesResponse(_ context.Context, grpcRes interface{}) (int

for _, e := range res.Entities {
clis = append(clis, enitity{
id: e.GetId(),
domain: e.GetDomainId(),
status: uint8(e.GetStatus()),
id: e.GetId(),
domain: e.GetDomainId(),
parentGroup: e.GetParentGroupId(),
status: uint8(e.GetStatus()),
})
}
return retrieveEntitiesRes{total: res.GetTotal(), limit: res.GetLimit(), offset: res.GetOffset(), clients: clis}, nil
Expand Down
6 changes: 3 additions & 3 deletions clients/api/grpc/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ func authenticateEndpoint(svc pClients.Service) endpoint.Endpoint {
func retrieveEntityEndpoint(svc pClients.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(retrieveEntityReq)
Client, err := svc.RetrieveById(ctx, req.Id)
client, err := svc.RetrieveById(ctx, req.Id)
if err != nil {
return retrieveEntityRes{}, err
}

return retrieveEntityRes{id: Client.ID, domain: Client.Domain, status: uint8(Client.Status)}, nil
return retrieveEntityRes{id: client.ID, domain: client.Domain, parentGroup: client.ParentGroup, status: uint8(client.Status)}, nil
}
}

Expand All @@ -46,7 +46,7 @@ func retrieveEntitiesEndpoint(svc pClients.Service) endpoint.Endpoint {
}
clientsBasic := []enitity{}
for _, client := range tp.Clients {
clientsBasic = append(clientsBasic, enitity{id: client.ID, domain: client.Domain, status: uint8(client.Status)})
clientsBasic = append(clientsBasic, enitity{id: client.ID, domain: client.Domain, parentGroup: client.ParentGroup, status: uint8(client.Status)})
}
return retrieveEntitiesRes{
total: tp.Total,
Expand Down
7 changes: 4 additions & 3 deletions clients/api/grpc/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ package grpc
import "github.com/absmach/magistrala/pkg/connections"

type enitity struct {
id string
domain string
status uint8
id string
domain string
parentGroup string
status uint8
}

type authenticateRes struct {
Expand Down
14 changes: 8 additions & 6 deletions clients/api/grpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ func encodeRetrieveEntityResponse(_ context.Context, grpcRes interface{}) (inter

return &grpcCommonV1.RetrieveEntityRes{
Entity: &grpcCommonV1.EntityBasic{
Id: res.id,
DomainId: res.domain,
Status: uint32(res.status),
Id: res.id,
DomainId: res.domain,
ParentGroupId: res.parentGroup,
Status: uint32(res.status),
},
}, nil
}
Expand All @@ -142,9 +143,10 @@ func encodeRetrieveEntitiesResponse(_ context.Context, grpcRes interface{}) (int
entities := []*grpcCommonV1.EntityBasic{}
for _, c := range res.clients {
entities = append(entities, &grpcCommonV1.EntityBasic{
Id: c.id,
DomainId: c.domain,
Status: uint32(c.status),
Id: c.id,
DomainId: c.domain,
ParentGroupId: c.parentGroup,
Status: uint32(c.status),
})
}
return &grpcCommonV1.RetrieveEntitiesRes{Total: res.total, Limit: res.limit, Offset: res.offset, Entities: entities}, nil
Expand Down
Loading

0 comments on commit 4f27511

Please sign in to comment.