Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/dashboard/src/app/AdminRoute.tsx
2499 views
1
/**
2
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3
* Licensed under the GNU Affero General Public License (AGPL).
4
* See License.AGPL.txt in the project root for license information.
5
*/
6
7
import { useContext } from "react";
8
import { Redirect, Route } from "react-router";
9
import { UserContext } from "../user-context";
10
import { RoleOrPermission } from "@gitpod/public-api/lib/gitpod/v1/user_pb";
11
12
// A wrapper for <Route> that redirects to the workspaces screen if the user isn't a admin.
13
// This wrapper only accepts the component property
14
export function AdminRoute({ component }: any) {
15
const { user } = useContext(UserContext);
16
return (
17
<Route
18
render={({ location }: any) =>
19
user?.rolesOrPermissions?.includes(RoleOrPermission.ADMIN) ? (
20
<Route component={component}></Route>
21
) : (
22
<Redirect
23
to={{
24
pathname: "/workspaces",
25
state: { from: location },
26
}}
27
/>
28
)
29
}
30
/>
31
);
32
}
33
34