Skip to content

Commit

Permalink
Merge pull request #17725 from nextcloud/enh/share_exp_internal
Browse files Browse the repository at this point in the history
Allow internal shares to have a default expiration date
  • Loading branch information
rullzer authored Nov 28, 2019
2 parents 669302e + 078f4ef commit 62dc320
Show file tree
Hide file tree
Showing 15 changed files with 318 additions and 39 deletions.
8 changes: 4 additions & 4 deletions apps/files_sharing/js/dist/files_sharing_tab.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion apps/files_sharing/js/dist/files_sharing_tab.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions apps/files_sharing/lib/Capabilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ public function getCapabilities() {
$public['expire_date']['enforced'] = $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes';
}

$public['expire_date_internal'] = [];
$public['expire_date_internal']['enabled'] = $this->config->getAppValue('core', 'shareapi_default_internal_expire_date', 'no') === 'yes';
if ($public['expire_date_internal']['enabled']) {
$public['expire_date_internal']['days'] = $this->config->getAppValue('core', 'shareapi_internal_expire_after_n_days', '7');
$public['expire_date_internal']['enforced'] = $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no') === 'yes';
}

$public['send_mail'] = $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no') === 'yes';
$public['upload'] = $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes';
$public['upload_files_drop'] = $public['upload'];
Expand Down
22 changes: 22 additions & 0 deletions apps/files_sharing/src/components/SharingEntry.vue
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,28 @@ export default {
set: function(checked) {
this.updatePermissions(this.canEdit, checked)
}
},
/**
* Does the current share have an expiration date
* @returns {boolean}
*/
hasExpirationDate: {
get: function() {
return this.config.isDefaultInternalExpireDateEnforced || !!this.share.expireDate
},
set: function(enabled) {
this.share.expireDate = enabled
? this.config.defaultInternalExpirationDateString !== ''
? this.config.defaultInternalExpirationDateString
: moment().format('YYYY-MM-DD')
: ''
}
},
dateMaxEnforced() {
return this.config.isDefaultInternalExpireDateEnforced
&& moment().add(1 + this.config.defaultInternalExpireDate, 'days')
}
},
Expand Down
22 changes: 22 additions & 0 deletions apps/files_sharing/src/components/SharingEntryLink.vue
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,28 @@ export default {
return t('files_sharing', 'Share link')
},
/**
* Does the current share have an expiration date
* @returns {boolean}
*/
hasExpirationDate: {
get: function() {
return this.config.isDefaultExpireDateEnforced || !!this.share.expireDate
},
set: function(enabled) {
this.share.expireDate = enabled
? this.config.defaultExpirationDateString !== ''
? this.config.defaultExpirationDateString
: moment().format('YYYY-MM-DD')
: ''
}
},
dateMaxEnforced() {
return this.config.isDefaultExpireDateEnforced
&& moment().add(1 + this.config.defaultExpireDate, 'days')
},
/**
* Is the current share password protected ?
* @returns {boolean}
Expand Down
22 changes: 0 additions & 22 deletions apps/files_sharing/src/mixins/SharesMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,6 @@ export default {

computed: {

/**
* Does the current share have an expiration date
* @returns {boolean}
*/
hasExpirationDate: {
get: function() {
return this.config.isDefaultExpireDateEnforced || !!this.share.expireDate
},
set: function(enabled) {
this.share.expireDate = enabled
? this.config.defaultExpirationDateString !== ''
? this.config.defaultExpirationDateString
: moment().format('YYYY-MM-DD')
: ''
}
},

/**
* Does the current share have a note
* @returns {boolean}
Expand All @@ -118,11 +101,6 @@ export default {
return moment().add(1, 'days')
},

dateMaxEnforced() {
return this.config.isDefaultExpireDateEnforced
&& moment().add(1 + this.config.defaultExpireDate, 'days')
},

/**
* Datepicker lang values
* https://github.com/nextcloud/nextcloud-vue/pull/146
Expand Down
55 changes: 53 additions & 2 deletions apps/files_sharing/src/services/ConfigService.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class Config {
}

/**
* Get the default expiration date as string
* Get the default link share expiration date as string
*
* @returns {string}
* @readonly
Expand All @@ -75,6 +75,24 @@ export default class Config {
return expireDateString
}

/**
* Get the default internal expiration date as string
*
* @returns {string}
* @readonly
* @memberof Config
*/
get defaultInternalExpirationDateString() {
let expireDateString = ''
if (this.isDefaultInternalExpireDateEnabled) {
const date = window.moment.utc()
const expireAfterDays = this.defaultInternalExpireDate
date.add(expireAfterDays, 'days')
expireDateString = date.format('YYYY-MM-DD')
}
return expireDateString
}

/**
* Are link shares password-enforced ?
*
Expand Down Expand Up @@ -119,6 +137,28 @@ export default class Config {
return OC.appConfig.core.defaultExpireDateEnabled === true
}

/**
* Is internal shares expiration enforced ?
*
* @returns {boolean}
* @readonly
* @memberof Config
*/
get isDefaultInternalExpireDateEnforced() {
return OC.appConfig.core.defaultInternalExpireDateEnforced === true
}

/**
* Is there a default expiration date for new internal shares ?
*
* @returns {boolean}
* @readonly
* @memberof Config
*/
get isDefaultInternalExpireDateEnabled() {
return OC.appConfig.core.defaultInternalExpireDateEnabled === true
}

/**
* Are users on this server allowed to send shares to other servers ?
*
Expand All @@ -142,7 +182,7 @@ export default class Config {
}

/**
* Get the default days to expiration
* Get the default days to link shares expiration
*
* @returns {int}
* @readonly
Expand All @@ -152,6 +192,17 @@ export default class Config {
return OC.appConfig.core.defaultExpireDate
}

/**
* Get the default days to internal shares expiration
*
* @returns {int}
* @readonly
* @memberof Config
*/
get defaultInternalExpireDate() {
return OC.appConfig.core.defaultInternalExpireDate
}

/**
* Is resharing allowed ?
*
Expand Down
4 changes: 4 additions & 0 deletions apps/settings/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ $(document).ready(function(){
$("#setDefaultExpireDate").toggleClass('hidden', !this.checked);
});

$('#shareapiDefaultInternalExpireDate').change(function() {
$("#setDefaultInternalExpireDate").toggleClass('hidden', !this.checked);
});

$('#publicShareDisclaimer').change(function() {
$("#publicShareDisclaimerText").toggleClass('hidden', !this.checked);
if(!this.checked) {
Expand Down
3 changes: 3 additions & 0 deletions apps/settings/lib/Settings/Admin/Sharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ public function getForm() {
'enableLinkPasswordByDefault' => $this->config->getAppValue('core', 'shareapi_enable_link_password_by_default', 'no'),
'shareApiDefaultPermissions' => $this->config->getAppValue('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL),
'shareApiDefaultPermissionsCheckboxes' => $this->getSharePermissionList(),
'shareDefaultInternalExpireDateSet' => $this->config->getAppValue('core', 'shareapi_default_internal_expire_date', 'no'),
'shareInternalExpireAfterNDays' => $this->config->getAppValue('core', 'shareapi_internal_expire_after_n_days', '7'),
'shareInternalEnforceExpireDate' => $this->config->getAppValue('core', 'shareapi_enforce_internal_expire_date', 'no'),
];

return new TemplateResponse('settings', 'settings/admin/sharing', $parameters, '');
Expand Down
18 changes: 17 additions & 1 deletion apps/settings/templates/settings/admin/sharing.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@
value="1" <?php if ($_['shareAPIEnabled'] === 'yes') print_unescaped('checked="checked"'); ?> />
<label for="shareAPIEnabled"><?php p($l->t('Allow apps to use the Share API'));?></label><br/>
</p>

<p id="internalShareSettings" class="indent <?php if ($_['shareAPIEnabled'] === 'no') p('hidden'); ?>">
<input type="checkbox" name="shareapi_default_internal_expire_date" id="shareapiDefaultInternalExpireDate" class="checkbox"
value="1" <?php if ($_['shareDefaultInternalExpireDateSet'] === 'yes') print_unescaped('checked="checked"'); ?> />
<label for="shareapiDefaultInternalExpireDate"><?php p($l->t('Set default expiration date for non-link shares'));?></label><br/>
</p>
<p id="setDefaultInternalExpireDate" class="double-indent <?php if ($_['shareDefaultInternalExpireDateSet'] === 'no' || $_['shareAPIEnabled'] === 'no') p('hidden');?>">
<?php p($l->t( 'Expire after ' )); ?>
<input type="text" name='shareapi_internal_expire_after_n_days' id="shareapiInternalExpireAfterNDays" placeholder="<?php p('7')?>"
value='<?php p($_['shareInternalExpireAfterNDays']) ?>' />
<?php p($l->t( 'days' )); ?>
<input type="checkbox" name="shareapi_internal_enforce_expire_date" id="shareapiInternalEnforceExpireDate" class="checkbox"
value="1" <?php if ($_['shareInternalEnforceExpireDate'] === 'yes') print_unescaped('checked="checked"'); ?> />
<label for="shareapiInternalEnforceExpireDate"><?php p($l->t('Enforce expiration date'));?></label><br/>
</p>

<p class="<?php if ($_['shareAPIEnabled'] === 'no') p('hidden');?>">
<input type="checkbox" name="shareapi_allow_links" id="allowLinks" class="checkbox"
value="1" <?php if ($_['allowLinks'] === 'yes') print_unescaped('checked="checked"'); ?> />
Expand All @@ -56,7 +72,7 @@

<input type="checkbox" name="shareapi_default_expire_date" id="shareapiDefaultExpireDate" class="checkbox"
value="1" <?php if ($_['shareDefaultExpireDateSet'] === 'yes') print_unescaped('checked="checked"'); ?> />
<label for="shareapiDefaultExpireDate"><?php p($l->t('Set default expiration date'));?></label><br/>
<label for="shareapiDefaultExpireDate"><?php p($l->t('Set default expiration date for link shares'));?></label><br/>

</p>
<p id="setDefaultExpireDate" class="double-indent <?php if ($_['allowLinks'] !== 'yes' || $_['shareDefaultExpireDateSet'] === 'no' || $_['shareAPIEnabled'] === 'no') p('hidden');?>">
Expand Down
40 changes: 38 additions & 2 deletions apps/settings/tests/Settings/Admin/SharingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ public function testGetFormWithoutExcludedGroups() {
->method('getAppValue')
->with('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL)
->willReturn(Constants::PERMISSION_ALL);
$this->config
->expects($this->at(14))
->method('getAppValue')
->with('core', 'shareapi_default_internal_expire_date', 'no')
->willReturn('no');
$this->config
->expects($this->at(15))
->method('getAppValue')
->with('core', 'shareapi_internal_expire_after_n_days', '7')
->willReturn('7');
$this->config
->expects($this->at(16))
->method('getAppValue')
->with('core', 'shareapi_enforce_internal_expire_date', 'no')
->willReturn('no');

$expected = new TemplateResponse(
'settings',
Expand All @@ -152,7 +167,10 @@ public function testGetFormWithoutExcludedGroups() {
'publicShareDisclaimerText' => 'Lorem ipsum',
'enableLinkPasswordByDefault' => 'yes',
'shareApiDefaultPermissions' => Constants::PERMISSION_ALL,
'shareApiDefaultPermissionsCheckboxes' => $this->invokePrivate($this->admin, 'getSharePermissionList', [])
'shareApiDefaultPermissionsCheckboxes' => $this->invokePrivate($this->admin, 'getSharePermissionList', []),
'shareDefaultInternalExpireDateSet' => 'no',
'shareInternalExpireAfterNDays' => '7',
'shareInternalEnforceExpireDate' => 'no',
],
''
);
Expand Down Expand Up @@ -231,6 +249,21 @@ public function testGetFormWithExcludedGroups() {
->method('getAppValue')
->with('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL)
->willReturn(Constants::PERMISSION_ALL);
$this->config
->expects($this->at(14))
->method('getAppValue')
->with('core', 'shareapi_default_internal_expire_date', 'no')
->willReturn('no');
$this->config
->expects($this->at(15))
->method('getAppValue')
->with('core', 'shareapi_internal_expire_after_n_days', '7')
->willReturn('7');
$this->config
->expects($this->at(16))
->method('getAppValue')
->with('core', 'shareapi_enforce_internal_expire_date', 'no')
->willReturn('no');


$expected = new TemplateResponse(
Expand All @@ -253,7 +286,10 @@ public function testGetFormWithExcludedGroups() {
'publicShareDisclaimerText' => 'Lorem ipsum',
'enableLinkPasswordByDefault' => 'yes',
'shareApiDefaultPermissions' => Constants::PERMISSION_ALL,
'shareApiDefaultPermissionsCheckboxes' => $this->invokePrivate($this->admin, 'getSharePermissionList', [])
'shareApiDefaultPermissionsCheckboxes' => $this->invokePrivate($this->admin, 'getSharePermissionList', []),
'shareDefaultInternalExpireDateSet' => 'no',
'shareInternalExpireAfterNDays' => '7',
'shareInternalEnforceExpireDate' => 'no',
],
''
);
Expand Down
10 changes: 10 additions & 0 deletions lib/private/Share20/DefaultShareProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,19 @@ public function create(\OCP\Share\IShare $share) {
//Set the UID of the user we share with
$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
$qb->setValue('accepted', $qb->createNamedParameter(IShare::STATUS_PENDING));

//If an expiration date is set store it
if ($share->getExpirationDate() !== null) {
$qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
}
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
//Set the GID of the group we share with
$qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));

//If an expiration date is set store it
if ($share->getExpirationDate() !== null) {
$qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
}
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
//set label for public link
$qb->setValue('label', $qb->createNamedParameter($share->getLabel()));
Expand Down
Loading

0 comments on commit 62dc320

Please sign in to comment.