Commit a346cb05 authored by Boiget Boiget's avatar Boiget Boiget

Finish error management for Nextcloud

parent f5150a3c
Pipeline #2323 failed with stages
in 2 minutes and 10 seconds
......@@ -187,6 +187,13 @@
"excludeRole": "Excluded Role",
"keycloakId": "Keycloak Id"
}
},
"nextcloud": {
"groupExists": "Group already exists in Nextcloud",
"addGroupError": "Error, could not create group in Nextcloud",
"addGroupFolderError": "Error, could not create group folder in Nextcloud",
"removeGroupError": "Warning, could not delete group in Nextcloud",
"removeGroupFolderError": "Warning, could not delete group folder in Nextcloud"
}
},
"components": {
......
......@@ -187,6 +187,13 @@
"excludeRole": "Rôle Exclus",
"keycloakId": "Identifiant Keycloak"
}
},
"nextcloud": {
"groupExists": "Ce groupe existe déjà dans Nextcloud",
"addGroupError": "Erreur lors de la création du groupe dans Nextcloud",
"addGroupFolderError": "Erreur lors de la création du partage dans Nextcloud",
"removeGroupError": "Attention, le groupe n'a pas pu être supprimé dans Nextcloud",
"removeGroupFolderError": "Attention, le dossier n'a pas pu être supprimé dans Nextcloud"
}
},
"components": {
......
......@@ -56,12 +56,12 @@ class NextcloudClient {
} else {
console.log(`Nextcloud: ERROR adding group ${groupName} (${infos.statuscode} - ${infos.message})`);
}
return infos.status === 'ok';
return infos.status === 'ok' ? infos.status : infos.message;
})
.catch((error) => {
console.log(`Nextcloud: ERROR adding group ${groupName}`);
console.log(error.response && error.response.data ? error.response.data : error);
return false;
return `Nextcloud: ERROR adding group ${groupName}`;
});
}
......
......@@ -109,7 +109,7 @@ export const createGroup = new ValidatedMethod({
if (nextcloud && nextClient) {
// create associated group in Nextcloud
return nextClient.addGroup(name).then((response) => {
if (response) {
if (response === 'ok') {
return nextClient.addGroupFolder(name, name).then((res) => {
if (res === false)
throw new Meteor.Error(
......@@ -119,7 +119,9 @@ export const createGroup = new ValidatedMethod({
else _createGroup({ name, type, content, description, nextcloud, userId: this.userId });
});
} else {
throw new Meteor.Error('api.groups.createGroup.nextcloudError', i18n.__('api.nextcloud.addGroupError'));
const msg =
response === 'group exists' ? i18n.__('api.nextcloud.groupExists') : i18n.__('api.nextcloud.addGroupError');
throw new Meteor.Error('api.groups.createGroup.nextcloudError', msg);
}
});
} else _createGroup({ name, type, content, description, nextcloud, userId: this.userId });
......@@ -149,24 +151,48 @@ export const removeGroup = new ValidatedMethod({
// delete associated groups and roles in keycloak
kcClient.removeGroup(group);
}
// remove all roles set on this group
Roles.removeScope(groupId);
Groups.remove(groupId);
// remove from users favorite groups
Meteor.users.update({ favGroups: { $all: [groupId] } }, { $pull: { favGroups: groupId } }, { multi: true });
if (nextClient && group.nextcloud) {
// remove group from nextcloud if it exists
nextClient.groupExists(group.name).then((res) => {
return nextClient.groupExists(group.name).then((res) => {
if (res) {
nextClient.removeGroupFolder(group.name).then((response) => {
if (response) nextClient.removeGroup(group.name);
return nextClient.removeGroupFolder(group.name).then((response) => {
if (response)
return nextClient.removeGroup(group.name).then((res) => {
if (res === false)
throw new Meteor.Error(
'api.groups.removeGroup.nextcloudError',
i18n.__('api.nextcloud.removeGroupError'),
);
});
else
throw new Meteor.Error(
'api.groups.removeGroup.nextcloudError',
i18n.__('api.nextcloud.removeGroupFolderError'),
);
});
}
});
}
// remove all roles set on this group
Roles.removeScope(groupId);
Groups.remove(groupId);
// remove from users favorite groups
Meteor.users.update({ favGroups: { $all: [groupId] } }, { $pull: { favGroups: groupId } }, { multi: true });
},
});
const _updateGroup = function (groupId, groupData) {
try {
Groups.update({ _id: groupId }, { $set: groupData });
} catch (error) {
if (error.code === 11000) {
throw new Meteor.Error('api.groups.updateGroup.duplicateName', i18n.__('api.groups.groupAlreadyExist'));
} else {
throw error;
}
}
};
export const updateGroup = new ValidatedMethod({
name: 'groups.updateGroup',
validate: new SimpleSchema({
......@@ -212,32 +238,37 @@ export const updateGroup = new ValidatedMethod({
} else {
groupData = { ...data };
}
try {
Groups.update({ _id: groupId }, { $set: groupData });
} catch (error) {
if (error.code === 11000) {
throw new Meteor.Error('api.groups.updateGroup.duplicateName', i18n.__('api.groups.groupAlreadyExist'));
} else {
throw error;
}
}
// update group in keycloak if name has changed
if (kcClient && groupData.name && groupData.name !== group.name) {
kcClient.updateGroup(group.name, groupData.name);
}
// create nextcloud group if needed
if (nextClient) {
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) => {
if (response) nextClient.addGroupFolder(groupName, groupName);
});
}
});
}
}
const nextRequired = data.nextcloud === true || (data.nextcloud === undefined && group.nextcloud === true);
if (nextClient && nextRequired) {
const groupName = groupData.name || group.name;
return nextClient.groupExists(groupName).then((res) => {
if (res === false) {
return nextClient.addGroup(groupName).then((response) => {
if (response === 'ok') {
return nextClient.addGroupFolder(groupName, groupName).then((res) => {
if (res === false)
throw new Meteor.Error(
'api.groups.updateGroup.nextcloudError',
i18n.__('api.nextcloud.addGroupFolderError'),
);
else _updateGroup(groupId, groupData);
});
} else {
const msg =
response === 'group exists'
? i18n.__('api.nextcloud.groupExists')
: i18n.__('api.nextcloud.addGroupError');
throw new Meteor.Error('api.groups.updateGroup.nextcloudError', msg);
}
});
} else _updateGroup(groupId, groupData);
});
} else _updateGroup(groupId, groupData);
},
});
......
......@@ -216,6 +216,7 @@ const AdminSingleGroupPage = ({ group, ready, match: { params } }) => {
onChange={() => setNextcloud(!nextcloud)}
name="nextcloud"
color="primary"
disabled={!isAdmin && !!params._id}
/>
}
label={i18n.__('pages.AdminSingleGroupPage.nextcloud')}
......
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