Explorar o código

Implemented auth module on namespace list for creation & deletion

jnfrati %!s(int64=4) %!d(string=hai) anos
pai
achega
09ee209140

+ 29 - 25
dashboard/src/main/home/Home.tsx

@@ -27,6 +27,7 @@ import Sidebar from "./sidebar/Sidebar";
 import PageNotFound from "components/PageNotFound";
 import PageNotFound from "components/PageNotFound";
 import DeleteNamespaceModal from "./modals/DeleteNamespaceModal";
 import DeleteNamespaceModal from "./modals/DeleteNamespaceModal";
 import { fakeGuardedRoute } from "shared/auth/RouteGuard";
 import { fakeGuardedRoute } from "shared/auth/RouteGuard";
+import { withAuth, WithAuthProps } from "shared/auth/AuthorizationHoc";
 
 
 // Guarded components
 // Guarded components
 const GuardedProjectSettings = fakeGuardedRoute("settings", "", [
 const GuardedProjectSettings = fakeGuardedRoute("settings", "", [
@@ -39,12 +40,13 @@ const GuardedIntegrations = fakeGuardedRoute("integrations", "", [
   "list",
   "list",
 ])(Integrations);
 ])(Integrations);
 
 
-type PropsType = RouteComponentProps & {
-  logOut: () => void;
-  currentProject: ProjectType;
-  currentCluster: ClusterType;
-  currentRoute: PorterUrl;
-};
+type PropsType = RouteComponentProps &
+  WithAuthProps & {
+    logOut: () => void;
+    currentProject: ProjectType;
+    currentCluster: ClusterType;
+    currentRoute: PorterUrl;
+  };
 
 
 type StateType = {
 type StateType = {
   forceSidebar: boolean;
   forceSidebar: boolean;
@@ -514,24 +516,26 @@ class Home extends Component<PropsType, StateType> {
             <IntegrationsInstructionsModal />
             <IntegrationsInstructionsModal />
           </Modal>
           </Modal>
         )}
         )}
-        {currentModal === "NamespaceModal" && (
-          <Modal
-            onRequestClose={() => setCurrentModal(null, null)}
-            width="600px"
-            height="220px"
-          >
-            <NamespaceModal />
-          </Modal>
-        )}
-        {currentModal === "DeleteNamespaceModal" && (
-          <Modal
-            onRequestClose={() => setCurrentModal(null, null)}
-            width="700px"
-            height="280px"
-          >
-            <DeleteNamespaceModal />
-          </Modal>
-        )}
+        {this.props.isAuthorized("namespace", "", ["get", "create"]) &&
+          currentModal === "NamespaceModal" && (
+            <Modal
+              onRequestClose={() => setCurrentModal(null, null)}
+              width="600px"
+              height="220px"
+            >
+              <NamespaceModal />
+            </Modal>
+          )}
+        {this.props.isAuthorized("namespace", "", ["get", "delete"]) &&
+          currentModal === "DeleteNamespaceModal" && (
+            <Modal
+              onRequestClose={() => setCurrentModal(null, null)}
+              width="700px"
+              height="280px"
+            >
+              <DeleteNamespaceModal />
+            </Modal>
+          )}
 
 
         {this.renderSidebar()}
         {this.renderSidebar()}
 
 
@@ -560,7 +564,7 @@ class Home extends Component<PropsType, StateType> {
 
 
 Home.contextType = Context;
 Home.contextType = Context;
 
 
-export default withRouter(Home);
+export default withRouter(withAuth(Home));
 
 
 const ViewWrapper = styled.div`
 const ViewWrapper = styled.div`
   height: 100%;
   height: 100%;

+ 26 - 20
dashboard/src/main/home/cluster-dashboard/dashboard/NamespaceList.tsx

@@ -4,6 +4,7 @@ import { Context } from "shared/Context";
 import { ClusterType, ProjectType } from "shared/types";
 import { ClusterType, ProjectType } from "shared/types";
 import { pushFiltered } from "shared/routing";
 import { pushFiltered } from "shared/routing";
 import { useHistory, useLocation } from "react-router";
 import { useHistory, useLocation } from "react-router";
+import useAuth from "shared/auth/useAuth";
 
 
 const OptionsDropdown: React.FC = ({ children }) => {
 const OptionsDropdown: React.FC = ({ children }) => {
   const [isOpen, setIsOpen] = useState(false);
   const [isOpen, setIsOpen] = useState(false);
@@ -68,6 +69,8 @@ export const NamespaceList: React.FunctionComponent = () => {
     setCurrentModal("DeleteNamespaceModal", namespace);
     setCurrentModal("DeleteNamespaceModal", namespace);
   };
   };
 
 
+  const [isAuthorized] = useAuth();
+
   const isAvailableForDeletion = (namespaceName: string) => {
   const isAvailableForDeletion = (namespaceName: string) => {
     // Only the namespaces that doesn't start with kube- or has by name default will be
     // Only the namespaces that doesn't start with kube- or has by name default will be
     // available for deletion (as those are the k8s namespaces)
     // available for deletion (as those are the k8s namespaces)
@@ -133,18 +136,20 @@ export const NamespaceList: React.FunctionComponent = () => {
   return (
   return (
     <NamespaceListWrapper>
     <NamespaceListWrapper>
       <ControlRow>
       <ControlRow>
-        <Button
-          onClick={() =>
-            setCurrentModal(
-              "NamespaceModal",
-              namespaces.map((namespace) => ({
-                value: namespace.metadata.name,
-              }))
-            )
-          }
-        >
-          <i className="material-icons">add</i> Add namespace
-        </Button>
+        {isAuthorized("namespace", "", ["get", "create"]) && (
+          <Button
+            onClick={() =>
+              setCurrentModal(
+                "NamespaceModal",
+                namespaces.map((namespace) => ({
+                  value: namespace.metadata.name,
+                }))
+              )
+            }
+          >
+            <i className="material-icons">add</i> Add namespace
+          </Button>
+        )}
       </ControlRow>
       </ControlRow>
       <NamespacesGrid>
       <NamespacesGrid>
         {sortedNamespaces.map((namespace) => {
         {sortedNamespaces.map((namespace) => {
@@ -165,14 +170,15 @@ export const NamespaceList: React.FunctionComponent = () => {
                   {namespace?.status?.phase}
                   {namespace?.status?.phase}
                 </Status>
                 </Status>
               </ContentContainer>
               </ContentContainer>
-              {isAvailableForDeletion(namespace?.metadata?.name) && (
-                <OptionsDropdown>
-                  <DropdownOption onClick={() => onDelete(namespace)}>
-                    <i className="material-icons-outlined">delete</i>
-                    <span>Delete</span>
-                  </DropdownOption>
-                </OptionsDropdown>
-              )}
+              {isAuthorized("namespace", "", ["get", "delete"]) &&
+                isAvailableForDeletion(namespace?.metadata?.name) && (
+                  <OptionsDropdown>
+                    <DropdownOption onClick={() => onDelete(namespace)}>
+                      <i className="material-icons-outlined">delete</i>
+                      <span>Delete</span>
+                    </DropdownOption>
+                  </OptionsDropdown>
+                )}
             </StyledCard>
             </StyledCard>
           );
           );
         })}
         })}