Skip to content

Commit

Permalink
Merge pull request #1291 from MR-MKZ/nonsudo
Browse files Browse the repository at this point in the history
Feature -> Revoke access of non sudo admin from panel settings
  • Loading branch information
erfjab authored Sep 2, 2024
2 parents 83476f3 + 4d9106c commit b2fa01e
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 156 deletions.
6 changes: 3 additions & 3 deletions app/dashboard/build/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
<meta name="msapplication-TileColor" content="#2b5797" />
<meta name="msapplication-config" content="/favicon/browserconfig.xml" />
<meta name="theme-color" content="#3B81F6" />
<script type="module" crossorigin src="/dashboard/assets/index.b21d6a54.js"></script>
<link rel="modulepreload" crossorigin href="/dashboard/assets/vendor.8aa8dc93.js">
<link rel="stylesheet" href="/dashboard/assets/index.59de2328.css">
<script type="module" crossorigin src="/dashboard/assets/index.aa165fd5.js"></script>
<link rel="modulepreload" crossorigin href="/dashboard/assets/vendor.23e3f149.js">
<link rel="stylesheet" href="/dashboard/assets/index.876adee7.css">
</head>
<body>
<div id="root"></div>
Expand Down

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions app/dashboard/build/assets/index.aa165fd5.js

Large diffs are not rendered by default.

14 changes: 0 additions & 14 deletions app/dashboard/build/assets/index.b21d6a54.js

This file was deleted.

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions app/dashboard/build/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
<meta name="msapplication-TileColor" content="#2b5797" />
<meta name="msapplication-config" content="/favicon/browserconfig.xml" />
<meta name="theme-color" content="#3B81F6" />
<script type="module" crossorigin src="/dashboard/assets/index.b21d6a54.js"></script>
<link rel="modulepreload" crossorigin href="/dashboard/assets/vendor.8aa8dc93.js">
<link rel="stylesheet" href="/dashboard/assets/index.59de2328.css">
<script type="module" crossorigin src="/dashboard/assets/index.aa165fd5.js"></script>
<link rel="modulepreload" crossorigin href="/dashboard/assets/vendor.23e3f149.js">
<link rel="stylesheet" href="/dashboard/assets/index.876adee7.css">
</head>
<body>
<div id="root"></div>
Expand Down
100 changes: 58 additions & 42 deletions app/dashboard/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useTranslation } from "react-i18next";
import { Link } from "react-router-dom";
import { updateThemeColor } from "utils/themeColor";
import { Language } from "./Language";
import useGetUser from "hooks/useGetUser";

type HeaderProps = {
actions?: ReactNode;
Expand Down Expand Up @@ -81,6 +82,15 @@ export const shouldShowDonation = (): boolean => {
};

export const Header: FC<HeaderProps> = ({ actions }) => {
const { userData, getUserIsSuccess, getUserIsPending } = useGetUser();

const isSudo = () => {
if (!getUserIsPending && getUserIsSuccess) {
return userData.is_sudo;
}
return false;
};

const {
onEditingHosts,
onResetAllUsage,
Expand Down Expand Up @@ -131,38 +141,42 @@ export const Header: FC<HeaderProps> = ({ actions }) => {
position="relative"
></MenuButton>
<MenuList minW="170px" zIndex={99999} className="menuList">
<MenuItem
maxW="170px"
fontSize="sm"
icon={<HostsIcon />}
onClick={onEditingHosts.bind(null, true)}
>
{t("header.hostSettings")}
</MenuItem>
<MenuItem
maxW="170px"
fontSize="sm"
icon={<NodesIcon />}
onClick={onEditingNodes.bind(null, true)}
>
{t("header.nodeSettings")}
</MenuItem>
<MenuItem
maxW="170px"
fontSize="sm"
icon={<NodesUsageIcon />}
onClick={onShowingNodesUsage.bind(null, true)}
>
{t("header.nodesUsage")}
</MenuItem>
<MenuItem
maxW="170px"
fontSize="sm"
icon={<ResetUsageIcon />}
onClick={onResetAllUsage.bind(null, true)}
>
{t("resetAllUsage")}
</MenuItem>
{isSudo() && (
<>
<MenuItem
maxW="170px"
fontSize="sm"
icon={<HostsIcon />}
onClick={onEditingHosts.bind(null, true)}
>
{t("header.hostSettings")}
</MenuItem>
<MenuItem
maxW="170px"
fontSize="sm"
icon={<NodesIcon />}
onClick={onEditingNodes.bind(null, true)}
>
{t("header.nodeSettings")}
</MenuItem>
<MenuItem
maxW="170px"
fontSize="sm"
icon={<NodesUsageIcon />}
onClick={onShowingNodesUsage.bind(null, true)}
>
{t("header.nodesUsage")}
</MenuItem>
<MenuItem
maxW="170px"
fontSize="sm"
icon={<ResetUsageIcon />}
onClick={onResetAllUsage.bind(null, true)}
>
{t("resetAllUsage")}
</MenuItem>
</>
)}
<Link to={DONATION_URL} target="_blank">
<MenuItem
maxW="170px"
Expand All @@ -185,16 +199,18 @@ export const Header: FC<HeaderProps> = ({ actions }) => {
</MenuList>
</Menu>

<IconButton
size="sm"
variant="outline"
aria-label="core settings"
onClick={() => {
useDashboard.setState({ isEditingCore: true });
}}
>
<CoreSettingsIcon />
</IconButton>
{isSudo() && (
<IconButton
size="sm"
variant="outline"
aria-label="core settings"
onClick={() => {
useDashboard.setState({ isEditingCore: true });
}}
>
<CoreSettingsIcon />
</IconButton>
)}

<Language />

Expand Down
31 changes: 31 additions & 0 deletions app/dashboard/src/hooks/useGetUser.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { getAuthToken } from "utils/authStorage";
import { fetch } from "service/http";
import { UserApi, UseGetUserReturn } from "types/User";
import { useQuery } from "react-query";

const fetchUser = async () => {
return await fetch("/admin");
}

const useGetUser = (): UseGetUserReturn => {
const { data, isError, isLoading, isSuccess, error } = useQuery<UserApi, Error>({
queryFn: () => fetchUser()
})

const userDataEmpty: UserApi = {
discord_webook: "",
is_sudo: false,
telegram_id: "",
username: ""
}

return {
userData: data || userDataEmpty,
getUserIsPending: isLoading,
getUserIsSuccess: isSuccess,
getUserIsError: isError,
getUserError: error
}
};

export default useGetUser;
15 changes: 15 additions & 0 deletions app/dashboard/src/types/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ export type UserCreate = Pick<
| "status"
| "note"
>;

export type UserApi = {
discord_webook: string;
is_sudo: boolean;
telegram_id: number | string;
username: string;
}

export type UseGetUserReturn = {
userData: UserApi;
getUserIsPending: boolean;
getUserIsSuccess: boolean;
getUserIsError: boolean;
getUserError: Error | null;
}

0 comments on commit b2fa01e

Please sign in to comment.