Skip to content

Commit

Permalink
MF-535 - Remove owner_id from Things and Channels (#536)
Browse files Browse the repository at this point in the history
* MF-535 - Remove owner_id from Things and Channels

* Fix tests

* Fix db constraints and tests

* Add migrations and fix tests

* Remove RetrieveThingsByGroup method

* Replace named parameter

* Use helper method GetOffsetLimitQuery
  • Loading branch information
majabirmancevic authored Nov 7, 2024
1 parent 7bf20f6 commit fd09e97
Show file tree
Hide file tree
Showing 46 changed files with 1,270 additions and 1,892 deletions.
15 changes: 2 additions & 13 deletions api/openapi/things.yml
Original file line number Diff line number Diff line change
Expand Up @@ -997,19 +997,11 @@ components:
channel_id:
type: string
example: "6e3d5c1e-8d5a-4b3f-8f3f-4c4b4c4b4c4b"
description: Unique channel identifier generated by the service.
channel_owner:
type: string
example: "6e3d5c1e-8d5a-4b3f-8f3f-4c4b4c4b4c4b"
description: Unique channel owner identifier generated by the service.
description: Unique channel identifier generated by the service.
thing_id:
type: string
example: "6e3d5c1e-8d5a-4b3f-8f3f-4c4b4c4b4c4b"
description: Unique thing identifier generated by the service.
thing_owner:
type: string
example: "6e3d5c1e-8d5a-4b3f-8f3f-4c4b4c4b4c4b"
description: Unique thing owner identifier generated by the service.
description: Unique thing identifier generated by the service.
name:
type: string
description: Free-form channel name.
Expand All @@ -1018,9 +1010,7 @@ components:
description: Arbitrary, object-encoded channel's data.
required:
- channel_id
- channel_owner
- thing_id
- thing_owner
GroupsPage:
type: object
properties:
Expand Down Expand Up @@ -1116,7 +1106,6 @@ components:
- id
- org_id
- name
- owner_id
- description
- created_at
- updated_at
Expand Down
6 changes: 1 addition & 5 deletions auth/postgres/orgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ func (or orgRepository) RetrieveAllMembersByOrg(ctx context.Context) ([]auth.Org
func (or orgRepository) retrieve(ctx context.Context, ownerID string, pm auth.PageMetadata) (auth.OrgsPage, error) {
ownq := dbutil.GetOwnerQuery(ownerID)
nq, name := dbutil.GetNameQuery(pm.Name)
olq := dbutil.GetOffsetLimitQuery(pm.Limit)
meta, mq, err := dbutil.GetMetadataQuery("orgs", pm.Metadata)
if err != nil {
return auth.OrgsPage{}, errors.Wrap(errors.ErrRetrieveEntity, err)
Expand All @@ -478,11 +479,6 @@ func (or orgRepository) retrieve(ctx context.Context, ownerID string, pm auth.Pa
whereClause = fmt.Sprintf(" WHERE %s", strings.Join(query, " AND "))
}

olq := "LIMIT :limit OFFSET :offset"
if pm.Limit == 0 {
olq = ""
}

q := fmt.Sprintf(`SELECT id, owner_id, name, description, metadata, created_at, updated_at FROM orgs %s %s;`, whereClause, olq)

params := map[string]interface{}{
Expand Down
5 changes: 2 additions & 3 deletions certs/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ func newThingsService(auth protomfx.AuthServiceClient) things.Service {
for i := 0; i < thingsNum; i++ {
id := strconv.Itoa(i + 1)
ths[id] = things.Thing{
ID: id,
Key: thingKey,
OwnerID: email,
ID: id,
Key: thingKey,
}
}

Expand Down
5 changes: 1 addition & 4 deletions consumers/notifiers/postgres/notifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ func (nr notifierRepository) RetrieveByGroupID(ctx context.Context, groupID stri

oq := dbutil.GetOrderQuery(pm.Order)
dq := dbutil.GetDirQuery(pm.Dir)
olq := "LIMIT :limit OFFSET :offset"
if pm.Limit == 0 {
olq = ""
}
olq := dbutil.GetOffsetLimitQuery(pm.Limit)

q := fmt.Sprintf(`SELECT id, group_id, name, contacts, metadata FROM notifiers WHERE group_id = :group_id ORDER BY %s %s %s;`, oq, dq, olq)
qc := `SELECT COUNT(*) FROM notifiers WHERE group_id = $1;`
Expand Down
6 changes: 2 additions & 4 deletions mqtt/postgres/subscriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/MainfluxLabs/mainflux/mqtt"
"github.com/MainfluxLabs/mainflux/pkg/dbutil"
"github.com/MainfluxLabs/mainflux/pkg/errors"
"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
Expand Down Expand Up @@ -96,10 +97,7 @@ func (mr *mqttRepository) HasClientID(ctx context.Context, clientID string) erro
}

func (mr *mqttRepository) RetrieveByGroupID(ctx context.Context, pm mqtt.PageMetadata, groupID string) (mqtt.Page, error) {
olq := "LIMIT :limit OFFSET :offset"
if pm.Limit == 0 {
olq = ""
}
olq := dbutil.GetOffsetLimitQuery(pm.Limit)

q := fmt.Sprintf(`SELECT subtopic, group_id, client_id, thing_id, status, created_at FROM subscriptions WHERE group_id= :group_id ORDER BY created_at %s;`, olq)
params := map[string]interface{}{
Expand Down
7 changes: 7 additions & 0 deletions pkg/dbutil/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,10 @@ func GetDirQuery(dir string) string {
return "DESC"
}
}
func GetOffsetLimitQuery(limit uint64) string {
if limit != 0 {
return "LIMIT :limit OFFSET :offset"
}

return ""
}
67 changes: 9 additions & 58 deletions pkg/mocks/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,12 @@ func NewThingsService(things map[string]things.Thing, channels map[string]things
}
}

func (svc *mainfluxThings) CreateThings(_ context.Context, owner string, ths ...things.Thing) ([]things.Thing, error) {
func (svc *mainfluxThings) CreateThings(_ context.Context, token string, ths ...things.Thing) ([]things.Thing, error) {
svc.mu.Lock()
defer svc.mu.Unlock()

user, err := svc.auth.Identify(context.Background(), &protomfx.Token{Value: owner})
if err != nil {
return []things.Thing{}, errors.ErrAuthentication
}
for i := range ths {
svc.counter++
ths[i].OwnerID = user.Email
ths[i].ID = strconv.FormatUint(svc.counter, 10)
ths[i].Key = ths[i].ID
svc.things[ths[i].ID] = ths[i]
Expand All @@ -54,53 +49,31 @@ func (svc *mainfluxThings) CreateThings(_ context.Context, owner string, ths ...
return ths, nil
}

func (svc *mainfluxThings) ViewThing(_ context.Context, owner, id string) (things.Thing, error) {
func (svc *mainfluxThings) ViewThing(_ context.Context, token, id string) (things.Thing, error) {
svc.mu.Lock()
defer svc.mu.Unlock()

user, err := svc.auth.Identify(context.Background(), &protomfx.Token{Value: owner})
if err != nil {
return things.Thing{}, errors.ErrAuthentication
}

if t, ok := svc.things[id]; ok && t.OwnerID == user.Email {
if t, ok := svc.things[id]; ok {
return t, nil

}

return things.Thing{}, errors.ErrNotFound
}

func (svc *mainfluxThings) Connect(_ context.Context, owner, chID string, thIDs []string) error {
func (svc *mainfluxThings) Connect(_ context.Context, token string, chID string, thIDs []string) error {
svc.mu.Lock()
defer svc.mu.Unlock()

user, err := svc.auth.Identify(context.Background(), &protomfx.Token{Value: owner})
if err != nil {
return errors.ErrAuthentication
}

if svc.channels[chID].OwnerID != user.Email {
return errors.ErrAuthentication
}
svc.connections[chID] = append(svc.connections[chID], thIDs...)

return nil
}

func (svc *mainfluxThings) Disconnect(_ context.Context, owner, chID string, thIDs []string) error {
func (svc *mainfluxThings) Disconnect(_ context.Context, token string, chID string, thIDs []string) error {
svc.mu.Lock()
defer svc.mu.Unlock()

user, err := svc.auth.Identify(context.Background(), &protomfx.Token{Value: owner})
if err != nil {
return errors.ErrAuthentication
}

if svc.channels[chID].OwnerID != user.Email {
return errors.ErrAuthentication
}

ids := svc.connections[chID]
var count int
var newConns []string
Expand All @@ -122,21 +95,12 @@ func (svc *mainfluxThings) Disconnect(_ context.Context, owner, chID string, thI
return nil
}

func (svc *mainfluxThings) ListThingsByIDs(_ context.Context, thingIDs []string) (things.ThingsPage, error) {
panic("not implemented")
}

func (svc *mainfluxThings) RemoveThings(_ context.Context, owner string, ids ...string) error {
func (svc *mainfluxThings) RemoveThings(_ context.Context, token string, ids ...string) error {
svc.mu.Lock()
defer svc.mu.Unlock()

user, err := svc.auth.Identify(context.Background(), &protomfx.Token{Value: owner})
if err != nil {
return errors.ErrAuthentication
}

for _, id := range ids {
if t, ok := svc.things[id]; !ok || t.OwnerID != user.Email {
if _, ok := svc.things[id]; !ok {
return errors.ErrNotFound
}

Expand All @@ -146,7 +110,7 @@ func (svc *mainfluxThings) RemoveThings(_ context.Context, owner string, ids ...
return nil
}

func (svc *mainfluxThings) ViewChannel(_ context.Context, owner, id string) (things.Channel, error) {
func (svc *mainfluxThings) ViewChannel(_ context.Context, token, id string) (things.Channel, error) {
if c, ok := svc.channels[id]; ok {
return c, nil
}
Expand Down Expand Up @@ -181,17 +145,12 @@ func (svc *mainfluxThings) Restore(context.Context, string, things.Backup) error
panic("not implemented")
}

func (svc *mainfluxThings) CreateChannels(_ context.Context, owner string, chs ...things.Channel) ([]things.Channel, error) {
func (svc *mainfluxThings) CreateChannels(_ context.Context, token string, chs ...things.Channel) ([]things.Channel, error) {
svc.mu.Lock()
defer svc.mu.Unlock()

user, err := svc.auth.Identify(context.Background(), &protomfx.Token{Value: owner})
if err != nil {
return []things.Channel{}, errors.ErrAuthentication
}
for i := range chs {
svc.counter++
chs[i].OwnerID = user.Id
chs[i].ID = strconv.FormatUint(svc.counter, 10)
svc.channels[chs[i].ID] = chs[i]
}
Expand Down Expand Up @@ -219,10 +178,6 @@ func (svc *mainfluxThings) GetConnByKey(context.Context, string) (things.Connect
panic("not implemented")
}

func (svc *mainfluxThings) IsChannelOwner(context.Context, string, string) error {
panic("not implemented")
}

func (svc *mainfluxThings) Authorize(context.Context, things.AuthorizeReq) error {
panic("not implemented")
}
Expand All @@ -243,10 +198,6 @@ func (svc *mainfluxThings) ListThingsByGroup(_ context.Context, token, groupID s
panic("not implemented")
}

func (svc *mainfluxThings) ListGroupThingsByChannel(_ context.Context, token, grID, chID string, pm things.PageMetadata) (things.ThingsPage, error) {
panic("not implemented")
}

func (svc *mainfluxThings) CreateGroups(_ context.Context, token string, groups ...things.Group) ([]things.Group, error) {
panic("not implemented")
}
Expand Down
Loading

0 comments on commit fd09e97

Please sign in to comment.