]> source.dussan.org Git - nextcloud-server.git/commitdiff
Properly reset pw expiration
authorVincent Petry <vincent@nextcloud.com>
Wed, 13 Apr 2022 13:46:30 +0000 (15:46 +0200)
committerVincent Petry <vincent@nextcloud.com>
Wed, 13 Apr 2022 13:46:30 +0000 (15:46 +0200)
When requesting a new password for share by mail link, now we correctly
reset the expiration date.

Signed-off-by: Vincent Petry <vincent@nextcloud.com>
apps/files_sharing/lib/Controller/ShareAPIController.php
lib/private/Share20/Manager.php

index d324af3e9f26fe5d01c7352950d95bf9d55d66fe..c044148513261b3185ca4df42fbd9cdb3250e205 100644 (file)
@@ -571,10 +571,6 @@ class ShareAPIController extends OCSController {
                        // Set password
                        if ($password !== '') {
                                $share->setPassword($password);
-                               // Shares shared by email have temporary passwords by default
-                               if ($shareType === IShare::TYPE_EMAIL) {
-                                       $this->setSharePasswordExpirationTime($share);
-                               }
                        }
 
                        // Only share by mail have a recipient
@@ -1182,9 +1178,6 @@ class ShareAPIController extends OCSController {
                                $share->setPassword(null);
                        } elseif ($password !== null) {
                                $share->setPassword($password);
-                               if ($share->getShareType() === IShare::TYPE_EMAIL) {
-                                       $this->setSharePasswordExpirationTime($share);
-                               }
                        }
 
                        if ($label !== null) {
@@ -1521,35 +1514,6 @@ class ShareAPIController extends OCSController {
                return $date;
        }
 
-       /**
-        * Set the share's password expiration time
-        */
-       private function setSharePasswordExpirationTime(IShare $share): void {
-               if ($this->config->getSystemValue('allow_mail_share_permanent_password')) {
-                       // Sets password expiration date to NULL
-                       $share->setPasswordExpirationTime();
-                       return;
-               }
-               // Sets password expiration date
-               $expirationTime = null;
-               try {
-                       $now = new \DateTime();
-                       $expirationInterval = $this->config->getSystemValue('share_temporary_password_expiration_interval');
-                       if ($expirationInterval === '' || is_null($expirationInterval)) {
-                               $expirationInterval = 'P0DT15M';
-                       }
-                       $expirationTime = $now->add(new \DateInterval($expirationInterval));
-               } catch (\Exception $e) {
-                       // Catches invalid format for system value 'share_temporary_password_expiration_interval'
-                       \OC::$server->getLogger()->logException($e, [
-                               'message' => 'The \'share_temporary_password_expiration_interval\' system setting does not respect the DateInterval::__construct() format. Setting it to \'P0DT15M\''
-                       ]);
-                       $expirationTime = $now->add(new \DateInterval('P0DT15M'));
-               } finally {
-                       $share->setPasswordExpirationTime($expirationTime);
-               }
-       }
-
        /**
         * Since we have multiple providers but the OCS Share API v1 does
         * not support this we need to check all backends.
index 4e87c37fedbcb183f445699a6c96d022528552f2..a5d876e8015902aa029d0b523c9d5713afca569f 100644 (file)
@@ -1148,11 +1148,18 @@ class Manager implements IManager {
                        // If a password is set. Hash it!
                        if (!empty($share->getPassword())) {
                                $share->setPassword($this->hasher->hash($share->getPassword()));
+                               if ($share->getShareType() === IShare::TYPE_EMAIL) {
+                                       // Shares shared by email have temporary passwords
+                                       $this->setSharePasswordExpirationTime($share);
+                               }
 
                                return true;
                        } else {
                                // Empty string and null are seen as NOT password protected
                                $share->setPassword(null);
+                               if ($share->getShareType() === IShare::TYPE_EMAIL) {
+                                       $share->setPasswordExpirationTime(null);
+                               }
                                return true;
                        }
                } else {
@@ -1164,6 +1171,36 @@ class Manager implements IManager {
                return false;
        }
 
+       /**
+        * Set the share's password expiration time
+        */
+       private function setSharePasswordExpirationTime(IShare $share): void {
+               if ($this->config->getSystemValue('allow_mail_share_permanent_password')) {
+                       // Sets password expiration date to NULL
+                       $share->setPasswordExpirationTime();
+                       return;
+               }
+               // Sets password expiration date
+               $expirationTime = null;
+               try {
+                       $now = new \DateTime();
+                       $expirationInterval = $this->config->getSystemValue('share_temporary_password_expiration_interval');
+                       if ($expirationInterval === '' || is_null($expirationInterval)) {
+                               $expirationInterval = 'P0DT15M';
+                       }
+                       $expirationTime = $now->add(new \DateInterval($expirationInterval));
+               } catch (\Exception $e) {
+                       // Catches invalid format for system value 'share_temporary_password_expiration_interval'
+                       \OC::$server->getLogger()->logException($e, [
+                               'message' => 'The \'share_temporary_password_expiration_interval\' system setting does not respect the DateInterval::__construct() format. Setting it to \'P0DT15M\''
+                       ]);
+                       $expirationTime = $now->add(new \DateInterval('P0DT15M'));
+               } finally {
+                       $share->setPasswordExpirationTime($expirationTime);
+               }
+       }
+
+
        /**
         * Delete all the children of this share
         * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in