Skip to content

Commit

Permalink
Hide admin page link for non-admin users
Browse files Browse the repository at this point in the history
  • Loading branch information
simenheg committed Dec 4, 2023
1 parent a1d7831 commit 39520a8
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ All notable changes to this project will be documented in this file. The format
based on the admin level of the current user.
- Organization admins should now be allowed to edit own organizations including
child departments and products.
- The link to the admin page is no longer visible to non-admin users.

### Security

Expand Down
9 changes: 6 additions & 3 deletions src/components/Navigation/UserMenuDropdown.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<div class="user-menu-dropdown__footer">
<template v-for="link in links">
<pkt-button
v-if="link.show !== undefined ? link.show : true"
v-if="link.show"
:key="`link_${link.key}`"
size="small"
skin="tertiary"
Expand All @@ -66,8 +66,9 @@
<script>
import { mapActions, mapState } from 'vuex';
import { db, auth } from '@/config/firebaseConfig';
import User from '@/db/User';
import { PktButton } from '@oslokommune/punkt-vue2';
import isAdmin from '@/util/user';
import User from '@/db/User';
import UserProfileForm from '@/components/forms/UserProfileForm.vue';
export default {
Expand Down Expand Up @@ -117,19 +118,21 @@ export default {
text: this.$t('general.admin'),
icon: 'cogwheel',
route: { name: 'Admin' },
show: !!this.user?.admin,
show: isAdmin(this.user),
},
{
key: 'api',
text: this.$t('general.api'),
icon: 'document-code',
route: { name: 'Api' },
show: true,
},
{
key: 'help',
text: this.$t('general.help'),
icon: 'question',
route: { name: 'Help' },
show: true,
},
];
},
Expand Down
6 changes: 2 additions & 4 deletions src/router/router-guards/routerGuardUtil.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { db } from '@/config/firebaseConfig';
import { firestoreEncode } from '@/util/firebaseUtil';
import isAdmin from '@/util/user';
import store from '@/store';

const getSlugRef = async (slug) => {
Expand All @@ -15,10 +16,7 @@ const getSlugRef = async (slug) => {
.then((snap) => snap.data())
.catch(() => null);

return !refData ||
(refData.archived && (!store.state.user.admin || !store.state.user.superAdmin))
? null
: reference;
return !refData || (refData.archived && !isAdmin(store.state.user)) ? null : reference;
};

export default getSlugRef;
16 changes: 0 additions & 16 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,6 @@ export const storeGetters = {
});
},

isAdmin: (state) => {
// Returns `true` if user has `admin: true` or if user is member of `activeItem`
const { user, activeItem } = state;

if (user && user.superAdmin) {
return true;
}
if (user && user.admin && user.admin.length > 0) {
return true;
}
if (!user || !activeItem || !activeItem.team) {
return false;
}
return activeItem.team.map(({ id }) => id).includes(user.id);
},

/**
* Returns `true` if the current user is an admin of the parent organization
* of `activeItem`.
Expand Down
10 changes: 10 additions & 0 deletions src/util/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Return true if `user` is an admin, otherwise return false.
*
* "Admin" meaning either a super admin, or admin of any organization.
*/
function isAdmin(user) {
return Boolean(user && (user.superAdmin || user.admin?.length > 0));
}

export default isAdmin;
6 changes: 3 additions & 3 deletions src/views/Admin/AdminWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
</template>

<script>
import { mapState, mapGetters } from 'vuex';
import { mapState } from 'vuex';
import isAdmin from '@/util/user';
export default {
name: 'AdminWrapper',
computed: {
...mapState(['user']),
...mapGetters(['isAdmin']),
},
created() {
if (!this.isAdmin && !this.user.superAdmin) {
if (!isAdmin(this.user)) {
this.$router.push({ name: 'Home' });
}
},
Expand Down
7 changes: 2 additions & 5 deletions src/views/Admin/components/AdminUsers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import { mapState } from 'vuex';
import Fuse from 'fuse.js';
import { PktButton } from '@oslokommune/punkt-vue2';
import isAdmin from '@/util/user';
import AddUsers from './AddUsers.vue';
import EditUser from './EditUser.vue';
Expand Down Expand Up @@ -121,11 +122,7 @@ export default {
},
},
methods: {
isAdmin(user) {
return user.admin?.length > 0 || user.superAdmin;
},
},
methods: { isAdmin },
};
</script>

Expand Down
2 changes: 1 addition & 1 deletion src/views/Item/ItemIntegrations.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default {
computed: {
...mapState(['activeItem', 'activeItemRef']),
...mapGetters(['isAdmin', 'hasEditRights']),
...mapGetters(['hasEditRights']),
},
watch: {
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/util/user.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import isAdmin from '../../../src/util/user';

describe('Test admin check', () => {
test('unconfigured user is not an admin', () => {
expect(isAdmin({})).toBe(false);
});
test('super admin is an admin', () => {
expect(isAdmin({ superAdmin: true })).toBe(true);
});
test('non-super admin is not an admin', () => {
expect(isAdmin({ superAdmin: false })).toBe(false);
});
test('falsy admin is not an admin', () => {
expect(isAdmin({ admin: false })).toBe(false);
});
test('empty admin is not an admin', () => {
expect(isAdmin({ admin: [] })).toBe(false);
});
test('admin of something is an admin', () => {
expect(isAdmin({ admin: ['foo-corp'] })).toBe(true);
});
test('admin of several things is an admin', () => {
expect(isAdmin({ admin: ['foo-corp', 'bar-corp'] })).toBe(true);
});
});

0 comments on commit 39520a8

Please sign in to comment.