2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-07-03 08:26:28 +02:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2018-10-31 13:42:38 +01:00
|
|
|
"code.vikunja.io/api/pkg/models"
|
2020-01-26 18:08:06 +01:00
|
|
|
"code.vikunja.io/api/pkg/user"
|
2019-09-20 18:16:31 +02:00
|
|
|
"code.vikunja.io/web/handler"
|
2019-05-07 21:42:24 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2018-07-03 08:26:28 +02:00
|
|
|
"net/http"
|
2018-07-12 23:36:55 +02:00
|
|
|
"strconv"
|
2018-07-03 08:26:28 +02:00
|
|
|
)
|
|
|
|
|
2018-07-10 14:02:23 +02:00
|
|
|
// GetListsByNamespaceID is the web handler to delete a namespace
|
2018-11-12 16:46:35 +01:00
|
|
|
// TODO: depricate this in favour of namespace.ReadOne() <-- should also return the lists
|
|
|
|
// @Summary Get all lists in a namespace
|
|
|
|
// @Description Returns all lists inside of a namespace.
|
|
|
|
// @tags namespace
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path int true "Namespace ID"
|
2019-01-03 23:22:06 +01:00
|
|
|
// @Security JWTKeyAuth
|
2018-11-12 16:46:35 +01:00
|
|
|
// @Success 200 {array} models.List "The lists."
|
|
|
|
// @Failure 403 {object} models.Message "No access to that namespace."
|
|
|
|
// @Failure 404 {object} models.Message "The namespace does not exist."
|
|
|
|
// @Failure 500 {object} models.Message "Internal error"
|
|
|
|
// @Router /namespaces/{id}/lists [get]
|
2018-07-03 08:26:28 +02:00
|
|
|
func GetListsByNamespaceID(c echo.Context) error {
|
2018-07-04 08:56:52 +02:00
|
|
|
// Get our namespace
|
|
|
|
namespace, err := getNamespace(c)
|
2018-07-03 08:26:28 +02:00
|
|
|
if err != nil {
|
2019-09-20 18:16:31 +02:00
|
|
|
return handler.HandleHTTPError(err, c)
|
2018-07-03 08:26:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the lists
|
2020-01-26 18:08:06 +01:00
|
|
|
doer, err := user.GetCurrentUser(c)
|
2018-12-04 11:16:42 +01:00
|
|
|
if err != nil {
|
2019-09-20 18:16:31 +02:00
|
|
|
return handler.HandleHTTPError(err, c)
|
2018-12-04 11:16:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
lists, err := models.GetListsByNamespaceID(namespace.ID, doer)
|
2018-07-03 08:26:28 +02:00
|
|
|
if err != nil {
|
2019-09-20 18:16:31 +02:00
|
|
|
return handler.HandleHTTPError(err, c)
|
2018-07-03 08:26:28 +02:00
|
|
|
}
|
|
|
|
return c.JSON(http.StatusOK, lists)
|
2018-07-03 08:48:28 +02:00
|
|
|
}
|
2018-07-12 23:36:55 +02:00
|
|
|
|
2019-09-20 18:16:31 +02:00
|
|
|
func getNamespace(c echo.Context) (namespace *models.Namespace, err error) {
|
2018-07-12 23:36:55 +02:00
|
|
|
// Check if we have our ID
|
2018-09-06 18:55:58 +02:00
|
|
|
id := c.Param("namespace")
|
2018-07-12 23:36:55 +02:00
|
|
|
// Make int
|
|
|
|
namespaceID, err := strconv.ParseInt(id, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-12-04 11:16:42 +01:00
|
|
|
if namespaceID == -1 {
|
2020-09-26 23:02:17 +02:00
|
|
|
namespace = &models.SharedListsPseudoNamespace
|
2018-12-04 11:16:42 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-12 23:36:55 +02:00
|
|
|
// Check if the user has acces to that namespace
|
2020-01-26 18:08:06 +01:00
|
|
|
user, err := user.GetCurrentUser(c)
|
2018-07-12 23:36:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-10-16 22:52:29 +02:00
|
|
|
namespace = &models.Namespace{ID: namespaceID}
|
2020-08-10 14:11:43 +02:00
|
|
|
canRead, _, err := namespace.CanRead(user)
|
2019-03-24 13:35:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return namespace, err
|
|
|
|
}
|
|
|
|
if !canRead {
|
2019-09-20 18:16:31 +02:00
|
|
|
return nil, echo.ErrForbidden
|
2018-07-12 23:36:55 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|