From 89754106e2376d1624bf2f64cc0c1c4530158e1f Mon Sep 17 00:00:00 2001 From: Nick Satterly Date: Fri, 23 Apr 2021 08:11:37 +0200 Subject: [PATCH] feat: allow readonly view without logging in (#468) --- src/App.vue | 15 +++++++++------ src/directives/hasPerms.ts | 7 ++++++- src/router.ts | 3 ++- src/store/modules/config.store.ts | 2 ++ 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/App.vue b/src/App.vue index 17e1c73d..c84eefca 100644 --- a/src/App.vue +++ b/src/App.vue @@ -5,7 +5,7 @@ >
@@ -386,7 +386,7 @@ @@ -589,7 +589,7 @@ export default { text: i18n.t('APIKeys'), path: '/keys', perms: 'read:keys', - show: true + show: this.isLoggedIn || !this.isAuthRequired }, { icon: 'assessment', @@ -649,6 +649,9 @@ export default { isAuthRequired() { return this.$config.auth_required }, + isAllowReadonly() { + return this.$config.allow_readonly + }, isSignupEnabled() { return this.$config.signup_enabled }, diff --git a/src/directives/hasPerms.ts b/src/directives/hasPerms.ts index a4910c4b..8213623a 100644 --- a/src/directives/hasPerms.ts +++ b/src/directives/hasPerms.ts @@ -7,11 +7,16 @@ import { store } from '@/main' export default Vue.directive('has-perms', function(el, binding) { let authRequired = store.getters.getConfig('auth_required') + let allowReadonly = store.getters.getConfig('allow_readonly') + let readonlyScopes = store.getters.getConfig('readonly_scopes') let authenticated = store.state.auth.isAuthenticated if (!authRequired) { return true } + if (allowReadonly) { + authenticated = true + } if (!authenticated) { return false } @@ -29,7 +34,7 @@ export default Vue.directive('has-perms', function(el, binding) { } let perm = binding.value - let scopes = store.getters['auth/scopes'] + let scopes = allowReadonly ? readonlyScopes : store.getters['auth/scopes'] let action = binding.modifiers.disable ? 'disable' : 'hide' if (!perm) { diff --git a/src/router.ts b/src/router.ts index 99c8237f..5052e05c 100644 --- a/src/router.ts +++ b/src/router.ts @@ -172,7 +172,8 @@ export function createRouter(basePath): VueRouter { router.beforeEach((to, from, next) => { if ((store.getters.getConfig('auth_required') && to.matched.some(record => record.meta.requiresAuth))) { - if (!store.getters['auth/isLoggedIn']) { + if (!store.getters['auth/isLoggedIn'] && + !store.getters.getConfig('allow_readonly')) { next({ path: '/login', query: { redirect: to.fullPath } diff --git a/src/store/modules/config.store.ts b/src/store/modules/config.store.ts index 48388c30..e5f905ad 100644 --- a/src/store/modules/config.store.ts +++ b/src/store/modules/config.store.ts @@ -5,6 +5,8 @@ const state = { alarm_model: {}, // includes severity, colors and status maps auth_required: true, + allow_readonly: false, + readonly_scopes: ['read'], provider: 'basic', customer_views: false, signup_enabled: true,