Added endpoint for checking if user is an admin

This commit is contained in:
Mutzi 2022-09-05 16:29:13 +02:00
parent 1491e2b6f3
commit c347a10e9f
4 changed files with 24 additions and 1 deletions

View File

@ -88,5 +88,9 @@ namespace api {
cbk(dto::Responses::get_badreq_res("Validation error"));
}
}
void admin::is_admin(req_type, cbk_type cbk) {
cbk(dto::Responses::get_success_res());
}
}
#pragma clang diagnostic pop

View File

@ -24,6 +24,7 @@ public:
METHOD_ADD(admin::logout, "/logout", drogon::Post, "Login", "Admin");
METHOD_ADD(admin::delete_user, "/delete", drogon::Post, "Login", "Admin");
METHOD_ADD(admin::disable_2fa, "/disable_2fa", drogon::Post, "Login", "Admin");
METHOD_ADD(admin::is_admin, "/is_admin", drogon::Get, "Login", "Admin");
METHOD_LIST_END
void users(req_type, cbk_type);
@ -31,6 +32,7 @@ public:
void logout(req_type, cbk_type);
void delete_user(req_type, cbk_type);
void disable_2fa(req_type, cbk_type);
void is_admin(req_type, cbk_type);
};
class auth : public drogon::HttpController<auth> {

View File

@ -5,6 +5,7 @@ import { useRouter, RouterLink } from 'vue-router';
import type { TokenInjectType } from '@/api';
import { useMessage, NMenu, NPageHeader, NIcon } from 'naive-ui';
import { BareMetalServer02 } from '@vicons/carbon';
import { is_admin } from '@/api/admin';
const router = useRouter();
const message = useMessage();
@ -69,6 +70,19 @@ const menuOptions: MenuOption[] = [
key: 'login'
}
];
if (await is_admin(jwt.value ?? ''))
menuOptions.unshift({
label: () =>
h(
RouterLink,
{
to: '/admin'
},
{ default: () => 'Admin' }
),
key: 'admin'
});
</script>
<template>

View File

@ -1,5 +1,5 @@
import type { Requests, Responses } from '@/dto';
import { UserRole, get_token, post_token } from './base';
import { UserRole, get_token, post_token, isErrorResponse } from './base';
export const get_users = (token: string): Promise<Responses.GetUsers> =>
get_token('/api/admin/users', token);
@ -53,3 +53,6 @@ export const disable_tfa = (
},
token
);
export const is_admin = async (token: string): Promise<boolean> =>
!isErrorResponse(await get_token('/api/admin/is_admin', token));