diff --git a/app/save-and-restore/app/doc/index.rst b/app/save-and-restore/app/doc/index.rst
index 6af024597a..aaa1471a2a 100644
--- a/app/save-and-restore/app/doc/index.rst
+++ b/app/save-and-restore/app/doc/index.rst
@@ -528,7 +528,7 @@ the selected PV *around the point in time defined by the PV timestamp*.
Authentication and Authorization
--------------------------------
-Authorization uses a role-based approach like so:
+Authorization is optional (configured on the service) and uses a role-based approach like so:
* Unauthenticated users may read data, i.e. browse the tree and view configurations, snapshots, search and view filters.
* Role "user":
@@ -541,7 +541,8 @@ Authorization uses a role-based approach like so:
* Object is a composite snapshot node
* Object is configuration or folder node with no child nodes
* Object is a filter
-* Role "superuser": +perform restore operation
+ * Perform restore operations
+ * Copy or move objects if the target node is "owned" by current user
* Role "admin": no restrictions
Roles are defined and managed on the service. Role (group) membership is managed in Active Directory or LDAP.
diff --git a/services/save-and-restore/doc/index.rst b/services/save-and-restore/doc/index.rst
index cb8f2b0578..a28a88298b 100644
--- a/services/save-and-restore/doc/index.rst
+++ b/services/save-and-restore/doc/index.rst
@@ -1060,6 +1060,8 @@ Authorization uses a role-based approach like so:
* Object is configuration or folder node with no child nodes
* Object is a filter
* Object is a tag
+ * Perform restore operations
+ * Copy or move objects if the target node is "owned" by current user
* Save-and-restore role "sar-admin": no restrictions
Authentication endpoint
diff --git a/services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/AuthorizationHelper.java b/services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/AuthorizationHelper.java
index 3b4b41e0ee..52953c781a 100644
--- a/services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/AuthorizationHelper.java
+++ b/services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/AuthorizationHelper.java
@@ -26,9 +26,12 @@
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.expression.method.MethodSecurityExpressionOperations;
import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
+import java.security.Principal;
import java.util.List;
import java.util.Optional;
@@ -279,13 +282,22 @@ public boolean mayCreate(MethodSecurityExpressionOperations methodSecurityExpres
/**
* Checks if user is allowed to move or copy objects. This is the case if authorization
- * is disabled or if user has admin role.
+ * is disabled, if the target {@link Node} was created by the user performing the copy/move operation,
+ * or if user has admin role.
*
+ * @param targetNodeId The id of the target {@link Node}
* @param methodSecurityExpressionOperations {@link MethodSecurityExpressionOperations} Spring managed object
* queried for authorization.
* @return false if user may not create the object, otherwise true.
*/
- public boolean mayMoveOrCopy(MethodSecurityExpressionOperations methodSecurityExpressionOperations) {
- return permitAll || methodSecurityExpressionOperations.hasAuthority(ROLE_PREFIX + roleAdmin);
+ public boolean mayMoveOrCopy(String targetNodeId, MethodSecurityExpressionOperations methodSecurityExpressionOperations) {
+ if(permitAll || methodSecurityExpressionOperations.hasAuthority(ROLE_PREFIX + roleAdmin)){
+ return true;
+ }
+
+ Node targetNode = nodeDAO.getNode(targetNodeId);
+
+ return targetNode.getUserName()
+ .equals(((UserDetails) methodSecurityExpressionOperations.getAuthentication().getPrincipal()).getUsername());
}
}
diff --git a/services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/StructureController.java b/services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/StructureController.java
index 2f42e76944..2d61c96d46 100644
--- a/services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/StructureController.java
+++ b/services/save-and-restore/src/main/java/org/phoebus/service/saveandrestore/web/controllers/StructureController.java
@@ -64,7 +64,7 @@ public class StructureController extends BaseController {
*/
@SuppressWarnings("unused")
@PostMapping(value = "/move", produces = JSON)
- @PreAuthorize("@authorizationHelper.mayMoveOrCopy(#root)")
+ @PreAuthorize("@authorizationHelper.mayMoveOrCopy(#to, #root)")
public Node moveNodes(@RequestParam(value = "to") String to,
@RequestBody List nodes,
Principal principal) {
@@ -72,7 +72,7 @@ public Node moveNodes(@RequestParam(value = "to") String to,
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Target node and list of source nodes must all be non-empty.");
}
// Get parent node before move
- Node sourceParentNode = nodeDAO.getParentNode(nodes.get(0));
+ Node sourceParentNode = nodeDAO.getParentNode(nodes.getFirst());
Logger.getLogger(StructureController.class.getName()).info(Thread.currentThread().getName() + " " + (new Date()) + " move");
Node targetNode = nodeDAO.moveNodes(nodes, to, principal.getName());
// Update clients
@@ -84,7 +84,7 @@ public Node moveNodes(@RequestParam(value = "to") String to,
}
/**
- * Copies a list of source nodes to a target (parent) node. Since the source nodes may contain sub-trees at
+ * Copies a list of source nodes to a target (parent) node. Since the source nodes may contain subtrees at
* any depth, the copy operation needs to do a deep copy, which may take some time to complete.
*
* @param to The unique id of the target parent node, which must be a folder. If empty or if
@@ -96,7 +96,7 @@ public Node moveNodes(@RequestParam(value = "to") String to,
*/
@SuppressWarnings("unused")
@PostMapping(value = "/copy", produces = JSON)
- @PreAuthorize("@authorizationHelper.mayMoveOrCopy(#root)")
+ @PreAuthorize("@authorizationHelper.mayMoveOrCopy(#to, #root)")
public Node copyNodes(@RequestParam(value = "to") String to,
@RequestBody List nodes,
Principal principal) {
diff --git a/services/save-and-restore/src/test/java/org/phoebus/service/saveandrestore/web/controllers/StructureControllerTest.java b/services/save-and-restore/src/test/java/org/phoebus/service/saveandrestore/web/controllers/StructureControllerTest.java
index c169a6de12..81bdac0579 100644
--- a/services/save-and-restore/src/test/java/org/phoebus/service/saveandrestore/web/controllers/StructureControllerTest.java
+++ b/services/save-and-restore/src/test/java/org/phoebus/service/saveandrestore/web/controllers/StructureControllerTest.java
@@ -73,9 +73,6 @@ void setUp() {
@Autowired
private String adminAuthorization;
- @Autowired
- private String readOnlyAuthorization;
-
@Autowired
private WebSocketService webSocketService;
@@ -85,7 +82,7 @@ public void resetMocks() {
}
@Test
- public void testMoveNode1() throws Exception {
+ public void testMoveNodeAdmin() throws Exception {
when(nodeDAO.moveNodes(List.of("a"), "b", demoAdmin))
.thenReturn(Node.builder().uniqueId("2").uniqueId("a").userName(demoAdmin).build());
@@ -106,42 +103,40 @@ public void testMoveNode1() throws Exception {
}
@Test
- public void testMoveNode2() throws Exception {
+ public void testMoveNodeUser() throws Exception {
- when(nodeDAO.moveNodes(List.of("a"), "b", demoUser))
+ when(nodeDAO.moveNodes(List.of("a"), "target", demoUser))
.thenReturn(Node.builder().uniqueId("2").uniqueId("a").userName(demoUser).build());
+ when(nodeDAO.getNode("target"))
+ .thenReturn(Node.builder().uniqueId("target").userName("user").build());
MockHttpServletRequestBuilder request = post("/move")
.header(HttpHeaders.AUTHORIZATION, userAuthorization)
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
- .param("to", "b")
+ .param("to", "target")
.param("username", "username");
- mockMvc.perform(request).andExpect(status().isForbidden());
+ mockMvc.perform(request).andExpect(status().isOk());
- verify(webSocketService, times(0)).sendMessageToClients(Mockito.any(WebSocketMessage.class));
+ verify(webSocketService, times(2)).sendMessageToClients(Mockito.any(WebSocketMessage.class));
}
@Test
- public void testMoveNode3() throws Exception {
+ public void testMoveNodeOtherUser() throws Exception {
+
+ when(nodeDAO.getNode("target"))
+ .thenReturn(Node.builder().uniqueId("target").userName("otheruser").build());
MockHttpServletRequestBuilder request = post("/move")
- .header(HttpHeaders.AUTHORIZATION, readOnlyAuthorization)
+ .header(HttpHeaders.AUTHORIZATION, userAuthorization)
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
- .param("to", "b")
+ .param("to", "target")
.param("username", "username");
mockMvc.perform(request).andExpect(status().isForbidden());
- request = post("/move")
- .contentType(JSON)
- .content(objectMapper.writeValueAsString(List.of("a")))
- .param("to", "b")
- .param("username", "username");
-
- mockMvc.perform(request).andExpect(status().isUnauthorized());
verify(webSocketService, times(0)).sendMessageToClients(Mockito.any(WebSocketMessage.class));
}
@@ -175,15 +170,36 @@ public void testMoveNodeTargetIdEmpty() throws Exception {
}
@Test
- public void testCopyNodes1() throws Exception {
+ public void testCopyNodesAdmin() throws Exception {
MockHttpServletRequestBuilder request = post("/copy")
.header(HttpHeaders.AUTHORIZATION, adminAuthorization)
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
.param("to", "target");
mockMvc.perform(request).andExpect(status().isOk());
+ }
+
+ @Test
+ public void testCopyNodesUser() throws Exception{
+
+ when(nodeDAO.getNode("target"))
+ .thenReturn(Node.builder().uniqueId("target").userName("user").build());
+ MockHttpServletRequestBuilder request = post("/copy")
+ .header(HttpHeaders.AUTHORIZATION, userAuthorization)
+ .contentType(JSON)
+ .content(objectMapper.writeValueAsString(List.of("a")))
+ .param("to", "target");
+ mockMvc.perform(request).andExpect(status().isOk());
+
+ verify(webSocketService, times(0)).sendMessageToClients(Mockito.any(WebSocketMessage.class));
+ }
+
+ @Test
+ public void testCopyNodesOtherUser() throws Exception{
- request = post("/copy")
+ when(nodeDAO.getNode("target"))
+ .thenReturn(Node.builder().uniqueId("target").userName("otheruser").build());
+ MockHttpServletRequestBuilder request = post("/copy")
.header(HttpHeaders.AUTHORIZATION, userAuthorization)
.contentType(JSON)
.content(objectMapper.writeValueAsString(List.of("a")))
@@ -194,7 +210,7 @@ public void testCopyNodes1() throws Exception {
}
@Test
- public void testCopyNodes2() throws Exception {
+ public void testCopyNodesNotLoggedIn() throws Exception {
MockHttpServletRequestBuilder request = post("/copy")
.contentType(JSON)