Bläddra i källkod

Merge pull request #3946 from nextcloud/feature/implement-expiration-date-for-federated-shares

Implement expiration date for federated shares
allexzander 4 år sedan
förälder
incheckning
6ad63fb770

+ 3 - 1
src/gui/sharelinkwidget.cpp

@@ -516,7 +516,9 @@ void ShareLinkWidget::toggleExpireDateOptions(const bool enable)
     
     const auto date = enable ? _linkShare->getExpireDate() : QDate::currentDate().addDays(1);
     _ui->calendar->setDate(date);
-    _ui->calendar->setMinimumDate(date);
+    _ui->calendar->setMinimumDate(QDate::currentDate().addDays(1));
+    _ui->calendar->setMaximumDate(
+        QDate::currentDate().addDays(_account->capabilities().sharePublicLinkExpireDateDays()));
     _ui->calendar->setFocus();
     
     if (!enable && _linkShare && _linkShare->getExpireDate().isValid()) {

+ 8 - 2
src/gui/sharemanager.cpp

@@ -146,6 +146,12 @@ void Share::deleteShare()
     job->deleteShare(getId());
 }
 
+bool Share::isShareTypeUserGroupEmailRoomOrRemote(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::isShareTypeUserGroupEmailRoomOrRemote(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::isShareTypeUserGroupEmailRoomOrRemote(static_cast <Share::ShareType>(shareType))) {
             newShare = parseUserGroupShare(data);
         } else {
             newShare = parseShare(data);

+ 5 - 0
src/gui/sharemanager.h

@@ -130,6 +130,11 @@ public:
      */
     void deleteShare();
 
+     /*
+     * Is it a share with a user or group (local or remote)
+     */
+    static bool isShareTypeUserGroupEmailRoomOrRemote(const ShareType type);
+
 signals:
     void permissionsSet();
     void shareDeleted();

+ 36 - 1
src/gui/shareusergroupwidget.cpp

@@ -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::isShareTypeUserGroupEmailRoomOrRemote(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();
+    }
+
+    return _account->capabilities().shareInternalEnforceExpireDate();
+}
+
 void ShareUserLine::setPasswordConfirmed()
 {
     if (_ui->lineEdit_password->text().isEmpty()) {

+ 3 - 0
src/gui/shareusergroupwidget.h

@@ -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;

+ 20 - 0
src/libsync/capabilities.cpp

@@ -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();

+ 4 - 0
src/libsync/capabilities.h

@@ -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;