-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(admin): add user list/get/remove method (#67)
* feat(admin): add user list/get/remove method * add admin user service to add user get, list and remove method * rename misspelled file * add router for user requests * extend webauthn user persister * update spec Closes: #22 * fix(review): fix review findings * update public spec for transaction list * cleanup public spec * switch tenant_id path entries with reference to component * add transaction tag * add user_id as path param * add paging to user list in admin api call * add paging to admin spec * rename userid to user_id in transaction list handler --------- Co-authored-by: Stefan Jacobi <[email protected]>
- Loading branch information
1 parent
49f70ff
commit 3a1c27b
Showing
12 changed files
with
729 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package request | ||
|
||
type UserListRequest struct { | ||
PerPage int `query:"per_page"` | ||
Page int `query:"page"` | ||
SortDirection string `query:"sort_direction"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package response | ||
|
||
import ( | ||
"github.com/gofrs/uuid" | ||
"github.com/teamhanko/passkey-server/api/dto/response" | ||
"github.com/teamhanko/passkey-server/persistence/models" | ||
) | ||
|
||
type UserListDto struct { | ||
ID uuid.UUID `json:"id"` | ||
UserID string `json:"user_id"` | ||
Name string `json:"name"` | ||
Icon string `json:"icon"` | ||
DisplayName string `json:"display_name"` | ||
} | ||
|
||
func UserListDtoFromModel(user models.WebauthnUser) UserListDto { | ||
return UserListDto{ | ||
ID: user.ID, | ||
UserID: user.UserID, | ||
Name: user.Name, | ||
Icon: user.Icon, | ||
DisplayName: user.DisplayName, | ||
} | ||
} | ||
|
||
type UserGetDto struct { | ||
UserListDto | ||
Credentials []response.CredentialDto `json:"credentials"` | ||
Transactions []response.TransactionDto `json:"transactions"` | ||
} | ||
|
||
func UserGetDtoFromModel(user models.WebauthnUser) UserGetDto { | ||
dto := UserGetDto{ | ||
UserListDto: UserListDtoFromModel(user), | ||
Credentials: make([]response.CredentialDto, 0), | ||
Transactions: make([]response.TransactionDto, 0), | ||
} | ||
|
||
for _, credential := range user.WebauthnCredentials { | ||
dto.Credentials = append(dto.Credentials, response.CredentialDtoFromModel(credential)) | ||
} | ||
|
||
for _, transaction := range user.Transactions { | ||
dto.Transactions = append(dto.Transactions, response.TransactionDtoFromModel(transaction)) | ||
} | ||
|
||
return dto | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package admin | ||
|
||
import ( | ||
"fmt" | ||
"github.com/gobuffalo/pop/v6" | ||
"github.com/gofrs/uuid" | ||
"github.com/labstack/echo/v4" | ||
adminRequest "github.com/teamhanko/passkey-server/api/dto/admin/request" | ||
"github.com/teamhanko/passkey-server/api/helper" | ||
"github.com/teamhanko/passkey-server/api/pagination" | ||
"github.com/teamhanko/passkey-server/api/services/admin" | ||
"github.com/teamhanko/passkey-server/persistence" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type UserHandler interface { | ||
List(ctx echo.Context) error | ||
Get(ctx echo.Context) error | ||
Remove(ctx echo.Context) error | ||
} | ||
|
||
type userHandler struct { | ||
persister persistence.Persister | ||
} | ||
|
||
func NewUserHandler(persister persistence.Persister) UserHandler { | ||
return &userHandler{persister: persister} | ||
} | ||
|
||
func (uh *userHandler) List(ctx echo.Context) error { | ||
var request adminRequest.UserListRequest | ||
err := (&echo.DefaultBinder{}).BindQueryParams(ctx, &request) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, "unable to parse request") | ||
} | ||
|
||
if request.Page == 0 { | ||
request.Page = 1 | ||
} | ||
|
||
if request.PerPage == 0 { | ||
request.PerPage = 20 | ||
} | ||
|
||
if request.SortDirection == "" { | ||
request.SortDirection = "desc" | ||
} | ||
|
||
switch strings.ToLower(request.SortDirection) { | ||
case "desc", "asc": | ||
default: | ||
return echo.NewHTTPError(http.StatusBadRequest, "sort_direction must be desc or asc") | ||
} | ||
|
||
h, err := helper.GetHandlerContext(ctx) | ||
if err != nil { | ||
ctx.Logger().Error(err) | ||
return err | ||
} | ||
|
||
return uh.persister.GetConnection().Transaction(func(tx *pop.Connection) error { | ||
userPersister := uh.persister.GetWebauthnUserPersister(tx) | ||
userService := admin.NewUserService(admin.CreateUserServiceParams{ | ||
Ctx: ctx, | ||
Tenant: *h.Tenant, | ||
UserPersister: userPersister, | ||
}) | ||
|
||
users, count, err := userService.List(request) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
u, _ := url.Parse(fmt.Sprintf("%s://%s%s", ctx.Scheme(), ctx.Request().Host, ctx.Request().RequestURI)) | ||
|
||
ctx.Response().Header().Set("Link", pagination.CreateHeader(u, count, request.Page, request.PerPage)) | ||
ctx.Response().Header().Set("X-Total-Count", strconv.FormatInt(int64(count), 10)) | ||
|
||
return ctx.JSON(http.StatusOK, users) | ||
}) | ||
} | ||
|
||
func (uh *userHandler) Get(ctx echo.Context) error { | ||
h, err := helper.GetHandlerContext(ctx) | ||
if err != nil { | ||
ctx.Logger().Error(err) | ||
return err | ||
} | ||
|
||
userIdString := ctx.Param("user_id") | ||
if userIdString == "" { | ||
return echo.NewHTTPError(http.StatusBadRequest, "missing user_id") | ||
} | ||
|
||
userId, err := uuid.FromString(userIdString) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, "invalid user_id") | ||
} | ||
|
||
return uh.persister.GetConnection().Transaction(func(tx *pop.Connection) error { | ||
userPersister := uh.persister.GetWebauthnUserPersister(tx) | ||
userService := admin.NewUserService(admin.CreateUserServiceParams{ | ||
Ctx: ctx, | ||
Tenant: *h.Tenant, | ||
UserPersister: userPersister, | ||
}) | ||
|
||
user, err := userService.Get(userId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ctx.JSON(http.StatusOK, user) | ||
}) | ||
} | ||
|
||
func (uh *userHandler) Remove(ctx echo.Context) error { | ||
h, err := helper.GetHandlerContext(ctx) | ||
if err != nil { | ||
ctx.Logger().Error(err) | ||
return err | ||
} | ||
|
||
userIdString := ctx.Param("user_id") | ||
if userIdString == "" { | ||
return echo.NewHTTPError(http.StatusBadRequest, "missing user_id") | ||
} | ||
|
||
userId, err := uuid.FromString(userIdString) | ||
if err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, "invalid user_id") | ||
} | ||
|
||
return uh.persister.GetConnection().Transaction(func(tx *pop.Connection) error { | ||
userPersister := uh.persister.GetWebauthnUserPersister(tx) | ||
userService := admin.NewUserService(admin.CreateUserServiceParams{ | ||
Ctx: ctx, | ||
Tenant: *h.Tenant, | ||
UserPersister: userPersister, | ||
}) | ||
|
||
err := userService.Delete(userId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return ctx.NoContent(http.StatusNoContent) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.