Skip to content
This repository has been archived by the owner on Oct 14, 2024. It is now read-only.

NOISSUE - Fix Update user roles, Listing and retrieve users with role #76

Merged
merged 5 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions api/openapi/things.yml
Original file line number Diff line number Diff line change
Expand Up @@ -196,32 +196,6 @@ paths:
"500":
$ref: "#/components/responses/ServiceError"

/things/{thingID}/owner:
patch:
summary: Updates the thing owner.
description: |
Updates owner for the thing with provided ID. Owner is updated using
authorization token and a new owner identifier received in request.
tags:
- Things
parameters:
- $ref: "#/components/parameters/ThingID"
requestBody:
$ref: "#/components/requestBodies/ThingUpdateOwnerReq"
security:
- bearerAuth: []
responses:
"200":
$ref: "#/components/responses/ThingRes"
"400":
description: Failed due to malformed JSON.
"404":
description: Failed due to non existing thing.
"401":
description: Missing or invalid access token provided.
"500":
$ref: "#/components/responses/ServiceError"

/things/{thingID}/secret:
patch:
summary: Updates Secret of the identified thing.
Expand Down Expand Up @@ -1648,14 +1622,6 @@ components:
schema:
$ref: "#/components/schemas/ThingSecret"

ThingUpdateOwnerReq:
description: JSON-formated document describing the owner of thing to be update
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/ThingOwner"

ShareThingReq:
description: JSON-formated document describing the policy related to sharing things
required: true
Expand Down
26 changes: 12 additions & 14 deletions api/openapi/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,17 @@ paths:
"500":
$ref: "#/components/responses/ServiceError"

/users/{userID}/owner:
/users/{userID}/role:
patch:
summary: Updates the user owner.
summary: Updates the user role.
description: |
Updates owner for the user with provided ID. Owner is updated using
authorization token and a new owner identifier received in request.
Updates role for the user with provided ID.
tags:
- Users
parameters:
- $ref: "#/components/parameters/UserID"
requestBody:
$ref: "#/components/requestBodies/UserUpdateOwnerReq"
$ref: "#/components/requestBodies/UserUpdateRoleReq"
security:
- bearerAuth: []
responses:
Expand Down Expand Up @@ -299,8 +298,6 @@ paths:
authorization token and the new received info.
tags:
- Users
parameters:
- $ref: "#/components/parameters/UserID"
requestBody:
$ref: "#/components/requestBodies/UserUpdateSecretReq"
security:
Expand Down Expand Up @@ -1264,13 +1261,14 @@ components:
- old_secret
- new_secret

UserOwner:
UserRole:
type: object
properties:
owner:
role:
type: string
example: [email protected]
description: User owner for example email address.
enum: ["admin","user"]
example: user
description: User role example.
required:
- owner

Expand Down Expand Up @@ -1658,13 +1656,13 @@ components:
schema:
$ref: "#/components/schemas/UserSecret"

UserUpdateOwnerReq:
description: JSON-formated document describing the owner of user to be update
UserUpdateRoleReq:
description: JSON-formated document describing the role of the user to be updated
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/UserOwner"
$ref: "#/components/schemas/UserRole"

GroupCreateReq:
description: JSON-formatted document describing the new group to be registered
Expand Down
28 changes: 16 additions & 12 deletions pkg/clients/postgres/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,15 @@ func (repo ClientRepository) RetrieveByIdentity(ctx context.Context, identity st
}

func (repo ClientRepository) RetrieveAll(ctx context.Context, pm clients.Page) (clients.ClientsPage, error) {
query, err := pageQuery(pm)
query, err := PageQuery(pm)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

q := fmt.Sprintf(`SELECT c.id, c.name, c.tags, c.identity, c.metadata, COALESCE(c.owner_id, '') AS owner_id, c.status,
c.created_at, c.updated_at, COALESCE(c.updated_by, '') AS updated_by FROM clients c %s ORDER BY c.created_at LIMIT :limit OFFSET :offset;`, query)

dbPage, err := toDBClientsPage(pm)
dbPage, err := ToDBClientsPage(pm)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
}
Expand Down Expand Up @@ -198,14 +198,14 @@ func (repo ClientRepository) RetrieveAll(ctx context.Context, pm clients.Page) (
}

func (repo ClientRepository) RetrieveAllBasicInfo(ctx context.Context, pm clients.Page) (clients.ClientsPage, error) {
query, err := pageQuery(pm)
query, err := PageQuery(pm)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

q := fmt.Sprintf(`SELECT c.id, c.name, c.tags, c.identity FROM clients c %s ORDER BY c.created_at LIMIT :limit OFFSET :offset;`, query)

dbPage, err := toDBClientsPage(pm)
dbPage, err := ToDBClientsPage(pm)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
}
Expand Down Expand Up @@ -254,15 +254,15 @@ func (repo ClientRepository) RetrieveAllByIDs(ctx context.Context, pm clients.Pa
Page: clients.Page{Total: pm.Total, Offset: pm.Offset, Limit: pm.Limit},
}, nil
}
query, err := pageQuery(pm)
query, err := PageQuery(pm)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
}

q := fmt.Sprintf(`SELECT c.id, c.name, c.tags, c.identity, c.metadata, COALESCE(c.owner_id, '') AS owner_id, c.status,
c.created_at, c.updated_at, COALESCE(c.updated_by, '') AS updated_by FROM clients c %s ORDER BY c.created_at LIMIT :limit OFFSET :offset;`, query)

dbPage, err := toDBClientsPage(pm)
dbPage, err := ToDBClientsPage(pm)
if err != nil {
return clients.ClientsPage{}, errors.Wrap(postgres.ErrFailedToRetrieveAll, err)
}
Expand Down Expand Up @@ -342,7 +342,7 @@ type DBClient struct {
UpdatedBy *string `db:"updated_by,omitempty"`
Groups []groups.Group `db:"groups,omitempty"`
Status clients.Status `db:"status,omitempty"`
Role clients.Role `db:"role,omitempty"`
Role *clients.Role `db:"role,omitempty"`
}

func ToDBClient(c clients.Client) (DBClient, error) {
Expand Down Expand Up @@ -383,7 +383,7 @@ func ToDBClient(c clients.Client) (DBClient, error) {
UpdatedAt: updatedAt,
UpdatedBy: updatedBy,
Status: c.Status,
Role: c.Role,
Role: &c.Role,
}, nil
}

Expand Down Expand Up @@ -411,7 +411,7 @@ func ToClient(c DBClient) (clients.Client, error) {
updatedAt = c.UpdatedAt.Time
}

return clients.Client{
cli := clients.Client{
ID: c.ID,
Name: c.Name,
Tags: tags,
Expand All @@ -425,10 +425,14 @@ func ToClient(c DBClient) (clients.Client, error) {
UpdatedAt: updatedAt,
UpdatedBy: updatedBy,
Status: c.Status,
}, nil
}
if c.Role != nil {
cli.Role = *c.Role
}
return cli, nil
}

func toDBClientsPage(pm clients.Page) (dbClientsPage, error) {
func ToDBClientsPage(pm clients.Page) (dbClientsPage, error) {
_, data, err := postgres.CreateMetadataQuery("", pm.Metadata)
if err != nil {
return dbClientsPage{}, errors.Wrap(repoerr.ErrViewEntity, err)
Expand Down Expand Up @@ -465,7 +469,7 @@ type dbClientsPage struct {
Role uint8 `db:"role"`
}

func pageQuery(pm clients.Page) (string, error) {
func PageQuery(pm clients.Page) (string, error) {
mq, _, err := postgres.CreateMetadataQuery("", pm.Metadata)
if err != nil {
return "", errors.Wrap(repoerr.ErrViewEntity, err)
Expand Down
19 changes: 18 additions & 1 deletion pkg/sdk/go/channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const channelsEndpoint = "channels"

// Channel represents magistrala channel.
type Channel struct {
ID string `json:"id"`
ID string `json:"id,omitempty"`
OwnerID string `json:"owner_id,omitempty"`
ParentID string `json:"parent_id,omitempty"`
Name string `json:"name,omitempty"`
Expand All @@ -28,6 +28,7 @@ type Channel struct {
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Status string `json:"status,omitempty"`
Permissions []string `json:"permissions,omitempty"`
}

func (sdk mgSDK) CreateChannel(c Channel, token string) (Channel, errors.SDKError) {
Expand Down Expand Up @@ -125,6 +126,22 @@ func (sdk mgSDK) Channel(id, token string) (Channel, errors.SDKError) {
return c, nil
}

func (sdk mgSDK) ChannelPermissions(id, token string) (Channel, errors.SDKError) {
url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, channelsEndpoint, id, permissionsEndpoint)

_, body, err := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
if err != nil {
return Channel{}, err
}

var c Channel
if err := json.Unmarshal(body, &c); err != nil {
return Channel{}, errors.NewSDKError(err)
}

return c, nil
}

func (sdk mgSDK) UpdateChannel(c Channel, token string) (Channel, errors.SDKError) {
data, err := json.Marshal(c)
if err != nil {
Expand Down
18 changes: 17 additions & 1 deletion pkg/sdk/go/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const (
// Path in a tree consisting of group IDs
// Paths are unique per owner.
type Group struct {
ID string `json:"id"`
ID string `json:"id,omitempty"`
OwnerID string `json:"owner_id,omitempty"`
ParentID string `json:"parent_id,omitempty"`
Name string `json:"name,omitempty"`
Expand All @@ -35,6 +35,7 @@ type Group struct {
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Status string `json:"status,omitempty"`
Permissions []string `json:"permissions,omitempty"`
}

func (sdk mgSDK) CreateGroup(g Group, token string) (Group, errors.SDKError) {
Expand Down Expand Up @@ -116,6 +117,21 @@ func (sdk mgSDK) Group(id, token string) (Group, errors.SDKError) {
return t, nil
}

func (sdk mgSDK) GroupPermissions(id, token string) (Group, errors.SDKError) {
url := fmt.Sprintf("%s/%s/%s/%s", sdk.usersURL, groupsEndpoint, id, permissionsEndpoint)

_, body, err := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
if err != nil {
return Group{}, err
}

var t Group
if err := json.Unmarshal(body, &t); err != nil {
return Group{}, errors.NewSDKError(err)
}

return t, nil
}
func (sdk mgSDK) UpdateGroup(g Group, token string) (Group, errors.SDKError) {
data, err := json.Marshal(g)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ type SDK interface {
// fmt.Println(thing)
Thing(id, token string) (Thing, errors.SDKError)

// ThingPermissions returns user permissions on the thing id.
//
// example:
// thing, _ := sdk.Thing("thingID", "token")
// fmt.Println(thing)
ThingPermissions(id, token string) (Thing, errors.SDKError)

// UpdateThing updates existing thing.
//
// example:
Expand Down Expand Up @@ -494,6 +501,13 @@ type SDK interface {
// fmt.Println(group)
Group(id, token string) (Group, errors.SDKError)

// GroupPermissions returns user permissions by group ID.
//
// example:
// group, _ := sdk.Group("groupID", "token")
// fmt.Println(group)
GroupPermissions(id, token string) (Group, errors.SDKError)

// UpdateGroup updates existing group.
//
// example:
Expand Down Expand Up @@ -633,6 +647,13 @@ type SDK interface {
// fmt.Println(channel)
Channel(id, token string) (Channel, errors.SDKError)

// ChannelPermissions returns user permissions on the channel ID.
//
// example:
// channel, _ := sdk.Channel("channelID", "token")
// fmt.Println(channel)
ChannelPermissions(id, token string) (Channel, errors.SDKError)

// UpdateChannel updates existing channel.
//
// example:
Expand Down
32 changes: 25 additions & 7 deletions pkg/sdk/go/things.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import (
)

const (
thingsEndpoint = "things"
connectEndpoint = "connect"
disconnectEndpoint = "disconnect"
identifyEndpoint = "identify"
shareEndpoint = "share"
unshareEndpoint = "unshare"
permissionsEndpoint = "permissions"
thingsEndpoint = "things"
connectEndpoint = "connect"
disconnectEndpoint = "disconnect"
identifyEndpoint = "identify"
shareEndpoint = "share"
unshareEndpoint = "unshare"
)

// Thing represents magistrala thing.
type Thing struct {
ID string `json:"id"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Credentials Credentials `json:"credentials"`
Tags []string `json:"tags,omitempty"`
Expand All @@ -32,6 +33,7 @@ type Thing struct {
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
Status string `json:"status,omitempty"`
Permissions []string `json:"permissions,omitempty"`
}

func (sdk mgSDK) CreateThing(thing Thing, token string) (Thing, errors.SDKError) {
Expand Down Expand Up @@ -130,6 +132,22 @@ func (sdk mgSDK) Thing(id, token string) (Thing, errors.SDKError) {
return t, nil
}

func (sdk mgSDK) ThingPermissions(id, token string) (Thing, errors.SDKError) {
url := fmt.Sprintf("%s/%s/%s/%s", sdk.thingsURL, thingsEndpoint, id, permissionsEndpoint)

_, body, sdkerr := sdk.processRequest(http.MethodGet, url, token, nil, nil, http.StatusOK)
if sdkerr != nil {
return Thing{}, sdkerr
}

var t Thing
if err := json.Unmarshal(body, &t); err != nil {
return Thing{}, errors.NewSDKError(err)
}

return t, nil
}

func (sdk mgSDK) UpdateThing(t Thing, token string) (Thing, errors.SDKError) {
data, err := json.Marshal(t)
if err != nil {
Expand Down
Loading
Loading