Skip to content

Commit

Permalink
Implement quick action for creating public links
Browse files Browse the repository at this point in the history
  • Loading branch information
JammingBen committed Aug 24, 2021
1 parent 1dd6596 commit 8f182d9
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 25 deletions.
123 changes: 123 additions & 0 deletions apps/files_sharing/js/share.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
PROPERTY_OWNER_DISPLAY_NAME: '{' + OC.Files.Client.NS_OWNCLOUD + '}owner-display-name'
});

var TEMPLATE_PUBLIC_LINK_ACTION =
'<span class="action action-create-public-link permanent" title="{{tooltip}}">' +
'<span class="icon icon-public" />' +
'</span>';

if (!OCA.Sharing) {
OCA.Sharing = {};
}
Expand All @@ -38,6 +43,7 @@
if (fileList.id === 'trashbin' || fileList.id === 'files.public') {
return;
}
var self = this;
var fileActions = fileList.fileActions;
var oldCreateRow = fileList._createRow;
fileList._createRow = function(fileData) {
Expand Down Expand Up @@ -206,6 +212,37 @@
fileList._setShareTreeUserGroupView();
});
fileList.registerTabView(shareTab);

var shareConfig = new OC.Share.ShareConfigModel();

var showPublicLinkQuickAction = OC.appConfig.files_sharing.showPublicLinkQuickAction
&& shareConfig.isShareWithLinkAllowed()
&& !shareConfig.isPublicSharingBlockedByAllowlist()
&& !shareConfig.defaults.enforceLinkPasswordReadOnly;

if (showPublicLinkQuickAction) {
fileActions.registerAction({
name: 'public-link-action',
displayName: t('core', 'Create public link'),
mime: 'all',
permissions: OC.PERMISSION_READ,
type: OCA.Files.FileActions.TYPE_INLINE,
render: function(actionSpec, isDefault, context) {
var permissions = parseInt(context.$file.attr('data-permissions'), 10);

if ((permissions & OC.PERMISSION_SHARE) !== 0) {
var $actionLink = $(self.renderPublicLinkAction());
context.$file.find('a.name>span.fileactions').append($actionLink);
return $actionLink;
}

return null;
},
actionHandler: function(fileName, context) {
return self._handlePublicLinkQuickAction(context.fileInfoModel, shareTab, shareConfig);
}
});
}
},

/**
Expand Down Expand Up @@ -269,6 +306,92 @@
text += ', +' + (count - maxRecipients);
}
return text;
},

renderPublicLinkAction: function () {
if (!this._template) {
this._template = Handlebars.compile(TEMPLATE_PUBLIC_LINK_ACTION);
}
return this._template({
'tooltip': t('files_sharing', 'Create and copy public link')
});
},

_handlePublicLinkQuickAction: function(fileInfoModel, shareTab, shareConfig) {
var self = this;

var attributes = {
permissions: OC.PERMISSION_READ,
expireDate: shareConfig.getDefaultExpirationDateString(),
shareType: OC.Share.SHARE_TYPE_LINK,
itemType: fileInfoModel.get('id'),
itemSource: fileInfoModel.get('type'),
};

var shareModel = new OC.Share.ShareModel(attributes);

var shareItemModel = new OC.Share.ShareItemModel({
itemType: fileInfoModel.isDirectory() ? 'folder' : 'file',
itemSource: fileInfoModel.get('id'),
possiblePermissions: fileInfoModel.get('sharePermissions')
}, {
configModel: shareConfig,
fileInfoModel: fileInfoModel
});

shareItemModel.fetch().then(function() {
var shareDialog = shareTab._dialog;
var linkShareView;

if (shareDialog && shareDialog.linkShareView) {
linkShareView = shareDialog.linkShareView;
} else {
var linkCollection = shareItemModel.getLinkSharesCollection();
linkShareView = new OC.Share.ShareDialogLinkListView({
collection: linkCollection,
itemModel: shareItemModel
});
}

attributes.name = linkShareView._generateName();
attributes.path = fileInfoModel.get('path') + fileInfoModel.get('name');

shareModel.save(attributes, {
attrs: attributes,
success: function(newFileInfoModel) {
linkShareView.collection.add(shareModel);
fileInfoModel.set({
'shareTypes': [newFileInfoModel.get('shareType')]
});

var link = shareModel.getLink();
self._copyToClipboard(link);

OC.Notification.show(t(
'files_sharing',
'A new public link with download & view permissions has been created and copied to the clipboard.',
));
},
error: function () {
OC.Notification.show(t(
'files_sharing',
'The public link could not be created. Please contact the administrator for help.'),
{ type: 'error' }
);
}
});
});
},

_copyToClipboard: function(text) {
// Clipboard.js apparently does not work for file actions, so we have
// to do it the "old" way.
var dummy = document.createElement("textarea");
document.body.appendChild(dummy);
dummy.value = text;
dummy.select();
document.execCommand("copy");
document.body.removeChild(dummy);
}
};
})();
Expand Down
27 changes: 2 additions & 25 deletions apps/files_sharing/js/sharedialogview.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@
});

var baseLinkShareViewRender = view.linkShareView.render;

view.render = function () {
baseRenderCall.call(view);
if (that.isPublicSharingBlockedByAllowlist() && !shareCollectionModel.length) {
if (view.configModel.isPublicSharingBlockedByAllowlist() && !shareCollectionModel.length) {
view.$el.find('.subtab-publicshare').remove();
}
};

view.linkShareView.render = function () {
baseLinkShareViewRender.call(view.linkShareView);

if (that.isPublicSharingBlockedByAllowlist()) {
if (view.configModel.isPublicSharingBlockedByAllowlist()) {
view.linkShareView.$el.find('.addLink').remove();
view.$el.find('.empty-message').remove();
}
Expand All @@ -74,28 +73,6 @@

};
},

isPublicSharingBlockedByAllowlist: function () {
var allowlistEnabled = oc_appconfig.files_sharing.publicShareSharersGroupsAllowlistEnabled;
var allowlistGroups = oc_appconfig.files_sharing.publicShareSharersGroupsAllowlist;


if (allowlistEnabled === true &&
allowlistGroups.length
) {
var userGroups = OC.getCurrentUser().groups;

for (var i = 0; i < userGroups.length; i++) {
if (allowlistGroups.indexOf(userGroups[i]) >= 0) {
return false;
}
}

return true;
}

return false;
}
};
})();

Expand Down
1 change: 1 addition & 0 deletions apps/files_sharing/lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public static function extendJsConfig($array) {
$array['array']['oc_appconfig']['files_sharing'] = [
'publicShareSharersGroupsAllowlist' => $sharingAllowlist->getPublicShareSharersGroupsAllowlist(),
'publicShareSharersGroupsAllowlistEnabled' => $sharingAllowlist->isPublicShareSharersGroupsAllowlistEnabled(),
'showPublicLinkQuickAction' => \OC::$server->getConfig()->getSystemValue('sharing.showPublicLinkQuickAction', false),
];

return $array;
Expand Down
10 changes: 10 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,16 @@
*/
'sharing.federation.allowHttpFallback' => false,

/**
* Show a quick action for the public link creation
* Set this to true to display a quick action for creating public links
* in the filelist. A public link created this way will be read-only per default.
*
* Note: if enforced password protection for read-only links is enabled, the
* quick action will not be displayed!
*/
'sharing.showPublicLinkQuickAction' => false,

/**
* All other configuration options
*/
Expand Down
19 changes: 19 additions & 0 deletions core/js/shareconfigmodel.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@

return defaultExpireDateRemote;
},

/**
* @returns {boolean}
*/
isPublicSharingBlockedByAllowlist: function () {
var allowlistEnabled = oc_appconfig.files_sharing.publicShareSharersGroupsAllowlistEnabled;
var allowlistGroups = oc_appconfig.files_sharing.publicShareSharersGroupsAllowlist;

if (allowlistEnabled === true && allowlistGroups.length) {
var userGroups = OC.getCurrentUser().groups;
for (var i = 0; i < userGroups.length; i++) {
if (allowlistGroups.indexOf(userGroups[i]) >= 0) {
return false;
}
}
return true;
}
return false;
}
});


Expand Down

0 comments on commit 8f182d9

Please sign in to comment.