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
|
|
|
|
2021-04-08 12:58:49 +08:00
|
|
|
export default ({ store, params, $api, $auth }: Context) => {
|
2021-03-22 13:03:41 +08:00
|
|
|
if (params.author && params.slug) {
|
2021-04-08 12:58:49 +08:00
|
|
|
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) {
|
2021-04-08 12:58:49 +08:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
2021-04-08 12:58:49 +08:00
|
|
|
} 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);
|
2021-03-23 11:07:53 +08:00
|
|
|
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
|
|
|
};
|