1
0

Updated handler config (#63)

This commit is contained in:
konrad
2019-03-24 09:13:40 +00:00
committed by Gitea
parent 1dc14d5ddf
commit 11e7c071ce
118 changed files with 3675 additions and 1235 deletions

41
vendor/code.vikunja.io/web/handler/config.go generated vendored Normal file
View File

@ -0,0 +1,41 @@
// Copyright (c) 2019 Vikunja and contributors.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
package handler
import (
"code.vikunja.io/web"
"github.com/op/go-logging"
)
type Config struct {
AuthProvider *web.Auths
LoggingProvider *logging.Logger
}
var config *Config
func init() {
config = &Config{}
}
func SetAuthProvider(provider *web.Auths) {
config.AuthProvider = provider
}
func SetLoggingProvider(logger *logging.Logger) {
config.LoggingProvider = logger
}

View File

@ -16,7 +16,6 @@
package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"net/http"
)
@ -37,15 +36,14 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
}
// Get the user to pass for later checks
authprovider := ctx.Get("AuthProvider").(*web.Auths)
currentAuth, err := authprovider.AuthObject(ctx)
currentAuth, err := config.AuthProvider.AuthObject(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
// Check rights
if !currentStruct.CanCreate(currentAuth) {
getLogger(ctx).Noticef("Tried to create while not having the rights for it (User: %v)", currentAuth)
config.LoggingProvider.Noticef("Tried to create while not having the rights for it (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden)
}

View File

@ -16,7 +16,6 @@
package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"net/http"
)
@ -37,13 +36,12 @@ func (c *WebHandler) DeleteWeb(ctx echo.Context) error {
}
// Check if the user has the right to delete
authprovider := ctx.Get("AuthProvider").(*web.Auths)
currentAuth, err := authprovider.AuthObject(ctx)
currentAuth, err := config.AuthProvider.AuthObject(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError)
}
if !currentStruct.CanDelete(currentAuth) {
getLogger(ctx).Noticef("Tried to delete while not having the rights for it (User: %v)", currentAuth)
config.LoggingProvider.Noticef("Tried to create while not having the rights for it (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden)
}

View File

@ -18,7 +18,6 @@ package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"github.com/op/go-logging"
"net/http"
)
@ -40,11 +39,6 @@ func HandleHTTPError(err error, ctx echo.Context) *echo.HTTPError {
errDetails := a.HTTPError()
return echo.NewHTTPError(errDetails.HTTPCode, errDetails)
}
getLogger(ctx).Error(err.Error())
config.LoggingProvider.Error(err.Error())
return echo.NewHTTPError(http.StatusInternalServerError)
}
// Helper function to get the logger
func getLogger(ctx echo.Context) *logging.Logger {
return ctx.Get("LoggingProvider").(*logging.Logger)
}

View File

@ -16,7 +16,6 @@
package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"net/http"
"strconv"
@ -27,8 +26,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
// Get our model
currentStruct := c.EmptyStruct()
authprovider := ctx.Get("AuthProvider").(*web.Auths)
currentAuth, err := authprovider.AuthObject(ctx)
currentAuth, err := config.AuthProvider.AuthObject(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
@ -45,7 +43,7 @@ func (c *WebHandler) ReadAllWeb(ctx echo.Context) error {
}
pageNumber, err := strconv.Atoi(page)
if err != nil {
getLogger(ctx).Error(err.Error())
config.LoggingProvider.Error(err.Error())
return echo.NewHTTPError(http.StatusBadRequest, "Bad page requested.")
}
if pageNumber < 0 {

View File

@ -16,7 +16,6 @@
package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"net/http"
)
@ -39,13 +38,12 @@ func (c *WebHandler) ReadOneWeb(ctx echo.Context) error {
// Check rights
// We can only check the rights on a full object, which is why we need to check it afterwards
authprovider := ctx.Get("AuthProvider").(*web.Auths)
currentAuth, err := authprovider.AuthObject(ctx)
currentAuth, err := config.AuthProvider.AuthObject(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !currentStruct.CanRead(currentAuth) {
getLogger(ctx).Noticef("Tried to read one while not having the rights for it (User: %v)", currentAuth)
config.LoggingProvider.Noticef("Tried to create while not having the rights for it (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden, "You don't have the right to see this")
}

View File

@ -16,7 +16,6 @@
package handler
import (
"code.vikunja.io/web"
"github.com/labstack/echo"
"net/http"
)
@ -38,13 +37,12 @@ func (c *WebHandler) UpdateWeb(ctx echo.Context) error {
}
// Check if the user has the right to do that
authprovider := ctx.Get("AuthProvider").(*web.Auths)
currentAuth, err := authprovider.AuthObject(ctx)
currentAuth, err := config.AuthProvider.AuthObject(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Could not determine the current user.")
}
if !currentStruct.CanUpdate(currentAuth) {
getLogger(ctx).Noticef("Tried to update while not having the rights for it (User: %v)", currentAuth)
config.LoggingProvider.Noticef("Tried to create while not having the rights for it (User: %v)", currentAuth)
return echo.NewHTTPError(http.StatusForbidden)
}