Hangar/frontend/middleware/routePermissions.ts
Jake Potrebic 7dc5adf2a8
couple fixes
* the most annoying issue surrounding hot reloads during dev
* HangarDecisionVoter was re-using the same eval context for SpEL expressions
* simplified project settings initialization
* perm api didn't work with no auth
2021-04-07 21:58:49 -07:00

41 lines
1.6 KiB
TypeScript

import { Context } from '@nuxt/types';
import { UserPermissions } from 'hangar-api';
import { AuthState } from '~/store/auth';
export default ({ store, params, $api, $auth }: Context) => {
if (params.author && params.slug) {
if ($auth.isLoggedIn()) {
return $api
.request<UserPermissions>('permissions', true, 'get', {
author: params.author,
slug: params.slug,
})
.then((userPermissions) => {
store.commit('auth/SET_ROUTE_PERMISSIONS', userPermissions.permissionBinString);
})
.catch(() => {
store.commit('auth/SET_ROUTE_PERMISSIONS', null);
});
}
} else if (params.user) {
if ($auth.isLoggedIn()) {
return $api
.request<UserPermissions>('permissions', true, 'get', {
organization: params.user,
})
.then((userPermissions) => {
store.commit('auth/SET_ROUTE_PERMISSIONS', userPermissions.permissionBinString);
})
.catch(() => {
store.commit('auth/SET_ROUTE_PERMISSIONS', null);
});
}
} else if ($auth.isLoggedIn()) {
// Catch-all (just use global permissions)
store.commit('auth/SET_ROUTE_PERMISSIONS', (store.state.auth as AuthState).user!.headerData.globalPermission);
return;
}
store.commit('auth/SET_ROUTE_PERMISSIONS', null);
// TODO other route permissions
};