remove isAdmin from guards, simplify logic

This commit is contained in:
Moriz Wahl 2023-07-24 14:58:43 +02:00
parent fdf40a6ec7
commit d82a994eeb
3 changed files with 6 additions and 12 deletions

View File

@ -26,7 +26,6 @@ export const searchUsers = gql`
hasElopage
emailConfirmationSend
deletedAt
# isAdmin
roles
}
}

View File

@ -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`
) {

View File

@ -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()
})