Open platform to collect and prioritize feedback
add blank secured page
goenning08df3fed9093
diff --git a/app/feedback/handler_test.go b/app/feedback/handler_test.goindex 159741b2c..3d951b903 100644--- a/app/feedback/handler_test.go+++ b/app/feedback/handler_test.go@@ -97,7 +97,7 @@ func TestDetailsHandler_AddIdea(t *testing.T) {server := mock.NewServer()server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})- server.Context.Set("__CTX_CLAIMS", &app.WechyClaims{UserID: 1, UserName: "Jon"})+ server.Context.Set("__CTX_USER", &app.User{ID: 1, Name: "Jon"})handler := feedback.Handlers(NewMockIdeaService()).PostIdea()code, _ := server.ExecutePost(handler, `{ "title": "My newest idea :)" }`)@@ -109,7 +109,7 @@ func TestDetailsHandler_AddIdea_WithoutTitle(t *testing.T) {server := mock.NewServer()server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})- server.Context.Set("__CTX_CLAIMS", &app.WechyClaims{UserID: 1, UserName: "Jon"})+ server.Context.Set("__CTX_USER", &app.User{ID: 1, Name: "Jon"})handler := feedback.Handlers(NewMockIdeaService()).PostIdea()code, _ := server.ExecutePost(handler, `{ "title": "" }`)@@ -121,7 +121,7 @@ func TestDetailsHandler_AddComment(t *testing.T) {server := mock.NewServer()server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})- server.Context.Set("__CTX_CLAIMS", &app.WechyClaims{UserID: 1, UserName: "Jon"})+ server.Context.Set("__CTX_USER", &app.User{ID: 1, Name: "Jon"})server.Context.SetParamNames("id")server.Context.SetParamValues("1")handler := feedback.Handlers(NewMockIdeaService()).PostComment()@@ -135,7 +135,7 @@ func TestDetailsHandler_AddComment_WithoutContent(t *testing.T) {server := mock.NewServer()server.Context.Set("__CTX_TENANT", &app.Tenant{ID: 2, Name: "Any Tenant"})- server.Context.Set("__CTX_CLAIMS", &app.WechyClaims{UserID: 1, UserName: "Jon"})+ server.Context.Set("__CTX_USER", &app.User{ID: 1, Name: "Jon"})server.Context.SetParamNames("id")server.Context.SetParamValues("1")handler := feedback.Handlers(NewMockIdeaService()).PostComment()diff --git a/app/identity/middleware_test.go b/app/identity/middleware_test.goindex abcd35eac..8d9e330bb 100644--- a/app/identity/middleware_test.go+++ b/app/identity/middleware_test.go@@ -130,7 +130,7 @@ func TestJwtGetter_WithCookie(t *testing.T) {mw := identity.JwtGetter(userService)mw(func(c app.Context) error {- return c.String(http.StatusOK, c.Claims().UserName)+ return c.String(http.StatusOK, c.User().Name)})(c)Expect(rec.Code).To(Equal(http.StatusOK))@@ -158,7 +158,7 @@ func TestJwtGetter_WithCookie_DifferentTenant(t *testing.T) {mw := identity.JwtGetter(userService)mw(func(c app.Context) error {- if c.Claims() == nil {+ if c.User() == nil {return c.NoContent(http.StatusNoContent)}return c.NoContent(http.StatusOK)diff --git a/app/infra/middleware.go b/app/infra/middleware.goindex b64b00cf9..f67d8d206 100644--- a/app/infra/middleware.go+++ b/app/infra/middleware.go@@ -19,6 +19,21 @@ func IsAuthenticated() app.MiddlewareFunc {}}+// IsAuthorized blocks non-authorized requests+func IsAuthorized(roles ...app.Role) app.MiddlewareFunc {+ return func(next app.HandlerFunc) app.HandlerFunc {+ return func(c app.Context) error {+ user := c.User()+ for _, role := range roles {+ if user.Role == role {+ return next(c)+ }+ }+ return c.NoContent(http.StatusForbidden)+ }+ }+}+// HostChecker checks for a specific hostfunc HostChecker(baseURL string) app.MiddlewareFunc {return func(next app.HandlerFunc) app.HandlerFunc {diff --git a/app/infra/middleware_test.go b/app/infra/middleware_test.goindex 9f1be8895..81e90343c 100644--- a/app/infra/middleware_test.go+++ b/app/infra/middleware_test.go@@ -12,15 +12,55 @@ import (. "github.com/onsi/gomega")-func TestIsAuthenticated_WithClaims(t *testing.T) {+func TestIsAuthorized_WithAllowedRole(t *testing.T) {RegisterTestingT(t)server := mock.NewServer()req, _ := http.NewRequest(echo.GET, "/", nil)rec := httptest.NewRecorder()c := server.NewContext(req, rec)- c.SetClaims(&app.WechyClaims{- UserID: 1,+ c.SetUser(&app.User{+ ID: 1,+ Role: app.RoleMember,+ })++ mw := infra.IsAuthorized(app.RoleAdministrator, app.RoleMember)+ mw(func(c app.Context) error {+ return c.NoContent(http.StatusOK)+ })(c)++ Expect(rec.Code).To(Equal(http.StatusOK))+}++func TestIsAuthorized_WithForbiddenRole(t *testing.T) {+ RegisterTestingT(t)++ server := mock.NewServer()+ req, _ := http.NewRequest(echo.GET, "/", nil)+ rec := httptest.NewRecorder()+ c := server.NewContext(req, rec)+ c.SetUser(&app.User{+ ID: 1,+ Role: app.RoleVisitor,+ })++ mw := infra.IsAuthorized(app.RoleAdministrator, app.RoleMember)+ mw(func(c app.Context) error {+ return c.NoContent(http.StatusOK)+ })(c)++ Expect(rec.Code).To(Equal(http.StatusForbidden))+}++func TestIsAuthenticated_WithUser(t *testing.T) {+ RegisterTestingT(t)++ server := mock.NewServer()+ req, _ := http.NewRequest(echo.GET, "/", nil)+ rec := httptest.NewRecorder()+ c := server.NewContext(req, rec)+ c.SetUser(&app.User{+ ID: 1,})mw := infra.IsAuthenticated()@@ -31,7 +71,7 @@ func TestIsAuthenticated_WithClaims(t *testing.T) {Expect(rec.Code).To(Equal(http.StatusOK))}-func TestIsAuthenticated_WithoutClaims(t *testing.T) {+func TestIsAuthenticated_WithoutUser(t *testing.T) {RegisterTestingT(t)server := mock.NewServer()diff --git a/router.go b/router.goindex 4d3a20e16..86da33b71 100644--- a/router.go+++ b/router.go@@ -119,15 +119,28 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {get(appGroup, "/api/status", infra.Status(ctx.Health, ctx.Settings))}- securedGroup := group(router, "")+ securedGroup := group(router, "/api"){use(securedGroup, identity.MultiTenant(ctx.Tenant))use(securedGroup, identity.JwtGetter(ctx.User))use(securedGroup, identity.JwtSetter())use(securedGroup, infra.IsAuthenticated())- post(securedGroup, "/api/ideas", feedback.Handlers(ctx.Idea).PostIdea())- post(securedGroup, "/api/ideas/:id/comments", feedback.Handlers(ctx.Idea).PostComment())+ post(securedGroup, "/ideas", feedback.Handlers(ctx.Idea).PostIdea())+ post(securedGroup, "/ideas/:id/comments", feedback.Handlers(ctx.Idea).PostComment())+ }++ adminGroup := group(router, "/admin")+ {+ use(adminGroup, identity.MultiTenant(ctx.Tenant))+ use(adminGroup, identity.JwtGetter(ctx.User))+ use(adminGroup, identity.JwtSetter())+ use(adminGroup, infra.IsAuthenticated())+ use(adminGroup, infra.IsAuthorized(app.RoleMember, app.RoleAdministrator))++ get(adminGroup, "", func(ctx app.Context) error {+ return ctx.HTML(http.StatusOK, "Welcome to Admin Page :)")+ })}return router