Commit b2f224b8 authored by Boiget Boiget's avatar Boiget Boiget

start error management for Nextcloud

parent c1f1dc86
Pipeline #2318 failed with stages
in 2 minutes and 18 seconds
......@@ -49,14 +49,19 @@ class NextcloudClient {
},
)
.then((response) => {
if (response.statusText === 'OK') {
const infos = response.data.ocs.meta;
console.log(response.data.ocs.meta);
if (infos.status === 'ok') {
console.log(`Nextcloud: group ${groupName} added`);
} else {
console.log(`Nextcloud: ERROR adding group ${groupName} (${infos.statuscode} - ${infos.message})`);
}
return response.statusText;
return infos.status === 'ok';
})
.catch((error) => {
console.log(`Nextcloud: ERROR adding group ${groupName}`);
console.log(error.response && error.response.data ? error.response.data : error);
return false;
});
}
......@@ -128,23 +133,26 @@ class NextcloudClient {
},
)
.then((response) => {
if (this._checkFolderActive(response) && response.statusText === 'OK') {
const infos = response.data.ocs.meta;
if (this._checkFolderActive(response) && infos.status === 'ok') {
console.log(`Nextcloud: group folder ${folderName} added`);
return this._addGroupToFolder(groupName, response.data.ocs.data.id).then((response) => {
if (response === true) {
return this._addGroupToFolder(groupName, response.data.ocs.data.id).then((resp) => {
if (resp === true) {
console.log(`Nextcloud: access and permissions set for group folder ${folderName}`);
} else {
console.log(`Nextcloud: ERROR settings group permissions for group folder ${folderName}`);
}
return resp;
});
} else {
console.log(`Nextcloud: ERROR adding group folder ${folderName}`);
return false;
}
return response.statusText;
})
.catch((error) => {
console.log(`Nextcloud: ERROR adding group folder ${folderName}`);
console.log(error.response && error.response.data ? error.response.data : error);
return false;
});
}
......@@ -159,21 +167,36 @@ class NextcloudClient {
},
})
.then((response) => {
if (this._checkFolderActive(response)) {
if (this._checkFolderActive(response) && response.data.ocs.meta.status === 'ok') {
// find groupFolder ID for groupName
const groups = Object.values(response.data.ocs.data).filter((entry) => entry.mount_point === groupName);
groups.forEach((group) => {
console.log(`Nextcloud: deleting group folder ${group.id} (${group.mount_point})`);
axios.delete(`${this.appsURL}/groupfolders/folders/${group.id}`, {
params: { format: 'json' },
headers: {
Accept: 'application/json',
Authorization: `Basic ${this.basicAuth}`,
'OCS-APIRequest': true,
},
});
const folders = Object.values(response.data.ocs.data).filter((entry) => {
return entry.mount_point === groupName && Object.keys(entry.groups).includes(groupName);
});
}
return Promise.all(
folders.map((folder) => {
// check that folder is linked to group
return axios
.delete(`${this.appsURL}/groupfolders/folders/${folder.id}`, {
params: { format: 'json' },
headers: {
Accept: 'application/json',
Authorization: `Basic ${this.basicAuth}`,
'OCS-APIRequest': true,
},
})
.then((response) => {
const infos = response.data.ocs.meta;
if (infos.status === 'ok') {
console.log(`Nextcloud: removed group folder ${folder.id} (${folder.mount_point})`);
return true;
} else {
console.log(`Nextcloud: ERROR deleting group folder ${folder.id} (${infos.message})`);
return false;
}
});
}),
).then((responses) => !responses.includes(false));
} else return false;
});
}
......@@ -186,13 +209,18 @@ class NextcloudClient {
},
})
.then((response) => {
if (response.statusText === 'OK') {
const infos = response.data.ocs.meta;
if (infos.status === 'ok') {
console.log(`Nextcloud: group ${groupName} removed`);
} else {
console.log(`Nextcloud: Error removing group ${groupName} (${infos.message})`);
}
return infos.status === 'ok';
})
.catch((error) => {
console.log(`Nextcloud: ERROR removing group ${groupName}`);
console.log(error.response && error.response.data ? error.response.data : error);
return false;
});
}
}
......
......@@ -65,6 +65,29 @@ export const unfavGroup = new ValidatedMethod({
},
});
const _createGroup = function ({ name, type, content, description, nextcloud, userId }) {
try {
const groupId = Groups.insert({
name,
type,
content,
description,
owner: userId,
admins: [userId],
active: true,
nextcloud,
});
Roles.addUsersToRoles(userId, 'admin', groupId);
favGroup._execute({ userId: userId }, { groupId });
} catch (error) {
if (error.code === 11000) {
throw new Meteor.Error('api.groups.createGroup.duplicateName', i18n.__('api.groups.groupAlreadyExist'));
} else {
throw error;
}
}
};
export const createGroup = new ValidatedMethod({
name: 'groups.createGroup',
validate: new SimpleSchema({
......@@ -79,34 +102,27 @@ export const createGroup = new ValidatedMethod({
if (!isActive(this.userId)) {
throw new Meteor.Error('api.groups.createGroup.notLoggedIn', i18n.__('api.users.mustBeLoggedIn'));
}
try {
const groupId = Groups.insert({
name,
type,
content,
description,
owner: this.userId,
admins: [this.userId],
active: true,
nextcloud,
});
Roles.addUsersToRoles(this.userId, 'admin', groupId);
favGroup._execute({ userId: this.userId }, { groupId });
} catch (error) {
if (error.code === 11000) {
throw new Meteor.Error('api.groups.createGroup.duplicateName', i18n.__('api.groups.groupAlreadyExist'));
} else {
throw error;
}
}
if (kcClient) {
// create associated groups and roles in keycloak
kcClient.addGroup({ name });
}
if (nextcloud && nextClient) {
// create associated group in Nextcloud
nextClient.addGroup(name).then((response) => nextClient.addGroupFolder(name, name));
}
return nextClient.addGroup(name).then((response) => {
if (response) {
return nextClient.addGroupFolder(name, name).then((res) => {
if (res === false)
throw new Meteor.Error(
'api.groups.createGroup.nextcloudError',
i18n.__('api.nextcloud.addGroupFolderError'),
);
else _createGroup({ name, type, content, description, nextcloud, userId: this.userId });
});
} else {
throw new Meteor.Error('api.groups.createGroup.nextcloudError', i18n.__('api.nextcloud.addGroupError'));
}
});
} else _createGroup({ name, type, content, description, nextcloud, userId: this.userId });
},
});
......@@ -136,8 +152,10 @@ export const removeGroup = new ValidatedMethod({
if (nextClient && group.nextcloud) {
// remove group from nextcloud if it exists
nextClient.groupExists(group.name).then((res) => {
if (res === true) {
nextClient.removeGroup(group.name).then(() => nextClient.removeGroupFolder(group.name));
if (res) {
nextClient.removeGroupFolder(group.name).then((response) => {
if (response) nextClient.removeGroup(group.name);
});
}
});
}
......@@ -209,11 +227,13 @@ export const updateGroup = new ValidatedMethod({
}
// create nextcloud group if needed
if (nextClient) {
const groupName = groupData.name || group.name;
if (data.nextcloud === true || (data.nextcloud === undefined && group.nextcloud === true)) {
const groupName = groupData.name || group.name;
nextClient.groupExists(groupName).then((res) => {
if (res === false) {
nextClient.addGroup(groupName).then((response) => nextClient.addGroupFolder(groupName, groupName));
nextClient.addGroup(groupName).then((response) => {
if (response) nextClient.addGroupFolder(groupName, groupName);
});
}
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment