Add NextCloud user create functionality

 Add NextCloud user invite functionality
This commit is contained in:
Jan 2021-01-23 13:03:22 +01:00 committed by GitHub
commit 6448537d4c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 152 additions and 3 deletions

View file

@ -17,7 +17,10 @@ import {
* @returns {Promise<any>}
*/
export async function nextCloudApiRequest(this: IHookFunctions | IExecuteFunctions, method: string, endpoint: string, body: object | string | Buffer, headers?: object, encoding?: null | undefined, query?: object): Promise<any> { // tslint:disable-line:no-any
const options : OptionsWithUri = {
const resource = this.getNodeParameter('resource', 0);
const operation = this.getNodeParameter('operation', 0);
const options: OptionsWithUri = {
headers,
method,
body,
@ -46,6 +49,9 @@ export async function nextCloudApiRequest(this: IHookFunctions | IExecuteFunctio
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
if (resource === 'user' && operation === 'create') {
options.uri = options.uri.replace('/remote.php/webdav', '');
}
return await this.helpers.request(options);
} else {
const credentials = this.getCredentials('nextCloudOAuth2Api');
@ -55,6 +61,10 @@ export async function nextCloudApiRequest(this: IHookFunctions | IExecuteFunctio
options.uri = `${credentials.webDavUrl}/${encodeURI(endpoint)}`;
if (resource === 'user' && operation === 'create') {
options.uri = options.uri.replace('/remote.php/webdav', '');
}
return await this.helpers.requestOAuth2!.call(this, 'nextCloudOAuth2Api', options);
}
} catch (error) {

View file

@ -88,6 +88,10 @@ export class NextCloud implements INodeType {
name: 'Folder',
value: 'folder',
},
{
name: 'User',
value: 'user',
},
],
default: 'file',
description: 'The resource to operate on.',
@ -182,7 +186,27 @@ export class NextCloud implements INodeType {
description: 'The operation to perform.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'user',
],
},
},
options: [
{
name: 'Create',
value: 'create',
description: 'Invite a user to a NextCloud organization',
},
],
default: 'create',
description: 'The operation to perform.',
},
// ----------------------------------
// file
@ -478,6 +502,77 @@ export class NextCloud implements INodeType {
description: 'The path of which to list the content.',
},
// ----------------------------------
// user
// ----------------------------------
// ----------------------------------
// user:create
// ----------------------------------
{
displayName: 'Username',
name: 'userId',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'create',
],
},
},
placeholder: 'john',
description: 'Username the user will have.',
},
{
displayName: 'Email',
name: 'email',
type: 'string',
default: '',
required: true,
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'create',
],
},
},
placeholder: 'john@email.com',
description: 'The email of the user to invite.',
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
resource: [
'user',
],
operation: [
'create',
],
},
},
options: [
{
displayName: 'Display name',
name: 'displayName',
type: 'string',
default: '',
description: 'The display name of the user to invite.',
},
],
},
],
};
@ -598,6 +693,31 @@ export class NextCloud implements INodeType {
headers.Destination = `${credentials.webDavUrl}/${encodeURI(toPath)}`;
}
} else if (resource === 'user') {
if (operation === 'create') {
// ----------------------------------
// user:create
// ----------------------------------
requestMethod = 'POST';
endpoint = 'ocs/v1.php/cloud/users';
headers['OCS-APIRequest'] = true;
headers['Content-Type'] = 'application/x-www-form-urlencoded';
const userid = this.getNodeParameter('userId', i) as string;
const email = this.getNodeParameter('email', i) as string;
body = `userid=${userid}&email=${email}`;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.displayName) {
body += `&displayName=${additionalFields.displayName}`;
}
}
} else {
throw new Error(`The resource "${resource}" is not known!`);
}
@ -645,6 +765,25 @@ export class NextCloud implements INodeType {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i) as string;
items[i].binary![binaryPropertyName] = await this.helpers.prepareBinaryData(responseData, endpoint);
} else if (resource === 'user' && operation === 'create') {
const jsonResponseData: IDataObject = await new Promise((resolve, reject) => {
parseString(responseData, { explicitArray: false }, (err, data) => {
if (err) {
return reject(err);
}
if (data.ocs.meta.status !== 'ok') {
return reject(new Error(data.ocs.meta.message));
}
resolve(data.ocs.data as IDataObject);
});
});
returnData.push(jsonResponseData as IDataObject);
} else if (resource === 'folder' && operation === 'list') {
const jsonResponseData: IDataObject = await new Promise((resolve, reject) => {
@ -706,7 +845,7 @@ export class NextCloud implements INodeType {
if (resource === 'file' && operation === 'download') {
// For file downloads the files get attached to the existing items
return this.prepareOutputData(items);
} else {
} else {
// For all other ones does the output get replaced
return [this.helpers.returnJsonArray(returnData)];
}