Hangar/frontend/middleware/routePermissions.ts

40 lines
1.5 KiB
TypeScript
Raw Normal View History

2021-02-05 08:22:22 +08:00
import { Context } from '@nuxt/types';
import { UserPermissions } from 'hangar-api';
2021-02-08 06:06:06 +08:00
import { AuthState } from '~/store/auth';
2021-02-05 08:22:22 +08:00
export default ({ store, params, $api, $auth }: Context) => {
2021-03-22 13:03:41 +08:00
if (params.author && params.slug) {
if ($auth.isLoggedIn()) {
2021-03-22 13:03:41 +08:00
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()) {
2021-03-22 13:03:41 +08:00
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()) {
2021-02-08 06:06:06 +08:00
// Catch-all (just use global permissions)
store.commit('auth/SET_ROUTE_PERMISSIONS', (store.state.auth as AuthState).user!.headerData.globalPermission);
return;
2021-02-05 08:22:22 +08:00
}
2021-03-22 13:03:41 +08:00
store.commit('auth/SET_ROUTE_PERMISSIONS', null);
2021-02-05 08:22:22 +08:00
};