enforce active user in router

This commit is contained in:
j3d1 2024-01-07 21:45:31 +01:00
parent 8c85aa4fe4
commit 2ba96c8b16

View file

@ -1,30 +1,105 @@
import Items from './views/Items'; import Items from './views/Items';
import Boxes from './views/Boxes'; import Boxes from './views/Boxes';
import Files from './views/Files'; import Files from './views/Files';
import Events from './views/Events';
import Error from './views/Error'; import Error from './views/Error';
import HowTo from './views/HowTo'; import HowTo from './views/HowTo';
import VueRouter from 'vue-router'; import VueRouter from 'vue-router';
import Vue from 'vue'; import Vue from 'vue';
import Login from '@/views/Login.vue';
import Register from '@/views/Register.vue';
import Debug from "@/views/admin/Debug.vue";
import Tickets from "@/views/Tickets.vue";
import Ticket from "@/views/Ticket.vue";
import Admin from "@/views/admin/Admin.vue";
import store from "@/store";
import Empty from "@/views/Empty.vue";
import Events from "@/views/admin/Events.vue";
import AccessControl from "@/views/admin/AccessControl.vue";
Vue.use(VueRouter); Vue.use(VueRouter);
const routes = [ const routes = [
{path: '/', redirect: '/Camp23/items'}, {path: '/', redirect: '/Camp23/items', meta: {requiresAuth: false}},
{path: '/howto', name: 'howto', component: HowTo}, {path: '/login/', name: 'login', component: Login, meta: {requiresAuth: false}},
{path: '/admin/files', name: 'files', component: Files}, {path: '/register/', name: 'register', component: Register, meta: {requiresAuth: false}},
{path: '/admin/events', name: 'events', component: Events}, {path: '/howto/', name: 'howto', component: HowTo, meta: {requiresAuth: true}},
{path: '/:event/boxes', name: 'boxes', component: Boxes}, {
{path: '/:event/items', name: 'items', component: Items}, path: '/:event/items/', name: 'items', component: Items, meta:
{path: '/:event/box/:uid', name: 'boxes', component: Boxes}, {requiresAuth: true, requiresPermission: 'view_item'}
{path: '/:event/item/:uid', name: 'items', component: Items}, },
{
path: '/:event/item/:uid/', name: 'item', component: Items, meta:
{requiresAuth: true, requiresPermission: 'view_item'}
},
{
path: '/:event/boxes/', name: 'boxes', component: Boxes, meta:
{requiresAuth: true, requiresPermission: 'view_container'}
},
{
path: '/:event/box/:uid/', name: 'box', component: Boxes, meta:
{requiresAuth: true, requiresPermission: 'view_container'}
},
{
path: '/:event/tickets/', name: 'tickets', component: Tickets, meta:
{requiresAuth: true, requiresPermission: 'view_issuethread'}
},
{
path: '/:event/ticket/:id/', name: 'ticket', component: Ticket, meta:
{requiresAuth: true, requiresPermission: 'view_issuethread'}
},
{
path: '/admin/', component: Admin, meta:
{requiresAuth: true, requiresPermission: 'delete_event'},
children: [
{
path: 'files/', name: 'files', component: Files, meta:
{requiresAuth: true, requiresPermission: 'delete_event'}
},
{
path: 'events/', name: 'events', component: Events, meta:
{requiresAuth: true, requiresPermission: 'delete_event'}
},
{
path: '', name: 'admin', component: Debug, meta:
{requiresAuth: true, requiresPermission: 'delete_event'}
},
{
path: 'users/', name: 'users', component: AccessControl, meta:
{requiresAuth: true, requiresPermission: 'delete_event'}
},
]
},
{path: '/user', name: 'user', component: Empty, meta: {requiresAuth: true}},
{path: '*', component: Error}, {path: '*', component: Error},
]; ];
const router = new VueRouter({ const router = new VueRouter({
mode: 'history', mode: 'history',
routes routes,
});
router.beforeEach((to, from, next) => {
if (to.meta.requiresAuth && !store.getters.isLoggedIn) {
console.log("Not logged in, redirecting to login page")
next({
name: 'login',
query: {redirect: to.fullPath},
})
} else if (to.meta.requiresPermission && !store.getters.checkPermission(to.params.event || "*", to.meta.requiresPermission)) {
console.log("Not enough permissions, redirecting to empty page")
next({
path: '/user',
})
} else {
next()
}
});
router.afterEach((to/*, from*/) => {
if (to.params.event) {
//console.log('update last event', to.params.event);
store.commit('updateLastEvent', to.params.event);
}
}); });
export default router; export default router;