changeset 5376:e09e003948c7 extented-report

Decouple and enforce roles in creating scheduled imports.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 24 Jun 2021 19:24:21 +0200
parents 755ed195fdc3
children d19fdf3d2099
files pkg/controllers/importconfig.go pkg/imports/report.go
diffstat 2 files changed, 22 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/importconfig.go	Thu Jun 24 18:39:03 2021 +0200
+++ b/pkg/controllers/importconfig.go	Thu Jun 24 19:24:21 2021 +0200
@@ -30,6 +30,11 @@
 	mw "gemma.intevation.de/gemma/pkg/middleware"
 )
 
+// RolesRequierer enforces roles when storing schedules.
+type RolesRequierer interface {
+	RequiresRoles() auth.Roles
+}
+
 func runImportConfig(req *http.Request) (jr mw.JSONResult, err error) {
 
 	id, _ := strconv.ParseInt(mux.Vars(req)["id"], 10, 64)
@@ -253,18 +258,6 @@
 
 	kind := imports.JobKind(cfg.Kind)
 
-	session, _ := auth.GetSession(req)
-
-	// TODO: Find a more general way to prevent this.
-	if kind == imports.ReportJobKind && !session.Roles.Has("sys_admin") {
-		err = mw.JSONError{
-			Code: http.StatusUnauthorized,
-			Message: fmt.Sprintf(
-				"Not allowed to add config for kind %s", string(cfg.Kind)),
-		}
-		return
-	}
-
 	ctor := imports.ImportModelForJobKind(kind)
 	if ctor == nil {
 		err = mw.JSONError{
@@ -274,6 +267,19 @@
 		return
 	}
 	config := ctor()
+
+	session, _ := auth.GetSession(req)
+
+	if r, ok := config.(RolesRequierer); ok {
+		if roles := r.RequiresRoles(); len(roles) > 0 && !session.Roles.HasAny(roles...) {
+			err = mw.JSONError{
+				Code: http.StatusUnauthorized,
+				Message: fmt.Sprintf(
+					"Not allowed to add config for kind %s", string(cfg.Kind)),
+			}
+			return
+		}
+	}
 	if err = json.Unmarshal(cfg.Config, config); err != nil {
 		return
 	}
--- a/pkg/imports/report.go	Thu Jun 24 18:39:03 2021 +0200
+++ b/pkg/imports/report.go	Thu Jun 24 19:24:21 2021 +0200
@@ -27,6 +27,7 @@
 	"text/template"
 	"time"
 
+	"gemma.intevation.de/gemma/pkg/auth"
 	"gemma.intevation.de/gemma/pkg/common"
 	"gemma.intevation.de/gemma/pkg/config"
 	"gemma.intevation.de/gemma/pkg/misc"
@@ -84,6 +85,9 @@
 	return nil
 }
 
+// RequiresRoles enforces to be a sys_admin to run this .
+func (*Report) RequiresRoles() auth.Roles { return auth.Roles{"sys_admin"} }
+
 func (r *Report) Description() (string, error) { return r.Name, nil }
 
 func (*Report) CleanUp() error { return nil }