From c347a10e9f2da87da2eed4b4abea32a9649a11d7 Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 5 Sep 2022 16:29:13 +0200 Subject: [PATCH] Added endpoint for checking if user is an admin --- backend/src/controllers/admin.cpp | 4 ++++ backend/src/controllers/controllers.h | 2 ++ frontend/src/App.vue | 14 ++++++++++++++ frontend/src/api/admin.ts | 5 ++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/backend/src/controllers/admin.cpp b/backend/src/controllers/admin.cpp index 9de8720..9db135e 100644 --- a/backend/src/controllers/admin.cpp +++ b/backend/src/controllers/admin.cpp @@ -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 \ No newline at end of file diff --git a/backend/src/controllers/controllers.h b/backend/src/controllers/controllers.h index fa2a8c1..fe43de7 100644 --- a/backend/src/controllers/controllers.h +++ b/backend/src/controllers/controllers.h @@ -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 { diff --git a/frontend/src/App.vue b/frontend/src/App.vue index ce980a3..54e6c2c 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -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' + });