Skip to content
This repository has been archived by the owner on Jun 5, 2024. It is now read-only.

Adjust share management to properly work with spaces #1013

Merged
merged 5 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions changelog/unreleased/enhancement-share-spaces
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Enhancement: Adjust share management to properly work with spaces

This includes a new method `shareSpaceWithUser` as well as the possibility to pass URL params to the `deleteShare` method.

https://github.com/owncloud/owncloud-sdk/pull/1013
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "owncloud-sdk",
"version": "2.0.1",
"version": "2.1.0-alpha.1",
"description": "ownCloud client library for JavaScript",
"keywords": [
"owncloud",
Expand Down
1 change: 1 addition & 0 deletions src/helperFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class helpers {
this.OCS_SHARE_TYPE_GROUP = 1
this.OCS_SHARE_TYPE_LINK = 3
this.OCS_SHARE_TYPE_REMOTE = 6
this.OCS_SHARE_TYPE_SPACE = 7

this.instance = null
this._authHeader = null
Expand Down
98 changes: 71 additions & 27 deletions src/shareManagement.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const ShareInfo = require('./shareInfo.js')
* <li>shareFileWithLink</li>
* <li>updateShare</li>
* <li>shareFileWithUser</li>
* <li>shareSpaceWithUser</li>
* <li>shareFileWithGroup</li>
* <li>getShares</li>
* <li>isShared</li>
Expand Down Expand Up @@ -90,28 +91,47 @@ class Shares {
shareFileWithUser (path, username, optionalParams) {
path = this.helpers._normalizePath(path)

const postData = {
let postData = {
shareType: this.helpers.OCS_SHARE_TYPE_USER,
shareWith: username,
path: path
}

if (optionalParams) {
if (optionalParams.permissions) {
postData.permissions = optionalParams.permissions
}
postData = { ...postData, ...this._getOptionalParams(optionalParams) }
}

if (optionalParams.expirationDate) {
postData.expireDate = optionalParams.expirationDate
}
return this.helpers._makeOCSrequest('POST', this.helpers.OCS_SERVICE_SHARE, 'shares', postData)
.then(data => {
const shareData = data.data.ocs.data
const share = new ShareInfo(shareData)

if (optionalParams.attributes) {
postData.attributes = optionalParams.attributes
}
return Promise.resolve(share)
})
}

if (optionalParams.remoteUser) {
postData.shareType = this.helpers.OCS_SHARE_TYPE_REMOTE
}
/**
* Shares a space with specified user
* @param {string} path path to the remote file share
* @param {string} username name of the user to share with
* @param {string} spaceId id of the space
* @param {object} optionalParams {permissions: integer, expirationDate: ISO Date, remoteUser: boolean, attributes: assoc array (at free disposal)}
* @returns {Promise.<ShareInfo>} instance of class ShareInfo
* @returns {Promise.<error>} string: error message, if any.
*/
shareSpaceWithUser (path, username, spaceId, optionalParams) {
let postData = {
shareType: this.helpers.OCS_SHARE_TYPE_SPACE,
shareWith: username,
space_ref: spaceId
}

if (path) {
postData.path = this.helpers._normalizePath(path)
JammingBen marked this conversation as resolved.
Show resolved Hide resolved
}

if (optionalParams) {
postData = { ...postData, ...this._getOptionalParams(optionalParams) }
}

return this.helpers._makeOCSrequest('POST', this.helpers.OCS_SERVICE_SHARE, 'shares', postData)
Expand All @@ -134,24 +154,14 @@ class Shares {
shareFileWithGroup (path, groupName, optionalParams) {
path = this.helpers._normalizePath(path)

const postData = {
let postData = {
shareType: this.helpers.OCS_SHARE_TYPE_GROUP,
shareWith: groupName,
path: path
}

if (optionalParams) {
if (optionalParams.permissions) {
postData.permissions = optionalParams.permissions
}

if (optionalParams.expirationDate) {
postData.expireDate = optionalParams.expirationDate
}

if (optionalParams.attributes) {
postData.attributes = optionalParams.attributes
}
postData = { ...postData, ...this._getOptionalParams(optionalParams) }
}

return this.helpers._makeOCSrequest('POST', this.helpers.OCS_SERVICE_SHARE, 'shares', postData)
Expand Down Expand Up @@ -371,14 +381,23 @@ class Shares {
/**
* Deletes a share
* @param {number} shareId ID of the share to delete
* @param {object} urlParams {shareWith: string}
* @returns {Promise.<status>} boolean: true if successful
* @returns {Promise.<error>} string: error message, if any.
*/
deleteShare (shareId) {
deleteShare (shareId, urlParams) {
let urlParamString = ''
if (urlParams) {
urlParamString = '?'
if (urlParams.shareWith) {
urlParamString += `shareWith=${urlParams.shareWith}`
}
}

return new Promise((resolve, reject) => {
/* jshint unused: false */
this.helpers._makeOCSrequest('DELETE', this.helpers.OCS_SERVICE_SHARE,
'shares/' + encodeURIComponent(shareId.toString())
'shares/' + encodeURIComponent(shareId.toString()) + urlParamString
).then(() => {
resolve(true)
}).catch(error => {
Expand Down Expand Up @@ -420,6 +439,31 @@ class Shares {
return json.ocs.data
})
}

/**
* @param {object} optionalParams
* @returns {object}
*/
_getOptionalParams (optionalParams) {
const data = {}
if (optionalParams.permissions) {
data.permissions = optionalParams.permissions
}

if (optionalParams.expirationDate) {
data.expireDate = optionalParams.expirationDate
}

if (optionalParams.attributes) {
data.attributes = optionalParams.attributes
}

if (optionalParams.remoteUser) {
data.shareType = this.helpers.OCS_SHARE_TYPE_REMOTE
}

return data
}
}

module.exports = Shares