Added endpoint to search for users
This commit is contained in:
parent
3401d7ab2c
commit
64d290bcae
5 changed files with 92 additions and 7 deletions
11
REST-Tests/users.http
Normal file
11
REST-Tests/users.http
Normal 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}}
|
||||
|
||||
###
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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": {
|
||||
|
|
44
routes/api/v1/user_list.go
Normal file
44
routes/api/v1/user_list.go
Normal 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)
|
||||
}
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue