From 514a72e80e15e26dd2488f7d633b20586c502bc5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Nd=C3=A8ye=20Anna=20Ndiaye?=
 <anna.ndiaye@savoirfairelinux.com>
Date: Thu, 23 Jul 2020 18:13:39 -0400
Subject: [PATCH] Update user devices tab

Change-Id: I869ea3f866a3fd105cce2932078462bee18c524e
---
 jams-react-client/src/api.js                  |   3 +-
 .../src/components/Devices/Devices.js         | 176 ++++++++++--------
 .../src/views/UserProfile/UserProfile.js      |  13 +-
 .../core/workflows/RegisterDeviceFlow.java    |   4 +-
 4 files changed, 103 insertions(+), 93 deletions(-)

diff --git a/jams-react-client/src/api.js b/jams-react-client/src/api.js
index a54545b2..9cebbd30 100644
--- a/jams-react-client/src/api.js
+++ b/jams-react-client/src/api.js
@@ -32,6 +32,7 @@ import {
     api_path_get_install_lastKnownStep,
     api_path_get_auth_user_search,
     api_path_get_auth_devices,
+    api_path_get_admin_devices,
     api_path_delete_admin_user_revoke,
     api_path_delete_auth_user_revoke,
     api_path_delete_auth_device_revoke,
@@ -85,7 +86,7 @@ export default function configApiCall(api_path, request_type, data, credentials)
     if (data) {
         if (api_path == api_path_get_user_directory_search || api_path == api_path_get_auth_user_search ||
             (api_path == api_path_post_create_user && request_type == 'POST') || api_path == api_path_post_update_user
-            || api_path == api_path_get_auth_devices || api_path == api_path_post_configuration_change_password)
+            || api_path == api_path_get_auth_devices || api_path == api_path_get_admin_devices || api_path == api_path_post_configuration_change_password)
             isSearch = true;
 
         // search dataType
diff --git a/jams-react-client/src/components/Devices/Devices.js b/jams-react-client/src/components/Devices/Devices.js
index f0273bec..b1a06c2f 100755
--- a/jams-react-client/src/components/Devices/Devices.js
+++ b/jams-react-client/src/components/Devices/Devices.js
@@ -1,12 +1,12 @@
-import React from "react";
+import React, {useEffect} from "react";
 import PropTypes from "prop-types";
 import classnames from "classnames";
 // @material-ui/core components
 import { makeStyles } from "@material-ui/core/styles";
-import Checkbox from "@material-ui/core/Checkbox";
 import Tooltip from "@material-ui/core/Tooltip";
 import IconButton from "@material-ui/core/IconButton";
 import Table from "@material-ui/core/Table";
+import TableHead from '@material-ui/core/TableHead';
 import TableRow from "@material-ui/core/TableRow";
 import TableBody from "@material-ui/core/TableBody";
 import TableCell from "@material-ui/core/TableCell";
@@ -16,91 +16,111 @@ import Close from "@material-ui/icons/Close";
 import Check from "@material-ui/icons/Check";
 // core components
 import styles from "assets/jss/material-dashboard-react/components/devicesStyle.js";
+import auth from "auth.js"
+import configApiCall from "api.js";
+import {api_path_get_admin_devices, api_path_get_auth_devices} from "../../globalUrls";
+import axios from "axios";
 
 const useStyles = makeStyles(styles);
 
 export default function Devices(props) {
   const classes = useStyles();
-  const [checked, setChecked] = React.useState([...props.checkedIndexes]);
-  const handleToggle = value => {
-    const currentIndex = checked.indexOf(value);
-    const newChecked = [...checked];
-    if (currentIndex === -1) {
-      newChecked.push(value);
-    } else {
-      newChecked.splice(currentIndex, 1);
-    }
-    setChecked(newChecked);
+  const [devices, setDevices] = React.useState([])
+  const  userData = {
+     "username":props.username
   };
-  const { devicesIndexes, devices, rtlActive } = props;
-  const tableCellClasses = classnames(classes.tableCell, {
-    [classes.tableCellRTL]: rtlActive
-  });
-  return (
-    <Table className={classes.table}>
-      <TableBody>
-        {devicesIndexes.map(value => (
-          <TableRow key={value} className={classes.tableRow}>
-            <TableCell className={tableCellClasses}>
-              <Checkbox
-                checked={checked.indexOf(value) !== -1}
-                tabIndex={-1}
-                onClick={() => handleToggle(value)}
-                checkedIcon={<Check className={classes.checkedIcon} />}
-                icon={<Check className={classes.uncheckedIcon} />}
-                classes={{
-                  checked: classes.checked,
-                  root: classes.root
-                }}
-              />
-            </TableCell>
-            <TableCell className={tableCellClasses}>{devices[value]}</TableCell>
-            <TableCell className={classes.tableActions}>
-              <Tooltip
-                id="tooltip-top"
-                title="Edit Device"
-                placement="top"
-                classes={{ tooltip: classes.tooltip }}
-              >
-                <IconButton
-                  aria-label="Edit"
-                  className={classes.tableActionButton}
-                >
-                  <Edit
-                    className={
-                      classes.tableActionButtonIcon + " " + classes.edit
+
+    useEffect(() => {
+        auth.checkDirectoryType(() => {
+            if(auth.hasAdminScope()){
+                axios(configApiCall(api_path_get_admin_devices, 'GET', userData, null)).then((response)=>{
+                    var resultSet = JSON.parse(response.data.replace(/\s+/g, ' ').trim());
+                    setDevices(resultSet)
                     }
-                  />
-                </IconButton>
-              </Tooltip>
-              <Tooltip
-                id="tooltip-top-start"
-                title="Remove"
-                placement="top"
-                classes={{ tooltip: classes.tooltip }}
-              >
-                <IconButton
-                  aria-label="Close"
-                  className={classes.tableActionButton}
-                >
-                  <Close
-                    className={
-                      classes.tableActionButtonIcon + " " + classes.close
+                ).catch((error) =>{
+                    console.log(error);
+                });
+            }
+            else{
+                axios(configApiCall(api_path_get_auth_devices, 'GET', null, null)).then((response)=>{
+                        setDevices(response.data)
                     }
-                  />
-                </IconButton>
-              </Tooltip>
-            </TableCell>
-          </TableRow>
-        ))}
-      </TableBody>
+                ).catch((error) =>{
+                    console.log(error);
+                });
+            }
+        })
+    }, []);
+
+  function getDeviceStatus(device) {
+      if (!(device.revoked)) {
+          return 'Active';
+      }
+      else {
+          return 'Revoked';
+
+      }
+  }
+  const tableCellClasses = classnames(classes.tableCell);
+  return (
+    <Table className={classes.table}>
+        <TableHead>
+            <TableRow>
+                <TableCell>Device Name</TableCell>
+                <TableCell align="right">Status</TableCell>
+                <TableCell align="right">Actions</TableCell>
+            </TableRow>
+        </TableHead>
+        {devices != null &&
+          <TableBody>
+            {devices.map(device =>
+              <TableRow key={device.displayName} className={classes.tableRow}>
+                <TableCell className={tableCellClasses}>
+                    {device.displayName}
+                </TableCell>
+                <TableCell align="right" className={tableCellClasses}>{getDeviceStatus(device)}</TableCell>
+                {!(device.revoked) &&
+                    <TableCell align="right" className={classes.tableActions}>
+                      <Tooltip
+                        id="tooltip-top"
+                        title="Edit Device"
+                        placement="top"
+                        classes={{ tooltip: classes.tooltip }}
+                      >
+                        <IconButton
+                          aria-label="Edit"
+                          className={classes.tableActionButton}
+                        >
+                          <Edit
+                            className={
+                              classes.tableActionButtonIcon + " " + classes.edit
+                            }
+                          />
+                        </IconButton>
+                      </Tooltip>
+                      <Tooltip
+                        id="tooltip-top-start"
+                        title="Desactivate Device"
+                        placement="top"
+                        classes={{ tooltip: classes.tooltip }}
+                      >
+                        <IconButton
+                          aria-label="Close"
+                          className={classes.tableActionButton}
+                        >
+                          <Close
+                            className={
+                              classes.tableActionButtonIcon + " " + classes.close
+                            }
+                          />
+                        </IconButton>
+                      </Tooltip>
+                    </TableCell>
+                }
+              </TableRow>
+            )}
+          </TableBody>}
     </Table>
   );
 }
 
-Devices.propTypes = {
-  devicesIndexes: PropTypes.arrayOf(PropTypes.number),
-  devices: PropTypes.arrayOf(PropTypes.node),
-  rtlActive: PropTypes.bool,
-  checkedIndexes: PropTypes.array
-};
diff --git a/jams-react-client/src/views/UserProfile/UserProfile.js b/jams-react-client/src/views/UserProfile/UserProfile.js
index cf677978..01efe302 100755
--- a/jams-react-client/src/views/UserProfile/UserProfile.js
+++ b/jams-react-client/src/views/UserProfile/UserProfile.js
@@ -30,13 +30,6 @@ import {
   infoColor
 } from "assets/jss/material-dashboard-react.js";
 
-var devices = [
-  "Samsung Galaxy S10",
-  "iPad 3",
-  "Lenovo T460s Ubuntu 18.04",
-  "Lenovo T460s Windows 10"
-];
-
 
 function TabPanel(props) {
   const { children, value, index, ...other } = props;
@@ -116,11 +109,7 @@ export default function UserProfile(props) {
         <EditCreateUserProfile username={username} createUser={false}/>
       </TabPanel>
       <TabPanel value={value} index={1}>
-        <Devices
-          checkedIndexes={[0, 3]}
-          devicesIndexes={[0, 1, 2, 3]}
-          devices={devices}
-        />
+        <Devices username={props.username}/>
       </TabPanel>
       <TabPanel value={value} index={2}>
         <div className={classes.searchWrapper}>
diff --git a/jams-server/src/main/java/net/jami/jams/server/core/workflows/RegisterDeviceFlow.java b/jams-server/src/main/java/net/jami/jams/server/core/workflows/RegisterDeviceFlow.java
index e945c952..3361566a 100644
--- a/jams-server/src/main/java/net/jami/jams/server/core/workflows/RegisterDeviceFlow.java
+++ b/jams-server/src/main/java/net/jami/jams/server/core/workflows/RegisterDeviceFlow.java
@@ -58,7 +58,7 @@ public class RegisterDeviceFlow {
             Device device = new Device();
             device.setCertificationRequest(registrationRequest.getCsr());
             device.setOwner(username);
-            device.setDisplayName(userProfile.getFirstName()+" "+userProfile.getLastName());
+            device.setDisplayName(registrationRequest.getDeviceName());
             device = certificateAuthority.getSignedCertificate(user, device);
             if(device == null){
                 log.error("Could not succesfully create a device certificate!");
@@ -72,7 +72,7 @@ public class RegisterDeviceFlow {
                     device.getCertificate().getPublicKey(), user.getEthAddress());
             response.setDeviceReceipt(devReceipt[0]);
             response.setReceiptSignature(devReceipt[1]);
-            response.setDisplayName(device.getDisplayName());
+            response.setDisplayName(userProfile.getFirstName()+" "+userProfile.getLastName());
             //We need to set
             response.setNameServer(nameServer.getURI());
             if(userProfile.getProfilePicture() != null) response.setUserPhoto(userProfile.getProfilePicture());
-- 
GitLab