From c6ef18b0098a7124a8422953776c4c4e4bf6a142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20Gr=C3=B6ger?= Date: Tue, 27 Oct 2020 11:51:53 +0100 Subject: [PATCH] login.ts (check route-permission) geupdatet --- src/boot/login.ts | 93 ++++++++++++------- src/pages/about/About.vue | 7 +- src/plugins/user/components/settings/Main.vue | 25 ++++- src/plugins/user/store/session.ts | 15 ++- 4 files changed, 97 insertions(+), 43 deletions(-) diff --git a/src/boot/login.ts b/src/boot/login.ts index 8ca4d1c..d369769 100644 --- a/src/boot/login.ts +++ b/src/boot/login.ts @@ -1,39 +1,70 @@ import { boot } from 'quasar/wrappers'; import { RouteRecord } from 'vue-router'; -import { Store } from 'vuex' +import { Store } from 'vuex'; import { StateInterface } from 'src/store'; +import { UserStateInterface } from 'src/plugins/user/store/user'; export default boot>(({ router, store }) => { router.beforeEach((to, from, next) => { - store.dispatch('user/loadFromLocalStorage').then(() => { - const user: User = store.getters['user/user']; - let permissions: string[] = []; - user.roles.forEach(role => { - permissions = permissions.concat(role.permissions) - }); - - if ( - to.matched.some((record: RouteRecord) => { - // permissions is set AND has NO matching permission - return ( - "permissions" in record.meta && - ( - record.meta.permissions.filter((value: string) => - permissions.includes(value) - ).length == 0 - ) - ); - }) - ) { - next({ - path: '/login', - query: { redirect: to.fullPath } + store + .dispatch('user/loadFromLocalStorage') + .then(() => { + const user: FG.User = store.getters['user/user']; + const session: FG.Session = store.getters['user/session']; + let permissions: string[] = []; + user.roles.forEach(role => { + permissions = permissions.concat(role.permissions); }); - } else { - next(); - } - }).catch(error => { - console.exception(error); - }); - }) + console.log('route to', to); + console.log('route from', from); + if (to.name != 'login') { + console.log(new Date(session.expires), new Date()); + console.log(new Date(session.expires) >= new Date()); + if ( + new Date(session.expires) >= new Date() && + to.matched.every((record: RouteRecord) => { + const checkedPerimssions: + | boolean + | undefined = record.meta?.permissions?.every( + (permission: FG.Permission) => { + return permissions.includes(permission); + } + ); + + return checkedPerimssions === undefined + ? true + : checkedPerimssions; + }) + ) { + next(); + } else { + next({ name: 'login', query: { redirect: to.fullPath } }); + } + } else { + next(); + } + + // if ( + // to.matched.some((record: RouteRecord) => { + // // permissions is set AND has NO matching permission + // return ( + // 'permissions' in record.meta && + // record.meta.permissions.filter((value: string) => + // permissions.includes(value) + // ).length == 0 + // ); + // }) + // ) { + // next({ + // path: '/login', + // query: { redirect: to.fullPath } + // }); + // } else { + // next(); + // } + }) + .catch(error => { + console.exception(error); + }); + }); }); diff --git a/src/pages/about/About.vue b/src/pages/about/About.vue index 24cf4b7..e98b6a2 100644 --- a/src/pages/about/About.vue +++ b/src/pages/about/About.vue @@ -38,10 +38,13 @@ {{ plugin.name }} - - {{ plugin.version }} + + {{ plugin.version }} + diff --git a/src/plugins/user/components/settings/Main.vue b/src/plugins/user/components/settings/Main.vue index fc46ef9..bfd5521 100644 --- a/src/plugins/user/components/settings/Main.vue +++ b/src/plugins/user/components/settings/Main.vue @@ -39,11 +39,32 @@ > + + + + + + @@ -53,7 +74,7 @@ export default defineComponent({ name: 'Main', setup(_, { root: { $store } }) { const user = computed(() => { - return $store.getters['user/session'].user; + return $store.getters['user/user']; }); return { user }; } diff --git a/src/plugins/user/store/session.ts b/src/plugins/user/store/session.ts index 98a6489..2333548 100644 --- a/src/plugins/user/store/session.ts +++ b/src/plugins/user/store/session.ts @@ -2,9 +2,8 @@ import { Module, MutationTree, ActionTree, GetterTree } from 'vuex'; import { StateInterface } from 'src/store'; import { axios } from 'src/boot/axios'; - export interface SessionInterface { - sessions: Session[]; + sessions: FG.Session[]; loading: boolean; } @@ -14,16 +13,16 @@ const state: SessionInterface = { }; const mutations: MutationTree = { - setSessions (state, sessions: Session[]) { + setSessions(state, sessions: FG.Session[]) { state.sessions = sessions; }, - setLoading (state, value: boolean) { + setLoading(state, value: boolean) { state.loading = value; } }; const actions: ActionTree = { - getSessions ({ commit, rootGetters }) { + getSessions({ commit, rootGetters }) { console.log(rootGetters); commit('setLoading', true); axios @@ -39,7 +38,7 @@ const actions: ActionTree = { commit('setLoading', false); }); }, - deleteSession ({ commit, dispatch }, token: string) { + deleteSession({ commit, dispatch }, token: string) { commit('setLoading', true); console.log('axios', axios); axios @@ -57,10 +56,10 @@ const actions: ActionTree = { }; const getters: GetterTree = { - sessions (state) { + sessions(state) { return state.sessions; }, - loading (state) { + loading(state) { return state.loading; } };