Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement dropUser command #3866

Merged
merged 19 commits into from
Dec 20, 2023
Prev Previous commit
Next Next commit
wip
  • Loading branch information
henvic committed Dec 12, 2023
commit 25afee760e1fe8ace8439895f4ce9af1820ab07d
31 changes: 28 additions & 3 deletions internal/handler/msg_createuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import (
"context"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/handler/common"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
Expand All @@ -33,33 +34,57 @@

// https://www.mongodb.com/docs/manual/reference/command/createUser/

username, err := common.GetRequiredParam[string](document, document.Command())
if err != nil {
return nil, err
}

Check warning on line 40 in internal/handler/msg_createuser.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_createuser.go#L37-L40

Added lines #L37 - L40 were not covered by tests

password, err := common.GetRequiredParam[string](document, "pwd")
_, err = common.GetRequiredParam[string](document, "pwd")
if err != nil {
return nil, err
}

Check warning on line 45 in internal/handler/msg_createuser.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_createuser.go#L42-L45

Added lines #L42 - L45 were not covered by tests

if err := common.UnimplementedNonDefault(document, "roles", func(v any) bool {
roles, ok := v.(*types.Array)
return ok && roles.Len() == 0
}); err != nil {
return nil, err
}

Check warning on line 52 in internal/handler/msg_createuser.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_createuser.go#L47-L52

Added lines #L47 - L52 were not covered by tests

_ = username
_ = password
dbName, err := common.GetRequiredParam[string](document, "$db")
if err != nil {
return nil, err
}

Check warning on line 57 in internal/handler/msg_createuser.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_createuser.go#L54-L57

Added lines #L54 - L57 were not covered by tests

db, err := h.b.Database(dbName)
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 62 in internal/handler/msg_createuser.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_createuser.go#L59-L62

Added lines #L59 - L62 were not covered by tests

collection, err := db.Collection("system.users")
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 67 in internal/handler/msg_createuser.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_createuser.go#L64-L67

Added lines #L64 - L67 were not covered by tests

_, err = collection.InsertAll(ctx, &backends.InsertAllParams{
Docs: []*types.Document{must.NotFail(types.NewDocument(
"createdUser", username,
"roles", []string{},
"pwd", "password", // TODO: hash the password.
)),
},
})
if err != nil {
return nil, lazyerrors.Error(err)
}

Check warning on line 80 in internal/handler/msg_createuser.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_createuser.go#L69-L80

Added lines #L69 - L80 were not covered by tests
// TODO https://github.com/FerretDB/FerretDB/issues/1491
var reply wire.OpMsg
must.NoError(reply.SetSections(wire.OpMsgSection{
Documents: []*types.Document{must.NotFail(types.NewDocument(
"ok", float64(1),
))},
}))

return &reply, nil

Check warning on line 89 in internal/handler/msg_createuser.go

View check run for this annotation

Codecov / codecov/patch

internal/handler/msg_createuser.go#L82-L89

Added lines #L82 - L89 were not covered by tests
}
Loading