Added endpoint to search for users

This commit is contained in:
kolaente 2018-09-20 19:42:01 +02:00
parent 3401d7ab2c
commit 64d290bcae
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
5 changed files with 92 additions and 7 deletions

11
REST-Tests/users.http Normal file
View file

@ -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}}
###

View file

@ -11,11 +11,6 @@ func ListUsers(searchterm string) (users []User, err error) {
Find(&users) Find(&users)
} }
// Obfuscate the password. Selecting everything except the password didn't work.
for i := range users {
users[i].Password = ""
}
if err != nil { if err != nil {
return []User{}, err return []User{}, err
} }

View file

@ -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": { "definitions": {

View file

@ -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)
}

View file

@ -71,6 +71,10 @@ func RegisterRoutes(e *echo.Echo) {
a.Use(middleware.JWT([]byte(viper.GetString("service.JWTSecret")))) a.Use(middleware.JWT([]byte(viper.GetString("service.JWTSecret"))))
a.POST("/tokenTest", apiv1.CheckToken) a.POST("/tokenTest", apiv1.CheckToken)
// User stuff
a.GET("/user", apiv1.UserShow)
a.GET("/users", apiv1.UserList)
listHandler := &crud.WebHandler{ listHandler := &crud.WebHandler{
CObject: &models.List{}, CObject: &models.List{},
} }
@ -143,6 +147,4 @@ func RegisterRoutes(e *echo.Echo) {
} }
a.PUT("/teams/:team/members", teamMemberHandler.CreateWeb) a.PUT("/teams/:team/members", teamMemberHandler.CreateWeb)
a.DELETE("/teams/:team/members/:user", teamMemberHandler.DeleteWeb) a.DELETE("/teams/:team/members/:user", teamMemberHandler.DeleteWeb)
a.GET("/user", apiv1.UserShow)
} }