From 2383e28cd8360e7159334b41c2fdec26a5ce9e02 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Mon, 19 Oct 2020 16:49:40 +0200 Subject: [PATCH 1/3] =?UTF-8?q?Kompatibilit=C3=A4t=20mit=20backend=20(addf?= =?UTF-8?q?b7c7c463de5f44733896b06cb5fb480bedf8)?= 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}`; } }; From e6da94ad0e09b87d3e3fb140a1d04f384c9aea2f Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Tue, 20 Oct 2020 19:24:37 +0200 Subject: [PATCH 2/3] Fixed displayname --- src/plugins/user/store/user.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/plugins/user/store/user.ts b/src/plugins/user/store/user.ts index 12d698b..5878e90 100644 --- a/src/plugins/user/store/user.ts +++ b/src/plugins/user/store/user.ts @@ -124,6 +124,9 @@ const getters: GetterTree = { user ({ user }) { return user; }, + displayName ({ user }) { + return user.display_name; + }, session ({ session }) { return session; }, From 05fd255a51edb330d2e21e4ddd5a43599587f842 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 21 Oct 2020 17:39:04 +0200 Subject: [PATCH 3/3] Added backend interfaces --- src/flaschengeist.d.ts | 69 ++++++++++++++++++++++++---------- src/plugins/user/store/user.ts | 8 ++-- 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/src/flaschengeist.d.ts b/src/flaschengeist.d.ts index bec77c0..d3ff9d4 100644 --- a/src/flaschengeist.d.ts +++ b/src/flaschengeist.d.ts @@ -1,20 +1,51 @@ -interface Session { - expires: Date; - token: string; - lifetime: number; - browser: string; - platform: string; -} -interface User { - userid: string; - display_name: string; - firstname: string; - lastname: string; - mail: string; - roles: Array; -} -type Permission = string; -interface Role { - name: string; - permissions: Array; +declare namespace FG { + interface Event { + id: number; + begin: Date; + end?: Date; + description: string; + kind: EventKind; + slots: Array; + } + interface EventKind { + id: number; + name: string; + } + interface EventSlot { + id: number; + start: Date; + end?: any; + slots: Array; + } + interface Job { + userid: string; + value: number; + } + type JobKind = string; + interface JobSlot { + id: number; + needed_persons: number; + kind: JobKind; + jobs: Array; + } + interface User { + userid: string; + display_name: string; + firstname: string; + lastname: string; + mail: string; + roles: Array; + } + interface Session { + expires: Date; + token: string; + lifetime: number; + browser: string; + platform: string; + } + type Permission = string; + interface Role { + name: string; + permissions: Array; + } } diff --git a/src/plugins/user/store/user.ts b/src/plugins/user/store/user.ts index 5878e90..c6f6b43 100644 --- a/src/plugins/user/store/user.ts +++ b/src/plugins/user/store/user.ts @@ -11,8 +11,8 @@ export interface UserStateInterface extends LoginResponse { } export interface LoginResponse { - user: User; - session: Session; + user: FG.User; + session: FG.Session; } const state: UserStateInterface = { @@ -35,10 +35,10 @@ const state: UserStateInterface = { }; const mutations: MutationTree = { - setUser (state, data: User) { + setUser (state, data: FG.User) { state.user = data; }, - setSession (state, data: Session) { + setSession (state, data: FG.Session) { state.session = data; }, setLoginLoading (state, data: boolean) {