From c3c27e3637e63af5fe2d9bb45712838740063906 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Tue, 26 Dec 2017 12:10:48 -0800 Subject: Add API handler to fetch user by username --- server/api/controller/user.go | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'server/api/controller/user.go') 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() { -- cgit v1.2.3