From d82a994eebc93d72c2aa831db6254b50bf0a5a78 Mon Sep 17 00:00:00 2001 From: Moriz Wahl Date: Mon, 24 Jul 2023 14:58:43 +0200 Subject: [PATCH] remove isAdmin from guards, simplify logic --- admin/src/graphql/searchUsers.js | 1 - admin/src/router/guards.js | 6 ++---- admin/src/router/guards.test.js | 11 ++++------- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/admin/src/graphql/searchUsers.js b/admin/src/graphql/searchUsers.js index 315b2e1aa..9f82452c3 100644 --- a/admin/src/graphql/searchUsers.js +++ b/admin/src/graphql/searchUsers.js @@ -26,7 +26,6 @@ export const searchUsers = gql` hasElopage emailConfirmationSend deletedAt - # isAdmin roles } } diff --git a/admin/src/router/guards.js b/admin/src/router/guards.js index 3780c6ee1..79e381478 100644 --- a/admin/src/router/guards.js +++ b/admin/src/router/guards.js @@ -13,10 +13,8 @@ const addNavigationGuards = (router, store, apollo, i18n) => { }) .then((result) => { const moderator = result.data.verifyLogin - if (moderator.roles.includes('ADMIN', 0) || moderator.roles.includes('MODERATOR', 0)) { + if (moderator.roles?.length) { i18n.locale = moderator.language - moderator.isAdmin = moderator.roles.includes('ADMIN', 0) - moderator.isModerator = moderator.roles.includes('MODERATOR', 0) store.commit('moderator', moderator) next({ path: '/' }) } else { @@ -37,7 +35,7 @@ const addNavigationGuards = (router, store, apollo, i18n) => { !CONFIG.DEBUG_DISABLE_AUTH && // we did not disabled the auth module for debug purposes (!store.state.token || // we do not have a token !store.state.moderator || // no moderator set in store - !(store.state.moderator.isAdmin || store.state.moderator.isModerator)) && // user is no admin + !store.state.moderator.roles.length) && // user is no admin to.path !== '/not-found' && // we are not on `not-found` to.path !== '/logout' // we are not on `logout` ) { diff --git a/admin/src/router/guards.test.js b/admin/src/router/guards.test.js index 19a1881ef..6d51fda6d 100644 --- a/admin/src/router/guards.test.js +++ b/admin/src/router/guards.test.js @@ -54,8 +54,6 @@ describe('navigation guards', () => { it('commits the moderator to the store', () => { expect(storeCommitMock).toBeCalledWith('moderator', { roles: ['ADMIN'], - isAdmin: true, - isModerator: false, language: 'de', }) }) @@ -90,8 +88,6 @@ describe('navigation guards', () => { it('commits the moderator to the store', () => { expect(storeCommitMock).toBeCalledWith('moderator', { roles: ['MODERATOR'], - isAdmin: false, - isModerator: true, language: 'de', }) }) @@ -103,6 +99,7 @@ describe('navigation guards', () => { describe('with valid token and no roles', () => { beforeEach(() => { + jest.clearAllMocks() apolloQueryMock.mockResolvedValue({ data: { verifyLogin: { @@ -119,7 +116,7 @@ describe('navigation guards', () => { }) it('does not commit the moderator to the store', () => { - expect(storeCommitMock).not.toBeCalledWith('moderator', { isAdmin: false }) + expect(storeCommitMock).not.toBeCalledWith('moderator') }) it('redirects to /not-found', async () => { @@ -178,14 +175,14 @@ describe('navigation guards', () => { it('does not redirect with token in store and as admin', () => { store.state.token = 'valid token' - store.state.moderator = { isAdmin: true } + store.state.moderator = { roles: ['ADMIN'] } navGuard({ path: '/' }, {}, next) expect(next).toBeCalledWith() }) it('does not redirect with token in store and as moderator', () => { store.state.token = 'valid token' - store.state.moderator = { isModerator: true } + store.state.moderator = { roles: ['MODERATOR'] } navGuard({ path: '/' }, {}, next) expect(next).toBeCalledWith() })