Merge pull request #8335 from owncloud/sharing_set_default_expire_date

[sharing] allow admin to set default expire date

* owncloud/sharing_set_default_expire_date:
  add setting to set default expire date
This commit is contained in:
Andreas Fischer 2014-04-24 14:03:41 +02:00
commit 522d101607
6 changed files with 139 additions and 3 deletions

View File

@ -199,4 +199,53 @@ class Helper extends \OC\Share\Constants {
$query->execute();
}
}
/**
* @brief get default expire settings defined by the admin
* @return array contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
*/
public static function getDefaultExpireSetting() {
$defaultExpireSettings = array('defaultExpireDateSet' => false);
// get default expire settings
$defaultExpireDate = \OC_Appconfig::getValue('core', 'shareapi_default_expire_date', 'no');
if ($defaultExpireDate === 'yes') {
$enforceExpireDate = \OC_Appconfig::getValue('core', 'shareapi_enforce_expire_date', 'no');
$defaultExpireSettings['defaultExpireDateSet'] = true;
$defaultExpireSettings['expireAfterDays'] = (int)\OC_Appconfig::getValue('core', 'shareapi_expire_after_n_days', '7');
$defaultExpireSettings['enforceExpireDate'] = $enforceExpireDate === 'yes' ? true : false;
}
return $defaultExpireSettings;
}
/**
* @brief calculate expire date
* @param array $defaultExpireSettings contains 'defaultExpireDateSet', 'enforceExpireDate', 'expireAfterDays'
* @param int $creationTime timestamp when the share was created
* @param int $userExpireDate expire timestamp set by the user
* @return mixed integer timestamp or False
*/
public static function calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate = null) {
$expires = false;
if (isset($defaultExpireSettings['defaultExpireDateSet']) && $defaultExpireSettings['defaultExpireDateSet']) {
$expires = $creationTime + $defaultExpireSettings['expireAfterDays'] * 86400;
}
if (isset($userExpireDate)) {
// if the admin decided to enforce the default expire date then we only take
// the user defined expire date of it is before the default expire date
if ($expires && isset($defaultExpireSettings['enforceExpireDate']) && $defaultExpireSettings['enforceExpireDate']) {
$expires = ($userExpireDate < $expires) ? $userExpireDate : $expires;
} else {
$expires = $userExpireDate;
}
}
return $expires;
}
}

View File

@ -844,9 +844,20 @@ class Share extends \OC\Share\Constants {
* @return bool True if item was expired, false otherwise.
*/
protected static function expireItem(array $item) {
// get default expire settings
$defaultSettings = Helper::getDefaultExpireSetting();
// calculate expire date
if (!empty($item['expiration'])) {
$now = new \DateTime();
$expires = new \DateTime($item['expiration']);
$userDefinedExpire = new \DateTime($item['expiration']);
$userDefinedExpireTimestamp = $userDefinedExpire->getTimestamp();
} else {
$userDefinedExpireTimestamp = null;
}
$expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $userDefinedExpireTimestamp);
if (is_int($expires)) {
$now = time();
if ($now > $expires) {
self::unshareItem($item);
return true;

View File

@ -45,6 +45,10 @@ $tmpl->assign('backgroundjobs_mode', OC_Appconfig::getValue('core', 'backgroundj
$tmpl->assign('cron_log', OC_Config::getValue('cron_log', true));
$tmpl->assign('lastcron', OC_Appconfig::getValue('core', 'lastcron', false));
$tmpl->assign('shareAPIEnabled', OC_Appconfig::getValue('core', 'shareapi_enabled', 'yes'));
$tmpl->assign('shareDefaultExpireDateSet', OC_Appconfig::getValue('core', 'shareapi_default_expire_date', 'no'));
$tmpl->assign('shareExpireAfterNDays', OC_Appconfig::getValue('core', 'shareapi_expire_after_n_days', '7'));
$tmpl->assign('shareEnforceExpireDate', OC_Appconfig::getValue('core', 'shareapi_enforce_expire_date', 'no'));
// Check if connected using HTTPS
if (OC_Request::serverProtocol() === 'https') {

View File

@ -132,6 +132,7 @@ table.grid td.date{
span.securitywarning {color:#C33; font-weight:bold; }
span.connectionwarning {color:#933; font-weight:bold; }
table.shareAPI td { padding-bottom: 0.8em; }
table.shareAPI input#shareapi_expire_after_n_days {width: 25px;}
#mail_settings p label:first-child {
display: inline-block;

View File

@ -254,6 +254,23 @@ if (!$_['internetconnectionworking']) {
<em><?php p($l->t('Allow users to send mail notification for shared files')); ?></em>
</td>
</tr>
<tr>
<td <?php if ($_['shareAPIEnabled'] == 'no') print_unescaped('class="hidden"');?>>
<input type="checkbox" name="shareapi_default_expire_date" id="shareapi_default_expire_date"
value="1" <?php if ($_['shareDefaultExpireDateSet'] == 'yes') print_unescaped('checked="checked"'); ?> />
<label for="shareapi_default_expire_date"><?php p($l->t('Set default expire date'));?></label><br/>
<?php p($l->t( 'Expire after ' )); ?>
<input type="text" name='shareapi_expire_after_n_days' id="shareapi_expire_after_n_days" placeholder="<?php p('7')?>"
value='<?php p($_['shareExpireAfterNDays']) ?>' />
<?php p($l->t( 'days' )); ?>
<input type="checkbox" name="shareapi_enforce_expire_date" id="shareapi_enforce_expire_date"
value="1" <?php if ($_['shareEnforceExpireDate'] == 'yes') print_unescaped('checked="checked"'); ?> />
<label for="shareapi_enforce_expire_date"><?php p($l->t('Enforce expire date'));?></label><br/>
<em><?php p($l->t('Expire shares by default after N days')); ?></em>
</td>
</tr>
</table>
</div>
@ -296,7 +313,7 @@ if (!$_['internetconnectionworking']) {
<p><?php p($l->t('This is used for sending out notifications.')); ?></p>
<p>
<label for="mail_smtpmode"><?php p($l->t( 'Send mode' )); ?></label>
<label for="mail_smtpmode"><?php p($l->t( 'Send mode' )); ?></label>
<select name='mail_smtpmode' id='mail_smtpmode'>
<?php foreach ($mail_smtpmode as $smtpmode):
$selected = '';

View File

@ -0,0 +1,54 @@
<?php
/**
* ownCloud
*
* @author Bjoern Schiessle
* @copyright 2014 Bjoern Schiessle <schiessle@owncloud.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
class Test_Share_Helper extends PHPUnit_Framework_TestCase {
public function expireDateProvider() {
return array(
// no default expire date, we take the users expire date
array(array('defaultExpireDateSet' => false), 2000000000, 2000010000, 2000010000),
// no default expire date and no user defined expire date, return false
array(array('defaultExpireDateSet' => false), 2000000000, null, false),
// unenforced expire data and no user defined expire date, take default expire date
array(array('defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => false), 2000000000, null, 2000086400),
// enforced expire date and no user defined expire date, take default expire date
array(array('defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => true), 2000000000, null, 2000086400),
// unenforced expire date and user defined date > default expire date, take users expire date
array(array('defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => false), 2000000000, 2000100000, 2000100000),
// unenforced expire date and user expire date < default expire date, take users expire date
array(array('defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => false), 2000000000, 2000010000, 2000010000),
// enforced expire date and user expire date < default expire date, take users expire date
array(array('defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => true), 2000000000, 2000010000, 2000010000),
// enforced expire date and users expire date > default expire date, take default expire date
array(array('defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => true), 2000000000, 2000100000, 2000086400),
);
}
/**
* @dataProvider expireDateProvider
*/
public function testCalculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate, $expected) {
$result = \OC\Share\Helper::calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate);
$this->assertSame($expected, $result);
}
}