Skip to content

Commit

Permalink
add user creation functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
TanmoySG committed Sep 26, 2024
1 parent 2c5cd5e commit 4bb4784
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 20 deletions.
15 changes: 8 additions & 7 deletions documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,16 @@ While starting a wdb instance an `admin` user profile can be created by setting

### Create User

Make POST request to the `/api/users` endpoint, passing username and password to create user.
Make POST request to the `/api/users` endpoint, passing username and password in autorization and metadata (if any) as JSON body, to create user.

```http
POST /api/users HTTP/1.1
Content-Type: application/json
Authorization: Basic
{
"username": "username",
"password": "password"
"metadata1": "value",
"metadata2": "value"
}
```

Expand Down Expand Up @@ -573,7 +574,7 @@ Some of the Privileges available for use in wunderDb and associated actions.
| readCollection | collection privileges | read/fetch collections in database |
| updateCollection | collection privileges | update collections in database |
| deleteCollection | collection privileges | delete collection from database |
| addData | collection privileges | add/insert data in collection |
| readData | collection privileges | read/fetch data from collection |
| updateData | collection privileges | update data in collection |
| deleteData | collection privileges | delete data from collection |
| addRecords | collection privileges | add/insert data in collection |
| readRecords | collection privileges | read/fetch data from collection |
| updateRecords | collection privileges | update data in collection |
| deleteRecords | collection privileges | delete data from collection |
2 changes: 1 addition & 1 deletion internal/filter/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func filter(primaryKey model.Identifier, data map[model.Identifier]*model.Record
}
}

func (f Filter) Filter(primaryKey model.Identifier, data map[model.Identifier]*model.Record) (map[model.Identifier]*model.Record) {
func (f Filter) Filter(primaryKey model.Identifier, data map[model.Identifier]*model.Record) map[model.Identifier]*model.Record {
filteredData := make(map[model.Identifier]*model.Record)

filter(primaryKey, data, f, func(id *model.Identifier, record *model.Record) {
Expand Down
15 changes: 6 additions & 9 deletions internal/server/handlers/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/TanmoySG/wunderDB/internal/privileges"
"github.com/TanmoySG/wunderDB/internal/server/response"
"github.com/TanmoySG/wunderDB/internal/users/authentication"
"github.com/TanmoySG/wunderDB/model"
er "github.com/TanmoySG/wunderDB/pkg/wdb/errors"
"github.com/gofiber/fiber/v2"
Expand All @@ -15,11 +16,6 @@ type userPermissions struct {
Permission model.Permissions `json:"permissions" xml:"permissions" form:"permissions" validate:"required,dive"`
}

type newUser struct {
Username string `json:"username" xml:"username" form:"username" validate:"required"`
Password string `json:"password" xml:"password" form:"password" validate:"required"`
}

func (wh wdbHandlers) LoginUser(c *fiber.Ctx) error {
privilege := privileges.LoginUser

Expand All @@ -45,15 +41,16 @@ func (wh wdbHandlers) CreateUser(c *fiber.Ctx) error {
privilege := privileges.CreateUser
var apiError *er.WdbError

u := new(newUser)
if err := c.BodyParser(u); err != nil {
metadata := new(model.Metadata)
if err := c.BodyParser(metadata); err != nil {
return err
}

if err := validateRequest(u); err != nil {
username, password, err := authentication.HandleUserCredentials(c.Get(Authorization))
if err != nil {
apiError = err
} else {
apiError = wh.wdbClient.CreateUser(model.Identifier(u.Username), u.Password)
apiError = wh.wdbClient.CreateUser(model.Identifier(*username), *password, *metadata)
}

resp := response.Format(privilege, apiError, nil, *wh.notices...)
Expand Down
4 changes: 2 additions & 2 deletions pkg/wdb/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

var wildcard = privileges.Wildcard

func (wdb wdbClient) CreateUser(userID model.Identifier, password string) *er.WdbError {
func (wdb wdbClient) CreateUser(userID model.Identifier, password string, metadata model.Metadata) *er.WdbError {
if !wdb.safeName.Check(userID.String()) {
return &er.EntityNameFormatError
}
Expand All @@ -18,7 +18,7 @@ func (wdb wdbClient) CreateUser(userID model.Identifier, password string) *er.Wd
return &er.UserAlreadyExistsError
}
hashedPassword := authentication.Hash(password, wdb.HashingAlgorithm)
wdb.Users.CreateUser(userID, hashedPassword, wdb.HashingAlgorithm, model.Metadata{})
wdb.Users.CreateUser(userID, hashedPassword, wdb.HashingAlgorithm, metadata)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/wdb/wdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type Client interface {
QueryRecords(databaseId, collectionId model.Identifier, query string, mode records.QueryType) (interface{}, *er.WdbError)

// Users Methods
CreateUser(userID model.Identifier, password string) *er.WdbError
CreateUser(userID model.Identifier, password string, metadata model.Metadata) *er.WdbError
AuthenticateUser(userID model.Identifier, password string) (bool, *er.WdbError)
CheckUserPermissions(userID model.Identifier, privilege string, entities model.Entities) (bool, *er.WdbError)
GrantRole(userID model.Identifier, permissions model.Permissions) *er.WdbError
Expand Down

0 comments on commit 4bb4784

Please sign in to comment.