2323use OCP \Server ;
2424use OCP \Share \Exceptions \ShareNotFound ;
2525use OCP \Share \IManager ;
26+ use Sabre \DAV \Exception \Forbidden ;
2627
2728abstract class Node implements \Sabre \DAV \INode {
2829 /**
@@ -103,6 +104,31 @@ public function getPath() {
103104 return $ this ->path ;
104105 }
105106
107+ /**
108+ * Check if this node can be renamed
109+ */
110+ public function canRename (): bool {
111+ // the root of a movable mountpoint can be renamed regardless of the file permissions
112+ if ($ this ->info ->getMountPoint () instanceof MoveableMount && $ this ->info ->getInternalPath () === '' ) {
113+ return true ;
114+ }
115+
116+ // we allow renaming the file if either the file has update permissions
117+ if ($ this ->info ->isUpdateable ()) {
118+ return true ;
119+ }
120+
121+ // or the file can be deleted and the parent has create permissions
122+ [$ parentPath ,] = \Sabre \Uri \split ($ this ->path );
123+ if ($ parentPath === null ) {
124+ // can't rename the users home
125+ return false ;
126+ }
127+
128+ $ parent = $ this ->node ->getParent ();
129+ return $ this ->info ->isDeletable () && $ parent ->isCreatable ();
130+ }
131+
106132 /**
107133 * Renames the node
108134 *
@@ -111,10 +137,8 @@ public function getPath() {
111137 * @throws \Sabre\DAV\Exception\Forbidden
112138 */
113139 public function setName ($ name ) {
114- // rename is only allowed if the delete privilege is granted
115- // (basically rename is a copy with delete of the original node)
116- if (!($ this ->info ->isDeletable () || ($ this ->info ->getMountPoint () instanceof MoveableMount && $ this ->info ->getInternalPath () === '' ))) {
117- throw new \Sabre \DAV \Exception \Forbidden ();
140+ if (!$ this ->canRename ()) {
141+ throw new Forbidden ('' );
118142 }
119143
120144 [$ parentPath ,] = \Sabre \Uri \split ($ this ->path );
0 commit comments