Skip to content

Commit

Permalink
NOISSUE: Fix Response , SDK, Listing of users with Relation (#1935)
Browse files Browse the repository at this point in the history
* fix: response of requests

Signed-off-by: Arvindh <[email protected]>

* fix: sdk url

Signed-off-by: Arvindh <[email protected]>

* fix: listing of users with relation

Signed-off-by: Arvindh <[email protected]>

---------

Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 authored Oct 18, 2023
1 parent 89fcf12 commit 75b37e0
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 63 deletions.
29 changes: 29 additions & 0 deletions auth/policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,35 @@ import (
"encoding/json"
)

const (
TokenKind = "token"
GroupsKind = "groups"
ChannelsKind = "channels"
ThingsKind = "things"
UsersKind = "users"

GroupType = "group"
ChannelType = "channel"
ThingType = "thing"
UserType = "user"

OwnerRelation = "owner"
AdminRelation = "admin"
EditorRelation = "editor"
ViewerRelation = "viewer"
ParentGroupRelation = "parent_group"
RoleGroupRelation = "role_group"
GroupRelation = "group"

AdministratorPermission = "administrator"
DeletePermission = "delete"
EditPermission = "edit"
ViewPermission = "view"
SharePermission = "share"
PublishPermission = "publish"
SubscribePermission = "subscribe"
)

// PolicyReq represents an argument struct for making a policy related
// function calls.
type PolicyReq struct {
Expand Down
16 changes: 16 additions & 0 deletions auth/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,19 @@ func (svc service) authenticate(token string) (string, string, error) {

return key.Issuer, key.Subject, nil
}

// Switch the relative permission for the relation
func SwitchToPermission(relation string) string {
switch relation {
case OwnerRelation:
return AdministratorPermission
case AdminRelation:
return AdministratorPermission
case EditorRelation:
return EditPermission
case ViewerRelation:
return ViewPermission
default:
return relation
}
}
22 changes: 11 additions & 11 deletions internal/groups/api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func CreateGroupEndpoint(svc groups.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createGroupReq)
if err := req.validate(); err != nil {
return createGroupRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

group, err := svc.CreateGroup(ctx, req.token, req.Group)
if err != nil {
return createGroupRes{}, err
return nil, err
}

return createGroupRes{created: true, Group: group}, nil
Expand All @@ -32,12 +32,12 @@ func ViewGroupEndpoint(svc groups.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(groupReq)
if err := req.validate(); err != nil {
return viewGroupRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

group, err := svc.ViewGroup(ctx, req.token, req.id)
if err != nil {
return viewGroupRes{}, err
return nil, err
}

return viewGroupRes{Group: group}, nil
Expand All @@ -48,7 +48,7 @@ func UpdateGroupEndpoint(svc groups.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(updateGroupReq)
if err := req.validate(); err != nil {
return updateGroupRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

group := groups.Group{
Expand All @@ -60,7 +60,7 @@ func UpdateGroupEndpoint(svc groups.Service) endpoint.Endpoint {

group, err := svc.UpdateGroup(ctx, req.token, group)
if err != nil {
return updateGroupRes{}, err
return nil, err
}

return updateGroupRes{Group: group}, nil
Expand Down Expand Up @@ -102,11 +102,11 @@ func ListGroupsEndpoint(svc groups.Service, memberKind string) endpoint.Endpoint
req.memberKind = memberKind
}
if err := req.validate(); err != nil {
return groupPageRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}
page, err := svc.ListGroups(ctx, req.token, req.memberKind, req.memberID, req.Page)
if err != nil {
return groupPageRes{}, err
return nil, err
}

if req.tree {
Expand All @@ -124,12 +124,12 @@ func ListMembersEndpoint(svc groups.Service, memberKind string) endpoint.Endpoin
req.memberKind = memberKind
}
if err := req.validate(); err != nil {
return membershipPageRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

page, err := svc.ListMembers(ctx, req.token, req.groupID, req.permission, req.memberKind)
if err != nil {
return membershipPageRes{}, err
return nil, err
}

return listMembersRes{
Expand Down Expand Up @@ -172,7 +172,7 @@ func UnassignMembersEndpoint(svc groups.Service, relation string, memberKind str
req.MemberKind = memberKind
}
if err := req.validate(); err != nil {
return membershipPageRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

if err := svc.Unassign(ctx, req.token, req.groupID, req.Relation, req.MemberKind, req.Members...); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/groups/api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func (res changeStatusRes) Empty() bool {
type assignRes struct{}

func (res assignRes) Code() int {
return http.StatusOK
return http.StatusCreated
}

func (res assignRes) Headers() map[string]string {
Expand Down
18 changes: 9 additions & 9 deletions pkg/sdk/go/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (sdk mfSDK) AddUserToChannel(channelID string, req UsersRelationRequest, to
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, usersEndpoint)
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, usersEndpoint, assignEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
return sdkerr
Expand All @@ -164,9 +164,9 @@ func (sdk mfSDK) RemoveUserFromChannel(channelID string, req UsersRelationReques
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, usersEndpoint)
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, usersEndpoint, unassignEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodDelete, url, token, data, nil, http.StatusOK)
_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
return sdkerr
}

Expand All @@ -193,7 +193,7 @@ func (sdk mfSDK) AddUserGroupToChannel(channelID string, req UserGroupsRequest,
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, groupsEndpoint)
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, groupsEndpoint, assignEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
return sdkerr
Expand All @@ -205,9 +205,9 @@ func (sdk mfSDK) RemoveUserGroupFromChannel(channelID string, req UserGroupsRequ
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, groupsEndpoint)
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, groupsEndpoint, unassignEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodDelete, url, token, data, nil, http.StatusOK)
_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
return sdkerr
}

Expand Down Expand Up @@ -255,17 +255,17 @@ func (sdk mfSDK) Disconnect(connIDs Connection, token string) errors.SDKError {
}

func (sdk mfSDK) ConnectThing(thingID, channelID, token string) errors.SDKError {
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, thingsEndpoint, thingID)
url := fmt.Sprintf("%s/%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, thingsEndpoint, thingID, connectEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, nil, nil, http.StatusNoContent)

return sdkerr
}

func (sdk mfSDK) DisconnectThing(thingID, channelID, token string) errors.SDKError {
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, thingsEndpoint, thingID)
url := fmt.Sprintf("%s/%s/%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, channelID, thingsEndpoint, thingID, disconnectEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodDelete, url, token, nil, nil, http.StatusNoContent)
_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, nil, nil, http.StatusNoContent)

return sdkerr
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/go/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (sdk mfSDK) AddUserToGroup(groupID string, req UsersRelationRequest, token
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.usersURL, groupsEndpoint, groupID, usersEndpoint)
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.usersURL, groupsEndpoint, groupID, usersEndpoint, assignEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
return sdkerr
Expand All @@ -163,9 +163,9 @@ func (sdk mfSDK) RemoveUserFromGroup(groupID string, req UsersRelationRequest, t
return errors.NewSDKError(err)
}

url := fmt.Sprintf("%s/%s/%s/%s", sdk.usersURL, groupsEndpoint, groupID, usersEndpoint)
url := fmt.Sprintf("%s/%s/%s/%s/%s", sdk.usersURL, groupsEndpoint, groupID, usersEndpoint, unassignEndpoint)

_, _, sdkerr := sdk.processRequest(http.MethodDelete, url, token, data, nil, http.StatusOK)
_, _, sdkerr := sdk.processRequest(http.MethodPost, url, token, data, nil, http.StatusOK)
return sdkerr
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/sdk/go/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

const (
usersEndpoint = "users"
assignEndpoint = "assign"
unassignEndpoint = "unassign"
enableEndpoint = "enable"
disableEndpoint = "disable"
issueTokenEndpoint = "tokens/issue"
Expand Down
32 changes: 16 additions & 16 deletions things/api/http/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func createClientEndpoint(svc things.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createClientReq)
if err := req.validate(); err != nil {
return createClientRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

client, err := svc.CreateThings(ctx, req.token, req.client)
if err != nil {
return createClientRes{}, err
return nil, err
}

return createClientRes{
Expand All @@ -37,12 +37,12 @@ func createClientsEndpoint(svc things.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createClientsReq)
if err := req.validate(); err != nil {
return clientsPageRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

page, err := svc.CreateThings(ctx, req.token, req.Clients...)
if err != nil {
return clientsPageRes{}, err
return nil, err
}

res := clientsPageRes{
Expand Down Expand Up @@ -79,7 +79,7 @@ func listClientsEndpoint(svc things.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listClientsReq)
if err := req.validate(); err != nil {
return mfclients.ClientsPage{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

pm := mfclients.Page{
Expand All @@ -94,7 +94,7 @@ func listClientsEndpoint(svc things.Service) endpoint.Endpoint {
}
page, err := svc.ListClients(ctx, req.token, req.userID, pm)
if err != nil {
return mfclients.ClientsPage{}, err
return nil, err
}

res := clientsPageRes{
Expand All @@ -117,12 +117,12 @@ func listMembersEndpoint(svc things.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(listMembersReq)
if err := req.validate(); err != nil {
return memberPageRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

page, err := svc.ListClientsByGroup(ctx, req.token, req.groupID, req.Page)
if err != nil {
return memberPageRes{}, err
return nil, err
}

return buildMembersResponse(page), nil
Expand Down Expand Up @@ -348,11 +348,11 @@ func connectEndpoint(svc groups.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(connectChannelThingRequest)
if err := req.validate(); err != nil {
return connectChannelThingRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

if err := svc.Assign(ctx, req.token, req.ChannelID, "group", "things", req.ThingID); err != nil {
return connectChannelThingRes{}, err
return nil, err
}

return connectChannelThingRes{}, nil
Expand All @@ -363,11 +363,11 @@ func disconnectEndpoint(svc groups.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(disconnectChannelThingRequest)
if err := req.validate(); err != nil {
return disconnectChannelThingRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

if err := svc.Unassign(ctx, req.token, req.ChannelID, "group", "things", req.ThingID); err != nil {
return disconnectChannelThingRes{}, err
return nil, err
}

return disconnectChannelThingRes{}, nil
Expand All @@ -378,11 +378,11 @@ func thingShareEndpoint(svc things.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(thingShareRequest)
if err := req.validate(); err != nil {
return thingShareRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

if err := svc.Share(ctx, req.token, req.thingID, req.Relation, req.UserIDs...); err != nil {
return thingShareRes{}, err
return nil, err
}

return thingShareRes{}, nil
Expand All @@ -393,11 +393,11 @@ func thingUnshareEndpoint(svc things.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(thingUnshareRequest)
if err := req.validate(); err != nil {
return thingUnshareRes{}, errors.Wrap(apiutil.ErrValidation, err)
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

if err := svc.Unshare(ctx, req.token, req.thingID, req.Relation, req.UserIDs...); err != nil {
return thingShareRes{}, err
return nil, err
}

return thingUnshareRes{}, nil
Expand Down
Loading

0 comments on commit 75b37e0

Please sign in to comment.