login.ts (check route-permission) geupdatet

This commit is contained in:
Tim Gröger 2020-10-27 11:51:53 +01:00
parent d097231dc1
commit c6ef18b009
4 changed files with 97 additions and 43 deletions

View File

@ -1,39 +1,70 @@
import { boot } from 'quasar/wrappers'; import { boot } from 'quasar/wrappers';
import { RouteRecord } from 'vue-router'; import { RouteRecord } from 'vue-router';
import { Store } from 'vuex' import { Store } from 'vuex';
import { StateInterface } from 'src/store'; import { StateInterface } from 'src/store';
import { UserStateInterface } from 'src/plugins/user/store/user';
export default boot<Store<StateInterface>>(({ router, store }) => { export default boot<Store<StateInterface>>(({ router, store }) => {
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
store.dispatch('user/loadFromLocalStorage').then(() => { store
const user: User = store.getters['user/user']; .dispatch('user/loadFromLocalStorage')
.then(() => {
const user: FG.User = <FG.User>store.getters['user/user'];
const session: FG.Session = <FG.Session>store.getters['user/session'];
let permissions: string[] = []; let permissions: string[] = [];
user.roles.forEach(role => { user.roles.forEach(role => {
permissions = permissions.concat(role.permissions) permissions = permissions.concat(role.permissions);
}); });
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 ( if (
to.matched.some((record: RouteRecord) => { new Date(session.expires) >= new Date() &&
// permissions is set AND has NO matching permission to.matched.every((record: RouteRecord) => {
return ( const checkedPerimssions:
"permissions" in record.meta && | boolean
( | undefined = record.meta?.permissions?.every(
record.meta.permissions.filter((value: string) => (permission: FG.Permission) => {
permissions.includes(value) return permissions.includes(permission);
).length == 0 }
)
); );
return checkedPerimssions === undefined
? true
: checkedPerimssions;
}) })
) { ) {
next({ next();
path: '/login', } else {
query: { redirect: to.fullPath } next({ name: 'login', query: { redirect: to.fullPath } });
}); }
} else { } else {
next(); next();
} }
}).catch(error => {
// 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); console.exception(error);
}); });
}) });
}); });

View File

@ -38,11 +38,14 @@
<q-chip <q-chip
v-for="(plugin, index) in $flaschengeistPlugins.plugins" v-for="(plugin, index) in $flaschengeistPlugins.plugins"
:key="'plugin' + index" :key="'plugin' + index"
square
class="q-my-none q-ml-xs q-mr-none q-pr-none"
> >
{{ plugin.name }} {{ plugin.name }}
<q-separator vertical color="black" class="q-ma-xs" /> <q-chip dense square color="green" class="q-pa-sm, q-mr-none">
{{ plugin.version }} {{ plugin.version }}
</q-chip> </q-chip>
</q-chip>
</q-card-section> </q-card-section>
<q-separator /> <q-separator />
<q-card-section class="text-h6"> <q-card-section class="text-h6">

View File

@ -39,11 +39,32 @@
> >
<template v-slot:selected-item="scope"> <template v-slot:selected-item="scope">
<q-chip v-for="(item, index) in scope.opt" :key="'item' + index"> <q-chip v-for="(item, index) in scope.opt" :key="'item' + index">
{{ item }} {{ item.name }}
</q-chip> </q-chip>
</template> </template>
</q-select> </q-select>
</q-card-section> </q-card-section>
<q-separator />
<q-card-section class="fit row justify-start content-center items-center">
<q-input
class="col-xs-12 col-sm-6 col-md-4 q-pa-sm"
label="Password"
type="password"
filled
/>
<q-input
class="col-xs-12 col-sm-6 col-md-4 q-pa-sm"
label="Neues Password"
type="password"
filled
/>
<q-input
class="col-xs-12 col-sm-6 col-md-4 q-pa-sm"
label="Wiederhole neues Password"
type="password"
filled
/>
</q-card-section>
</q-card> </q-card>
</template> </template>
@ -53,7 +74,7 @@ export default defineComponent({
name: 'Main', name: 'Main',
setup(_, { root: { $store } }) { setup(_, { root: { $store } }) {
const user = computed(() => { const user = computed(() => {
return $store.getters['user/session'].user; return $store.getters['user/user'];
}); });
return { user }; return { user };
} }

View File

@ -2,9 +2,8 @@ import { Module, MutationTree, ActionTree, GetterTree } from 'vuex';
import { StateInterface } from 'src/store'; import { StateInterface } from 'src/store';
import { axios } from 'src/boot/axios'; import { axios } from 'src/boot/axios';
export interface SessionInterface { export interface SessionInterface {
sessions: Session[]; sessions: FG.Session[];
loading: boolean; loading: boolean;
} }
@ -14,7 +13,7 @@ const state: SessionInterface = {
}; };
const mutations: MutationTree<SessionInterface> = { const mutations: MutationTree<SessionInterface> = {
setSessions (state, sessions: Session[]) { setSessions(state, sessions: FG.Session[]) {
state.sessions = sessions; state.sessions = sessions;
}, },
setLoading(state, value: boolean) { setLoading(state, value: boolean) {