diff options
author | 2017-12-26 12:10:48 -0800 | |
---|---|---|
committer | 2017-12-26 12:10:48 -0800 | |
commit | c3c27e3637e63af5fe2d9bb45712838740063906 (patch) | |
tree | d40bc55d67bd18de3bd8fb146940f676c7c651e2 /server/api/controller/user.go | |
parent | d5b8f2fb8839189bdf6893da0f86f3bb26001d3d (diff) | |
download | v2-c3c27e3637e63af5fe2d9bb45712838740063906.tar.gz v2-c3c27e3637e63af5fe2d9bb45712838740063906.tar.zst v2-c3c27e3637e63af5fe2d9bb45712838740063906.zip |
Add API handler to fetch user by username
Diffstat (limited to 'server/api/controller/user.go')
-rw-r--r-- | server/api/controller/user.go | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/server/api/controller/user.go b/server/api/controller/user.go index 8ca7fd2c..a9259081 100644 --- a/server/api/controller/user.go +++ b/server/api/controller/user.go @@ -88,8 +88,8 @@ func (c *Controller) UpdateUser(ctx *core.Context, request *core.Request, respon response.JSON().Created(originalUser) } -// GetUsers is the API handler to get the list of users. -func (c *Controller) GetUsers(ctx *core.Context, request *core.Request, response *core.Response) { +// Users is the API handler to get the list of users. +func (c *Controller) Users(ctx *core.Context, request *core.Request, response *core.Response) { if !ctx.IsAdminUser() { response.JSON().Forbidden() return @@ -104,8 +104,8 @@ func (c *Controller) GetUsers(ctx *core.Context, request *core.Request, response response.JSON().Standard(users) } -// GetUser is the API handler to fetch the given user. -func (c *Controller) GetUser(ctx *core.Context, request *core.Request, response *core.Response) { +// UserByID is the API handler to fetch the given user by the ID. +func (c *Controller) UserByID(ctx *core.Context, request *core.Request, response *core.Response) { if !ctx.IsAdminUser() { response.JSON().Forbidden() return @@ -131,6 +131,28 @@ func (c *Controller) GetUser(ctx *core.Context, request *core.Request, response response.JSON().Standard(user) } +// UserByUsername is the API handler to fetch the given user by the username. +func (c *Controller) UserByUsername(ctx *core.Context, request *core.Request, response *core.Response) { + if !ctx.IsAdminUser() { + response.JSON().Forbidden() + return + } + + username := request.StringParam("username", "") + user, err := c.store.UserByUsername(username) + if err != nil { + response.JSON().BadRequest(errors.New("Unable to fetch this user from the database")) + return + } + + if user == nil { + response.JSON().NotFound(errors.New("User not found")) + return + } + + response.JSON().Standard(user) +} + // RemoveUser is the API handler to remove an existing user. func (c *Controller) RemoveUser(ctx *core.Context, request *core.Request, response *core.Response) { if !ctx.IsAdminUser() { |