From 2383e28cd8360e7159334b41c2fdec26a5ce9e02 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 19 Oct 2020 16:49:40 +0200 Subject: [PATCH] =?UTF-8?q?Kompatibilit=C3=A4t=20mit=20backend=20(addfb7c7?= =?UTF-8?q?c463de5f44733896b06cb5fb480bedf8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/boot/login.ts | 20 ++++---- src/flaschengeist.d.ts | 17 ++----- src/plugins/user/pages/User.vue | 16 +++--- src/plugins/user/store/user.ts | 86 ++++++++++++++++++--------------- 4 files changed, 70 insertions(+), 69 deletions(-) diff --git a/src/boot/login.ts b/src/boot/login.ts index 94c8b49..8ca4d1c 100644 --- a/src/boot/login.ts +++ b/src/boot/login.ts @@ -6,19 +6,21 @@ import { StateInterface } from 'src/store'; export default boot>(({ router, store }) => { router.beforeEach((to, from, next) => { store.dispatch('user/loadFromLocalStorage').then(() => { - const permissions = store.getters['user/permissions']; - console.log('login_boot', permissions); + 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 - ) + "permissions" in record.meta && + ( + record.meta.permissions.filter((value: string) => + permissions.includes(value) + ).length == 0 ) ); }) diff --git a/src/flaschengeist.d.ts b/src/flaschengeist.d.ts index a9b081c..bec77c0 100644 --- a/src/flaschengeist.d.ts +++ b/src/flaschengeist.d.ts @@ -1,5 +1,4 @@ interface Session { - user: User; expires: Date; token: string; lifetime: number; @@ -7,25 +6,15 @@ interface Session { platform: string; } interface User { - roles: Array; userid: string; display_name: string; firstname: string; lastname: string; mail: string; + roles: Array; } -interface Permission { - id: number; - name: string; -} +type Permission = string; interface Role { + name: string; permissions: Array; - id: number; - name: string; -} -interface UserAttribute { - id: number; - user: number; - name: string; - value: string; } diff --git a/src/plugins/user/pages/User.vue b/src/plugins/user/pages/User.vue index 55eb717..2232c4c 100644 --- a/src/plugins/user/pages/User.vue +++ b/src/plugins/user/pages/User.vue @@ -10,17 +10,17 @@ height="" > - Name: {{ userSession.user.firstname }} {{ userSession.user.lastname }}
- E-Mail: {{ userSession.user.mail }}
+ Name: {{ userObj.firstname }} {{ userObj.lastname }}
+ E-Mail: {{ userObj.mail }}
Roles:
  • {{ role }}

- Token expires: {{ userSession.expires }} + Token expires: {{ sessionObj.expires }}
@@ -36,16 +36,16 @@ import { mainLink } from '../plugin'; export default defineComponent({ // name: 'PageName' setup(_, { root }) { - const userPermissions = computed( - () => root.$store.getters['user/permissions'] + const userObj = computed( + () => root.$store.getters['user/user'] ); - const userSession = computed( + const sessionObj = computed( () => root.$store.getters['user/session'] ); const checkMain = computed(() => { return mainLink.name == root.$route.name; }); - return { userPermissions, userSession, checkMain }; + return { userObj, sessionObj, checkMain }; } }); diff --git a/src/plugins/user/store/user.ts b/src/plugins/user/store/user.ts index 40dd2e0..12d698b 100644 --- a/src/plugins/user/store/user.ts +++ b/src/plugins/user/store/user.ts @@ -11,57 +11,56 @@ export interface UserStateInterface extends LoginResponse { } export interface LoginResponse { - permissions: string[]; + user: User; session: Session; } const state: UserStateInterface = { - permissions: [], + user: { + display_name: '', + firstname: '', + lastname: '', + mail: '', + roles: [], + userid: '' + }, session: { browser: '', expires: new Date(), lifetime: -1, platform: '', - token: '', - user: { - display_name: '', - firstname: '', - lastname: '', - mail: '', - roles: [], - userid: '' - } + token: '' }, loginLoading: false }; const mutations: MutationTree = { - setPermissions(state, data: []) { - state.permissions = data; + setUser (state, data: User) { + state.user = data; }, - setSession(state, data: Session) { + setSession (state, data: Session) { state.session = data; }, - setLoginLoading(state, data: boolean) { + setLoginLoading (state, data: boolean) { state.loginLoading = data; }, - showState(state) { + showState (state) { console.log(state); } }; const actions: ActionTree = { - login({ commit }, data: LoginData) { + login ({ commit }, data: LoginData) { commit('setLoginLoading', true); Loading.show({ message: 'Du wirst eingeloggt' }); void axios .post('/auth', data) .then((response: AxiosResponse) => { - commit('setPermissions', response.data.permissions); + commit('setUser', response.data.user); console.log('saved permisisons'); commit('setSession', response.data.session); commit('showState'); - LocalStorage.set('permissions', response.data.permissions); + LocalStorage.set('user', response.data.user); LocalStorage.set('session', response.data.session); void Router.push({ name: 'user' }); @@ -74,10 +73,17 @@ const actions: ActionTree = { Loading.hide(); }); }, - logout({ commit }, token) { + logout ({ commit }, token) { void axios.delete(`/auth/${token}`).then(() => { - commit('setPermissions', []); - commit('setToken', { + commit('setUser', { + display_name: '', + firstname: '', + lastname: '', + mail: '', + roles: [], + userid: '' + }); + commit('setSession', { browser: '', expires: '', lifetime: '', @@ -86,39 +92,43 @@ const actions: ActionTree = { }); }); }, - loadFromLocalStorage({ commit }) { + loadFromLocalStorage ({ commit }) { console.log('load from store'); - let data = LocalStorage.getItem('permissions'); - commit('setPermissions', data ? data : []); + let data = LocalStorage.getItem('user'); + commit('setUser', data ? data : { + display_name: '', + firstname: '', + lastname: '', + mail: '', + roles: [], + userid: '' + }); data = LocalStorage.getItem('session'); commit( 'setSession', data ? data : { - browser: '', - expires: new Date(), - lifetime: -1, - platform: '', - token: '' - } + browser: '', + expires: new Date(), + lifetime: -1, + platform: '', + token: '' + } ); commit('showState'); } }; const getters: GetterTree = { - permissions({ permissions }) { - return permissions; + user ({ user }) { + return user; }, - session({ session }) { + session ({ session }) { return session; }, - loginLoading({ loginLoading }) { + loginLoading ({ loginLoading }) { return loginLoading; - }, - displayName({ session: { user } }) { - return `${user.firstname} ${user.lastname}`; } };