summaryrefslogtreecommitdiffstats
path: root/lib/private/Share20/Manager.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Share20/Manager.php')
-rw-r--r--lib/private/Share20/Manager.php172
1 files changed, 161 insertions, 11 deletions
diff --git a/lib/private/Share20/Manager.php b/lib/private/Share20/Manager.php
index 2a2c64cf383..db9c704871d 100644
--- a/lib/private/Share20/Manager.php
+++ b/lib/private/Share20/Manager.php
@@ -55,17 +55,18 @@ use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\Mail\IMailer;
+use OCP\Security\Events\ValidatePasswordPolicyEvent;
use OCP\Security\IHasher;
use OCP\Security\ISecureRandom;
+use OCP\Share;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IManager;
use OCP\Share\IProviderFactory;
use OCP\Share\IShare;
+use OCP\Share\IShareProvider;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
-use OCP\Share\IShareProvider;
-use OCP\Share;
/**
* This class is the communication hub for all sharing related operations.
@@ -191,8 +192,7 @@ class Manager implements IManager {
// Let others verify the password
try {
- $event = new GenericEvent($password);
- $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event);
+ $this->eventDispatcher->dispatch(new ValidatePasswordPolicyEvent($password));
} catch (HintException $e) {
throw new \Exception($e->getHint());
}
@@ -302,6 +302,7 @@ class Manager implements IManager {
/* Check if this is an incoming share */
$incomingShares = $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_USER, $userMountPoint, -1, 0);
$incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_GROUP, $userMountPoint, -1, 0));
+ $incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_CIRCLE, $userMountPoint, -1, 0));
$incomingShares = array_merge($incomingShares, $this->getSharedWith($share->getSharedBy(), Share::SHARE_TYPE_ROOM, $userMountPoint, -1, 0));
/** @var \OCP\Share\IShare[] $incomingShares */
@@ -359,6 +360,77 @@ class Manager implements IManager {
* @throws \InvalidArgumentException
* @throws \Exception
*/
+ protected function validateExpirationDateInternal(\OCP\Share\IShare $share) {
+ $expirationDate = $share->getExpirationDate();
+
+ if ($expirationDate !== null) {
+ //Make sure the expiration date is a date
+ $expirationDate->setTime(0, 0, 0);
+
+ $date = new \DateTime();
+ $date->setTime(0, 0, 0);
+ if ($date >= $expirationDate) {
+ $message = $this->l->t('Expiration date is in the past');
+ throw new GenericShareException($message, $message, 404);
+ }
+ }
+
+ // If expiredate is empty set a default one if there is a default
+ $fullId = null;
+ try {
+ $fullId = $share->getFullId();
+ } catch (\UnexpectedValueException $e) {
+ // This is a new share
+ }
+
+ if ($fullId === null && $expirationDate === null && $this->shareApiInternalDefaultExpireDate()) {
+ $expirationDate = new \DateTime();
+ $expirationDate->setTime(0,0,0);
+ $expirationDate->add(new \DateInterval('P'.$this->shareApiInternalDefaultExpireDays().'D'));
+ }
+
+ // If we enforce the expiration date check that is does not exceed
+ if ($this->shareApiInternalDefaultExpireDateEnforced()) {
+ if ($expirationDate === null) {
+ throw new \InvalidArgumentException('Expiration date is enforced');
+ }
+
+ $date = new \DateTime();
+ $date->setTime(0, 0, 0);
+ $date->add(new \DateInterval('P' . $this->shareApiInternalDefaultExpireDays() . 'D'));
+ if ($date < $expirationDate) {
+ $message = $this->l->t('Can’t set expiration date more than %s days in the future', [$this->shareApiInternalDefaultExpireDays()]);
+ throw new GenericShareException($message, $message, 404);
+ }
+ }
+
+ $accepted = true;
+ $message = '';
+ \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [
+ 'expirationDate' => &$expirationDate,
+ 'accepted' => &$accepted,
+ 'message' => &$message,
+ 'passwordSet' => $share->getPassword() !== null,
+ ]);
+
+ if (!$accepted) {
+ throw new \Exception($message);
+ }
+
+ $share->setExpirationDate($expirationDate);
+
+ return $share;
+ }
+
+ /**
+ * Validate if the expiration date fits the system settings
+ *
+ * @param \OCP\Share\IShare $share The share to validate the expiration date of
+ * @return \OCP\Share\IShare The modified share object
+ * @throws GenericShareException
+ * @throws \InvalidArgumentException
+ * @throws \Exception
+ */
protected function validateExpirationDate(\OCP\Share\IShare $share) {
$expirationDate = $share->getExpirationDate();
@@ -635,8 +707,16 @@ class Manager implements IManager {
//Verify share type
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
$this->userCreateChecks($share);
+
+ //Verify the expiration date
+ $share = $this->validateExpirationDateInternal($share);
+
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
$this->groupCreateChecks($share);
+
+ //Verify the expiration date
+ $share = $this->validateExpirationDateInternal($share);
+
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
$this->linkCreateChecks($share);
$this->setLinkParent($share);
@@ -652,7 +732,7 @@ class Manager implements IManager {
);
//Verify the expiration date
- $this->validateExpirationDate($share);
+ $share = $this->validateExpirationDate($share);
//Verify the password
$this->verifyPassword($share->getPassword());
@@ -849,8 +929,20 @@ class Manager implements IManager {
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
$this->userCreateChecks($share);
+
+ if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
+ //Verify the expiration date
+ $this->validateExpirationDate($share);
+ $expirationDateUpdated = true;
+ }
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
$this->groupCreateChecks($share);
+
+ if ($share->getExpirationDate() != $originalShare->getExpirationDate()) {
+ //Verify the expiration date
+ $this->validateExpirationDate($share);
+ $expirationDateUpdated = true;
+ }
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
$this->linkCreateChecks($share);
@@ -928,6 +1020,30 @@ class Manager implements IManager {
}
/**
+ * Accept a share.
+ *
+ * @param IShare $share
+ * @param string $recipientId
+ * @return IShare The share object
+ * @throws \InvalidArgumentException
+ * @since 9.0.0
+ */
+ public function acceptShare(IShare $share, string $recipientId): IShare {
+ [$providerId, ] = $this->splitFullId($share->getFullId());
+ $provider = $this->factory->getProvider($providerId);
+
+ if (!method_exists($provider, 'acceptShare')) {
+ // TODO FIX ME
+ throw new \InvalidArgumentException('Share provider does not support accepting');
+ }
+ $provider->acceptShare($share, $recipientId);
+ $event = new GenericEvent($share);
+ $this->eventDispatcher->dispatch('OCP\Share::postAcceptShare', $event);
+
+ return $share;
+ }
+
+ /**
* Updates the password of the given share if it is not the same as the
* password of the original share.
*
@@ -1313,8 +1429,7 @@ class Manager implements IManager {
}
protected function checkExpireDate($share) {
- if ($share->getExpirationDate() !== null &&
- $share->getExpirationDate() <= new \DateTime()) {
+ if ($share->isExpired()) {
$this->deleteShare($share);
throw new ShareNotFound($this->l->t('The requested share does not exist anymore'));
}
@@ -1551,7 +1666,7 @@ class Manager implements IManager {
}
/**
- * Is default expire date enabled
+ * Is default link expire date enabled
*
* @return bool
*/
@@ -1560,7 +1675,7 @@ class Manager implements IManager {
}
/**
- * Is default expire date enforced
+ * Is default link expire date enforced
*`
* @return bool
*/
@@ -1569,9 +1684,9 @@ class Manager implements IManager {
$this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
}
+
/**
- * Number of default expire days
- *shareApiLinkAllowPublicUpload
+ * Number of default link expire days
* @return int
*/
public function shareApiLinkDefaultExpireDays() {
@@ -1579,6 +1694,34 @@ class Manager implements IManager {
}
/**
+ * Is default internal expire date enabled
+ *
+ * @return bool
+ */
+ public function shareApiInternalDefaultExpireDate(): bool {
+ return $this->config->getAppValue('core', 'shareapi_default_internal_expire_date', 'no') === 'yes';
+ }
+
+ /**
+ * Is default expire date enforced
+ *`
+ * @return bool
+ */
+ public function shareApiInternalDefaultExpireDateEnforced(): bool {
+ return $this->shareApiInternalDefaultExpireDate() &&
+ $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no') === 'yes';
+ }
+
+
+ /**
+ * Number of default expire days
+ * @return int
+ */
+ public function shareApiInternalDefaultExpireDays(): int {
+ return (int)$this->config->getAppValue('core', 'shareapi_internal_expire_after_n_days', '7');
+ }
+
+ /**
* Allow public upload on link shares
*
* @return bool
@@ -1672,4 +1815,11 @@ class Manager implements IManager {
return true;
}
+ public function getAllShares(): iterable {
+ $providers = $this->factory->getAllProviders();
+
+ foreach ($providers as $provider) {
+ yield from $provider->getAllShares();
+ }
+ }
}