set expire date for all share types

Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
This commit is contained in:
Bjoern Schiessle 2017-03-29 16:50:23 +02:00
parent 3cc0d15f92
commit b84fd7c361
No known key found for this signature in database
GPG Key ID: 2378A753E2BF04F6
7 changed files with 124 additions and 57 deletions

View File

@ -163,6 +163,11 @@ class ShareAPIController extends OCSController {
$result['file_parent'] = $node->getParent()->getId();
$result['file_target'] = $share->getTarget();
$expiration = $share->getExpirationDate();
if ($expiration !== null) {
$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
}
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
$sharedWith = $this->userManager->get($share->getSharedWith());
$result['share_with'] = $share->getSharedWith();
@ -179,11 +184,6 @@ class ShareAPIController extends OCSController {
$result['token'] = $share->getToken();
$result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
$expiration = $share->getExpirationDate();
if ($expiration !== null) {
$result['expiration'] = $expiration->format('Y-m-d 00:00:00');
}
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) {
$result['share_with'] = $share->getSharedWith();
$result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD');
@ -741,32 +741,30 @@ class ShareAPIController extends OCSController {
}
} else {
// For other shares only permissions is valid.
if ($share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL && $permissions === null) {
throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
} elseif ($permissions !== null) {
if ($permissions !== null) {
$permissions = (int)$permissions;
$share->setPermissions($permissions);
}
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) {
if ($expireDate === '') {
$share->setExpirationDate(null);
} else if ($expireDate !== null) {
try {
$expireDate = $this->parseDate($expireDate);
} catch (\Exception $e) {
throw new OCSBadRequestException($e->getMessage());
}
$share->setExpirationDate($expireDate);
}
if ($password === '') {
$share->setPassword(null);
} else if ($password !== null) {
$share->setPassword($password);
}
}
if ($expireDate === '') {
$share->setExpirationDate(null);
} else if ($expireDate !== null) {
try {
$expireDate = $this->parseDate($expireDate);
} catch (\Exception $e) {
throw new OCSBadRequestException($e->getMessage());
}
$share->setExpirationDate($expireDate);
}
}
if ($permissions !== null && $share->getShareOwner() !== $this->currentUser) {

View File

@ -24,6 +24,7 @@ namespace OCA\ShareByMail;
use OC\HintException;
use OC\Share20\Exception\InvalidShare;
use OCP\Activity\IManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Folder;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
@ -440,6 +441,7 @@ class ShareByMailProvider implements IShareProvider {
->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
->set('password', $qb->createNamedParameter($share->getPassword()))
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
->execute();
return $share;
@ -699,6 +701,13 @@ class ShareByMailProvider implements IShareProvider {
$share->setShareOwner($owner->getUID());
}
if ($data['expiration'] !== null) {
$expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
if ($expiration !== false) {
$share->setExpirationDate($expiration);
}
}
$share->setNodeId((int)$data['file_source']);
$share->setNodeType($data['item_type']);

View File

@ -194,3 +194,6 @@ a {
width: inherit !important;
}
.ui-datepicker {
z-index: 1111 !important;
}

View File

@ -105,9 +105,9 @@
'<span class="shareOption menuitem">' +
'<input id="expireDate-{{cid}}-{{shareId}}" type="checkbox" name="expirationDate" class="expireDate checkbox" {{#if hasExpireDate}}checked="checked"{{/if}}" />' +
'<label for="expireDate-{{cid}}-{{shareId}}">{{expireDateLabel}}</label>' +
'<div class="expirationDateContainer-{{cid}}-{{shareId}} {{#unless isExpirationSet}}hidden{{/unless}}">' +
'<div class="expirationDateContainer-{{cid}}-{{shareId}} {{#unless hasExpireDate}}hidden{{/unless}}">' +
' <label for="expirationDatePicker-{{cid}}-{{shareId}}" class="hidden-visually" value="{{expirationDate}}">{{expirationLabel}}</label>' +
' <input id="expirationDatePicker-{{cid}}-{{shareId}}" class="datepicker" type="text" placeholder="{{expirationDatePlaceholder}}" value="{{expirationValue}}" />' +
' <input id="expirationDatePicker-{{cid}}-{{shareId}}" class="datepicker" type="text" placeholder="{{expirationDatePlaceholder}}" value="{{expireDate}}" />' +
'</div>' +
'</span>' +
'</li>' +
@ -166,7 +166,9 @@
'click .password' : 'onMailSharePasswordProtectChange',
'click .secureDrop' : 'onSecureDropChange',
'keyup input.passwordField': 'onMailSharePasswordKeyUp',
'focusout input.passwordField': 'onMailSharePasswordEntered'
'focusout input.passwordField': 'onMailSharePasswordEntered',
'change .datepicker': 'onChangeExpirationDate',
'click .datepicker' : 'showDatePicker'
},
initialize: function(options) {
@ -237,6 +239,8 @@
isFileSharedByMail: shareType === OC.Share.SHARE_TYPE_EMAIL && !this.model.isFolder(),
isPasswordSet: hasPassword,
secureDropMode: !this.model.hasReadPermission(shareIndex),
hasExpireDate: this.model.getExpireDate(shareIndex) !== null,
expireDate: this.model.getExpireDate(shareIndex),
passwordPlaceholder: hasPassword ? PASSWORD_PLACEHOLDER : PASSWORD_PLACEHOLDER_MESSAGE,
});
},
@ -464,19 +468,35 @@
var state = element.prop('checked');
datePicker.toggleClass('hidden', !state);
if (!state) {
// discard expiration date
this.model.get('linkShare').expiration = '';
/*
this.model.saveLinkShare({
expireDate: ''
});
*/
this.setExpirationDate(shareId, '');
} else {
var expirationDatePicker = '#expirationDatePicker-' + this.cid + '-' + shareId;
this.$(expirationDatePicker).focus();
this.showDatePicker(event);
}
},
showDatePicker: function(event) {
var element = $(event.target);
var li = element.closest('li[data-share-id]');
var shareId = li.data('share-id');
var expirationDatePicker = '#expirationDatePicker-' + this.cid + '-' + shareId;
$(expirationDatePicker).datepicker({dateFormat : 'dd-mm-yy'});
$(expirationDatePicker).focus();
},
onChangeExpirationDate: function(event) {
datePicker = $(event.target);
expireDate = datePicker.val();
var element = $(event.target);
var li = element.closest('li[data-share-id]');
var shareId = li.data('share-id');
this.setExpirationDate(shareId, expireDate);
},
setExpirationDate: function(shareId, expireDate) {
this.model.updateShare(shareId, {expireDate: expireDate}, {});
},
onMailSharePasswordProtectChange: function(event) {
var element = $(event.target);
var li = element.closest('li[data-share-id]');

View File

@ -363,6 +363,10 @@
return this.get('reshare').share_type;
},
getExpireDate: function(shareIndex) {
return this._shareExpireDate(shareIndex);
},
/**
* Returns all share entries that only apply to the current item
* (file/folder)
@ -449,6 +453,16 @@
return (share.permissions & permission) === permission;
},
_shareExpireDate: function(shareIndex) {
var share = this.get('shares')[shareIndex];
if(!_.isObject(share)) {
throw "Unknown Share";
}
var date2 = share.expiration;
return date2;
},
/**
* @returns {boolean}
*/

View File

@ -202,6 +202,7 @@ class DefaultShareProvider implements IShareProvider {
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
->execute();
} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
$qb = $this->dbConn->getQueryBuilder();
@ -212,6 +213,7 @@ class DefaultShareProvider implements IShareProvider {
->set('permissions', $qb->createNamedParameter($share->getPermissions()))
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
->execute();
/*
@ -224,6 +226,7 @@ class DefaultShareProvider implements IShareProvider {
->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
->execute();
/*

View File

@ -938,22 +938,11 @@ class Manager implements IManager {
*/
if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
$shares2 = [];
$today = new \DateTime();
while(true) {
$added = 0;
foreach ($shares as $share) {
// Check if the share is expired and if so delete it
if ($share->getExpirationDate() !== null &&
$share->getExpirationDate() <= $today
) {
try {
$this->deleteShare($share);
} catch (NotFoundException $e) {
//Ignore since this basically means the share is deleted
}
continue;
}
$added++;
$shares2[] = $share;
@ -985,6 +974,22 @@ class Manager implements IManager {
$shares = $shares2;
}
// remove all shares which are already expired
foreach ($shares as $key => $share) {
try {
$this->checkExpireDate($share);
} catch (ShareNotFound $e) {
unset($shares[$key]);
try {
$this->deleteShare($share);
} catch (NotFoundException $e) {
//Ignore since this basically means the share is deleted
}
}
}
return $shares;
}
@ -998,7 +1003,23 @@ class Manager implements IManager {
return [];
}
return $provider->getSharedWith($userId, $shareType, $node, $limit, $offset);
$shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset);
// remove all shares which are already expired
foreach ($shares as $key => $share) {
try {
$this->checkExpireDate($share);
} catch (ShareNotFound $e) {
unset($shares[$key]);
try {
$this->deleteShare($share);
} catch (NotFoundException $e) {
//Ignore since this basically means the share is deleted
}
}
}
return $shares;
}
/**
@ -1019,13 +1040,7 @@ class Manager implements IManager {
$share = $provider->getShareById($id, $recipient);
// Validate link shares expiration date
if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK &&
$share->getExpirationDate() !== null &&
$share->getExpirationDate() <= new \DateTime()) {
$this->deleteShare($share);
throw new ShareNotFound();
}
$this->checkExpireDate($share);
return $share;
}
@ -1087,11 +1102,7 @@ class Manager implements IManager {
throw new ShareNotFound();
}
if ($share->getExpirationDate() !== null &&
$share->getExpirationDate() <= new \DateTime()) {
$this->deleteShare($share);
throw new ShareNotFound();
}
$this->checkExpireDate($share);
/*
* Reduce the permissions for link shares if public upload is not enabled
@ -1104,6 +1115,15 @@ class Manager implements IManager {
return $share;
}
protected function checkExpireDate($share) {
if ($share->getExpirationDate() !== null &&
$share->getExpirationDate() <= new \DateTime()) {
$this->deleteShare($share);
throw new ShareNotFound();
}
}
/**
* Verify the password of a public share
*