From 2d9757f677db536059038454db40be3653868406 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Tue, 20 Aug 2024 20:16:19 -0500 Subject: [PATCH] fix: add setting to auto-redirect anon users to login, from home page --- src/wuttaweb/templates/appinfo/configure.mako | 30 +++++++++++++++++++ src/wuttaweb/templates/base.mako | 2 +- src/wuttaweb/views/common.py | 5 ++++ src/wuttaweb/views/settings.py | 4 +++ tests/views/test_common.py | 11 +++++++ 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/src/wuttaweb/templates/appinfo/configure.mako b/src/wuttaweb/templates/appinfo/configure.mako index da7d94d..d8c3af9 100644 --- a/src/wuttaweb/templates/appinfo/configure.mako +++ b/src/wuttaweb/templates/appinfo/configure.mako @@ -24,6 +24,36 @@ +

User/Auth

+
+ + + + Home Page auto-redirect to Login + + + + + + + +
+

Web Libraries

diff --git a/src/wuttaweb/templates/base.mako b/src/wuttaweb/templates/base.mako index f58f7ec..bae0544 100644 --- a/src/wuttaweb/templates/base.mako +++ b/src/wuttaweb/templates/base.mako @@ -154,7 +154,7 @@ .wutta-form-wrapper { margin-left: 5rem; margin-top: 2rem; - width: 50%; + width: 75%; } diff --git a/src/wuttaweb/views/common.py b/src/wuttaweb/views/common.py index 233ef20..a13fc50 100644 --- a/src/wuttaweb/views/common.py +++ b/src/wuttaweb/views/common.py @@ -53,6 +53,11 @@ class CommonView(View): if not user: return self.redirect(self.request.route_url('setup')) + # maybe auto-redirect anons to login + if not self.request.user: + if self.config.get_bool('wuttaweb.home_redirect_to_login'): + return self.redirect(self.request.route_url('login')) + return { 'index_title': self.app.get_title(), } diff --git a/src/wuttaweb/views/settings.py b/src/wuttaweb/views/settings.py index 1f2447c..087a7df 100644 --- a/src/wuttaweb/views/settings.py +++ b/src/wuttaweb/views/settings.py @@ -63,6 +63,10 @@ class AppInfoView(MasterView): {'name': f'{self.app.appname}.production', 'type': bool}, + # user/auth + {'name': 'wuttaweb.home_redirect_to_login', + 'type': bool, 'default': False}, + # web libs {'name': 'wuttaweb.libver.vue'}, {'name': 'wuttaweb.liburl.vue'}, diff --git a/tests/views/test_common.py b/tests/views/test_common.py index be227e3..bf240b5 100644 --- a/tests/views/test_common.py +++ b/tests/views/test_common.py @@ -24,6 +24,7 @@ class TestCommonView(WebTestCase): def test_home(self): self.pyramid_config.add_route('setup', '/setup') + self.pyramid_config.add_route('login', '/login') model = self.app.model view = self.make_view() @@ -40,6 +41,16 @@ class TestCommonView(WebTestCase): context = view.home(session=self.session) self.assertEqual(context['index_title'], self.app.get_title()) + # but if configured, anons will be redirected to login + self.config.setdefault('wuttaweb.home_redirect_to_login', 'true') + response = view.home(session=self.session) + self.assertEqual(response.status_code, 302) + + # now only an auth'ed user can see home page + self.request.user = user + context = view.home(session=self.session) + self.assertEqual(context['index_title'], self.app.get_title()) + def test_setup(self): self.pyramid_config.add_route('home', '/') self.pyramid_config.add_route('login', '/login')