diff --git a/management/src/main/java/eu/nebulous/resource/discovery/monitor/controller/DeviceManagementController.java b/management/src/main/java/eu/nebulous/resource/discovery/monitor/controller/DeviceManagementController.java index de33edb..34caf74 100644 --- a/management/src/main/java/eu/nebulous/resource/discovery/monitor/controller/DeviceManagementController.java +++ b/management/src/main/java/eu/nebulous/resource/discovery/monitor/controller/DeviceManagementController.java @@ -2,14 +2,14 @@ package eu.nebulous.resource.discovery.monitor.controller; import eu.nebulous.resource.discovery.monitor.model.Device; import eu.nebulous.resource.discovery.monitor.model.DeviceException; -import eu.nebulous.resource.discovery.monitor.service.DeviceConversionService; import eu.nebulous.resource.discovery.monitor.service.DeviceManagementService; -import eu.nebulous.resource.discovery.registration.IRegistrationRequestProcessor; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.http.MediaType; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.GrantedAuthority; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -21,10 +21,29 @@ import java.util.List; @PreAuthorize("hasAuthority('ROLE_ADMIN')") public class DeviceManagementController { private final DeviceManagementService deviceService; - private final DeviceConversionService deviceConversionService; - private final IRegistrationRequestProcessor deviceRequestProcessor; - @GetMapping(value = { "/device", "/device/all" }, produces = MediaType.APPLICATION_JSON_VALUE) + private boolean isAuthenticated(Authentication authentication) { + return authentication!=null && StringUtils.isNotBlank(authentication.getName()); + } + + private boolean isAdmin(Authentication authentication) { + if (isAuthenticated(authentication)) { + return authentication.getAuthorities().stream() + .map(GrantedAuthority::getAuthority) + .anyMatch("ROLE_ADMIN"::equals); + } + return false; + } + + @PreAuthorize("hasAuthority('ROLE_ADMIN') || hasAuthority('ROLE_USER')") + @GetMapping(value = "/device", produces = MediaType.APPLICATION_JSON_VALUE) + public List listDevicesUser(Authentication authentication) { + return isAuthenticated(authentication) + ? deviceService.getByOwner(authentication.getName().trim()) + : listDevicesAll(); + } + + @GetMapping(value = "/device/all", produces = MediaType.APPLICATION_JSON_VALUE) public List listDevicesAll() { return deviceService.getAll(); } @@ -34,10 +53,16 @@ public class DeviceManagementController { return deviceService.getByOwner(owner); } + @PreAuthorize("hasAuthority('ROLE_ADMIN') || hasAuthority('ROLE_USER')") @GetMapping(value = "/device/{id}", produces = MediaType.APPLICATION_JSON_VALUE) - public Device getDevice(@PathVariable String id) { - return deviceService.getById(id) - .orElseThrow(() -> new DeviceException("Not found device with id: "+id)); + public Device getDevice(@PathVariable String id, Authentication authentication) { + Device device = deviceService.getById(id) + .orElseThrow(() -> new DeviceException("Not found device with id: " + id)); + if (isAuthenticated(authentication) + && ! authentication.getName().trim().equals(device.getOwner()) + && ! isAdmin(authentication)) + throw new DeviceException("Cannot retrieve device with id: " + id); + return device; } @GetMapping(value = "/device/ipaddress/{ipAddress}", produces = MediaType.APPLICATION_JSON_VALUE) diff --git a/management/src/main/resources/static/freebees_webdesign_6/device-view.html b/management/src/main/resources/static/freebees_webdesign_6/device-view.html new file mode 100644 index 0000000..a72adee --- /dev/null +++ b/management/src/main/resources/static/freebees_webdesign_6/device-view.html @@ -0,0 +1,470 @@ + + + + + + + + NebulOuS Resource Discovery - Management page + + + + + + + + + + + + + + + + +
+
+ +
+
+ + +       + + + +       + +
+
+
+ +
+ +
+ +
+

* * * CAUTION: YOU'RE VIEWING A DEVICE YOU DON'T OWN * * *

+ +

Device ---

+ + + +       + +       + +       + +

 

+ +
+
+
Device details
+
+ + +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ + +
+ +
+ +
+
+ +
+
Device metrics
+
+ + ++++ TODO ++++ + + + +
+ +
+ +
+
+
+
+
+ + + +
+
+
+
Fbee 2022 copyright
+ + \ No newline at end of file diff --git a/management/src/main/resources/static/freebees_webdesign_6/devices.html b/management/src/main/resources/static/freebees_webdesign_6/devices.html index 4b413c0..4fc8cf2 100644 --- a/management/src/main/resources/static/freebees_webdesign_6/devices.html +++ b/management/src/main/resources/static/freebees_webdesign_6/devices.html @@ -19,12 +19,23 @@