vikunja-api/routes/crud/read_one.go

38 lines
1.1 KiB
Go
Raw Normal View History

2018-07-11 01:45:11 +02:00
package crud
import (
2018-07-25 16:24:46 +02:00
"code.vikunja.io/api/models"
"github.com/labstack/echo"
2018-07-11 01:45:11 +02:00
"net/http"
)
// ReadOneWeb is the webhandler to get one object
func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
// Get our model
currentStruct := c.EmptyStruct()
2018-07-11 01:45:11 +02:00
2018-07-21 15:08:46 +02:00
// Get the object & bind params to struct
if err := ParamBinder(currentStruct, ctx); err != nil {
2018-07-21 15:08:46 +02:00
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided.")
2018-07-11 01:45:11 +02:00
}
// Get our object
err := currentStruct.ReadOne()
2018-07-11 01:45:11 +02:00
if err != nil {
2018-10-06 15:04:14 +02:00
return HandleHTTPError(err)
2018-07-11 01:45:11 +02:00
}
2018-07-12 13:43:42 +02:00
// Check rights
// We can only check the rights on a full object, which is why we need to check it afterwards
currentUser, err := models.GetCurrentUser(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !currentStruct.CanRead(&currentUser) {
2018-09-19 08:35:53 +02:00
models.Log.Noticef("%s [ID: %d] tried to read while not having the rights for it", currentUser.Username, currentUser.ID)
2018-07-12 13:43:42 +02:00
return echo.NewHTTPError(http.StatusForbidden, "You don't have the right to see this")
}
return ctx.JSON(http.StatusOK, currentStruct)
2018-07-11 01:45:11 +02:00
}