Implement expiration date for federated shares
authoralex-z <blackslayer4@gmail.com>
Mon, 1 Nov 2021 15:37:23 +0000 (17:37 +0200)
committerMatthieu Gallien (Rebase PR Action) <matthieu_gallien@yahoo.fr>
Wed, 3 Nov 2021 10:54:18 +0000 (10:54 +0000)
Signed-off-by: alex-z <blackslayer4@gmail.com>
src/gui/sharemanager.cpp
src/gui/sharemanager.h
src/gui/shareusergroupwidget.cpp
src/gui/shareusergroupwidget.h
src/libsync/capabilities.cpp
src/libsync/capabilities.h

index 4490ff0677931354e83cdc384ab39b36c24c595f..afebb981f78b88a77980cacc00c8d4120f6106ab 100644 (file)
@@ -146,6 +146,12 @@ void Share::deleteShare()
     job->deleteShare(getId());
 }
 
+bool Share::isUserGroupShare(const ShareType type)
+{
+    return (type == Share::TypeUser || type == Share::TypeGroup || type == Share::TypeEmail || type == Share::TypeRoom
+        || type == Share::TypeRemote);
+}
+
 void Share::slotDeleted()
 {
     updateFolder(_account, _path);
@@ -317,7 +323,7 @@ UserGroupShare::UserGroupShare(AccountPtr account,
     , _note(note)
     , _expireDate(expireDate)
 {
-    Q_ASSERT(shareType == TypeUser || shareType == TypeGroup || shareType == TypeEmail || shareType == TypeRoom);
+    Q_ASSERT(Share::isUserGroupShare(shareType));
     Q_ASSERT(shareWith);
 }
 
@@ -487,7 +493,7 @@ void ShareManager::slotSharesFetched(const QJsonDocument &reply)
 
         if (shareType == Share::TypeLink) {
             newShare = parseLinkShare(data);
-        } else if (shareType == Share::TypeGroup || shareType == Share::TypeUser || shareType == Share::TypeEmail || shareType == Share::TypeRoom) {
+        } else if (Share::isUserGroupShare(static_cast <Share::ShareType>(shareType))) {
             newShare = parseUserGroupShare(data);
         } else {
             newShare = parseShare(data);
index f30a26576c181049136fb2e9508dfea3ab1385fa..06e53c02669cc0554acb822023d78feaab8f25c2 100644 (file)
@@ -130,6 +130,11 @@ public:
      */
     void deleteShare();
 
+     /*
+     * Is it a share with a user or group (local or remote)
+     */
+    static bool isUserGroupShare(const ShareType type);
+
 signals:
     void permissionsSet();
     void shareDeleted();
index 45d2c9ec7d7c5facfc184b077f73d60319f68b15..4b463ad70c68a533507915698039fa3c86b492fb 100644 (file)
@@ -249,7 +249,7 @@ void ShareUserGroupWidget::slotSharesFetched(const QList<QSharedPointer<Share>>
         }
 
 
-        Q_ASSERT(share->getShareType() == Share::TypeUser || share->getShareType() == Share::TypeGroup || share->getShareType() == Share::TypeEmail || share->getShareType() == Share::TypeRoom);
+        Q_ASSERT(Share::isUserGroupShare(share->getShareType()));
         auto userGroupShare = qSharedPointerDynamicCast<UserGroupShare>(share);
         auto *s = new ShareUserLine(_account, userGroupShare, _maxSharingPermissions, _isFile, _parentScrollArea);
         connect(s, &ShareUserLine::resizeRequested, this, &ShareUserGroupWidget::slotAdjustScrollWidgetSize);
@@ -1031,6 +1031,12 @@ void ShareUserLine::showExpireDateOptions(bool show, const QDate &initialDate)
         _ui->calendar->setMinimumDate(QDate::currentDate().addDays(1));
         _ui->calendar->setDate(initialDate.isValid() ? initialDate : _ui->calendar->minimumDate());
         _ui->calendar->setFocus();
+
+        if (enforceExpirationDateForShare(_share->getShareType())) {
+            _ui->calendar->setMaximumDate(maxExpirationDateForShare(_share->getShareType(), _ui->calendar->maximumDate()));
+            _expirationDateLinkAction->setChecked(true);
+            _expirationDateLinkAction->setEnabled(false);
+        }
     }
 
     emit resizeRequested();
@@ -1072,6 +1078,35 @@ void ShareUserLine::disableProgessIndicatorAnimation()
     enableProgessIndicatorAnimation(false);
 }
 
+QDate ShareUserLine::maxExpirationDateForShare(const Share::ShareType type, const QDate &fallbackDate) const
+{
+    auto daysToExpire = 0;
+    if (type == Share::ShareType::TypeRemote) {
+        daysToExpire = _account->capabilities().shareRemoteExpireDateDays();
+    } else if (type == Share::ShareType::TypeEmail) {
+       daysToExpire = _account->capabilities().sharePublicLinkExpireDateDays();
+    } else {
+        daysToExpire = _account->capabilities().shareInternalExpireDateDays();
+    }
+
+    if (daysToExpire > 0) {
+        return QDate::currentDate().addDays(daysToExpire);
+    }
+
+    return fallbackDate;
+}
+
+bool ShareUserLine::enforceExpirationDateForShare(const Share::ShareType type) const
+{
+    if (type == Share::ShareType::TypeRemote) {
+        return _account->capabilities().shareRemoteEnforceExpireDate();
+    } else if (type == Share::ShareType::TypeEmail) {
+        return _account->capabilities().sharePublicLinkEnforceExpireDate();
+    } else {
+        return _account->capabilities().shareInternalEnforceExpireDate();
+    }
+}
+
 void ShareUserLine::setPasswordConfirmed()
 {
     if (_ui->lineEdit_password->text().isEmpty()) {
index 8715af22fb30da6ca8eaa2147226318d2693d99d..c63e56d74b0df1b4f85f1cf479af9c1a0e9807b8 100644 (file)
@@ -189,6 +189,9 @@ private:
   void enableProgessIndicatorAnimation(bool enable);
   void disableProgessIndicatorAnimation();
 
+  QDate maxExpirationDateForShare(const Share::ShareType type, const QDate &fallbackDate) const;
+  bool enforceExpirationDateForShare(const Share::ShareType type) const;
+
   Ui::ShareUserLine *_ui;
   AccountPtr _account;
   QSharedPointer<UserGroupShare> _share;
index 7eabe70dc3e6d12ff0e0e109a672eb8e9261ab12..d8fa1096a98d159bef50d1c0388bc7a3ab9ebb3e 100644 (file)
@@ -90,6 +90,26 @@ int Capabilities::sharePublicLinkExpireDateDays() const
     return _capabilities["files_sharing"].toMap()["public"].toMap()["expire_date"].toMap()["days"].toInt();
 }
 
+bool Capabilities::shareInternalEnforceExpireDate() const
+{
+    return _capabilities["files_sharing"].toMap()["public"].toMap()["expire_date_internal"].toMap()["enforced"].toBool();
+}
+
+int Capabilities::shareInternalExpireDateDays() const
+{
+    return _capabilities["files_sharing"].toMap()["public"].toMap()["expire_date_internal"].toMap()["days"].toInt();
+}
+
+bool Capabilities::shareRemoteEnforceExpireDate() const
+{
+    return _capabilities["files_sharing"].toMap()["public"].toMap()["expire_date_remote"].toMap()["enforced"].toBool();
+}
+
+int Capabilities::shareRemoteExpireDateDays() const
+{
+    return _capabilities["files_sharing"].toMap()["public"].toMap()["expire_date_remote"].toMap()["days"].toInt();
+}
+
 bool Capabilities::sharePublicLinkMultiple() const
 {
     return _capabilities["files_sharing"].toMap()["public"].toMap()["multiple"].toBool();
index 8e0c900abe149167542c8b948e3747717a0dfc0f..fb251e7e3a725c15427130d44ce82c2309274c0e 100644 (file)
@@ -55,6 +55,10 @@ public:
     bool sharePublicLinkEnforcePassword() const;
     bool sharePublicLinkEnforceExpireDate() const;
     int sharePublicLinkExpireDateDays() const;
+    bool shareInternalEnforceExpireDate() const;
+    int shareInternalExpireDateDays() const;
+    bool shareRemoteEnforceExpireDate() const;
+    int shareRemoteExpireDateDays() const;
     bool sharePublicLinkMultiple() const;
     bool shareResharing() const;
     int shareDefaultPermissions() const;