From 64d290bcaef6b89bda1f0f8d0bf188715ca506af Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 20 Sep 2018 19:42:01 +0200 Subject: [PATCH] Added endpoint to search for users --- REST-Tests/users.http | 11 +++++++++ models/users_list.go | 5 ---- public/swagger/swagger.v1.json | 33 +++++++++++++++++++++++++ routes/api/v1/user_list.go | 44 ++++++++++++++++++++++++++++++++++ routes/routes.go | 6 +++-- 5 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 REST-Tests/users.http create mode 100644 routes/api/v1/user_list.go diff --git a/REST-Tests/users.http b/REST-Tests/users.http new file mode 100644 index 00000000..a082437f --- /dev/null +++ b/REST-Tests/users.http @@ -0,0 +1,11 @@ + +# Get all users +GET http://localhost:8080/api/v1/users +Authorization: Bearer {{auth_token}} + +###### +# Search for a user +GET http://localhost:8080/api/v1/users?s=3 +Authorization: Bearer {{auth_token}} + +### \ No newline at end of file diff --git a/models/users_list.go b/models/users_list.go index bc91ea9f..9fbe4bb3 100644 --- a/models/users_list.go +++ b/models/users_list.go @@ -11,11 +11,6 @@ func ListUsers(searchterm string) (users []User, err error) { Find(&users) } - // Obfuscate the password. Selecting everything except the password didn't work. - for i := range users { - users[i].Password = "" - } - if err != nil { return []User{}, err } diff --git a/public/swagger/swagger.v1.json b/public/swagger/swagger.v1.json index fc69a782..c4d98434 100644 --- a/public/swagger/swagger.v1.json +++ b/public/swagger/swagger.v1.json @@ -1542,6 +1542,39 @@ } } } + }, + "/users": { + "get": { + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "user" + ], + "summary": "Lists all users", + "operationId": "list", + "parameters": [ + { + "description": "The searchterm to filter users", + "name": "s", + "in": "query" + } + ], + "responses": { + "200": { + "$ref": "#/responses/User" + }, + "400": { + "$ref": "#/responses/Message" + }, + "500": { + "$ref": "#/responses/Message" + } + } + } } }, "definitions": { diff --git a/routes/api/v1/user_list.go b/routes/api/v1/user_list.go new file mode 100644 index 00000000..c165c783 --- /dev/null +++ b/routes/api/v1/user_list.go @@ -0,0 +1,44 @@ +package v1 + +import ( + "code.vikunja.io/api/models" + "github.com/labstack/echo" + "net/http" +) + +// UserList gets all information about a user +func UserList(c echo.Context) error { + + // swagger:operation GET /users user list + // --- + // summary: Lists all users + // consumes: + // - application/json + // produces: + // - application/json + // parameters: + // - name: s + // description: A searchterm to search for a user by its username + // in: query + // responses: + // "200": + // "$ref": "#/responses/User" + // "400": + // "$ref": "#/responses/Message" + // "500": + // "$ref": "#/responses/Message" + + s := c.QueryParam("s") + users, err := models.ListUsers(s) + if err != nil { + models.Log.Error(err.Error()) + return echo.NewHTTPError(http.StatusInternalServerError, "An error occured.") + } + + // Obfuscate the mailadresses + for in := range users { + users[in].Email = "" + } + + return c.JSON(http.StatusOK, users) +} diff --git a/routes/routes.go b/routes/routes.go index 5b819f83..ed6d9cd8 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -71,6 +71,10 @@ func RegisterRoutes(e *echo.Echo) { a.Use(middleware.JWT([]byte(viper.GetString("service.JWTSecret")))) a.POST("/tokenTest", apiv1.CheckToken) + // User stuff + a.GET("/user", apiv1.UserShow) + a.GET("/users", apiv1.UserList) + listHandler := &crud.WebHandler{ CObject: &models.List{}, } @@ -143,6 +147,4 @@ func RegisterRoutes(e *echo.Echo) { } a.PUT("/teams/:team/members", teamMemberHandler.CreateWeb) a.DELETE("/teams/:team/members/:user", teamMemberHandler.DeleteWeb) - - a.GET("/user", apiv1.UserShow) }