Skip to content

Commit

Permalink
Use mixin to factorize association rules
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobesson authored and Bruno Besson committed Mar 25, 2021
1 parent fe1c90c commit e8a7c97
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 90 deletions.
95 changes: 7 additions & 88 deletions src/components/association-editor/AssociationItems.vue
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,16 @@ import AssociationImageLink from './AssociationImageLink';
import AssociationOutingLink from './AssociationOutingLink';
import c2c from '@/js/apis/c2c';
import associationRights from '@/js/associations-rights-mixin';
export default {
components: {
AssociationOutingLink,
AssociationImageLink,
},
mixins: [associationRights],
props: {
parent: {
type: Object,
Expand Down Expand Up @@ -117,7 +120,10 @@ export default {
document,
status,
buttonLabel,
disabled: status === 'current' ? !this.canRemove(document) : !this.canAdd(document),
disabled:
status === 'current'
? !this.canRemove(this.parent, document)
: !this.canAdd(this.parent, document, this.forbiddenChildren),
});
}
}
Expand All @@ -135,93 +141,6 @@ export default {
},
methods: {
canAdd(child) {
// technical limitation : for waypoint/waypoint associations
if (
this.forbiddenChildren &&
this.forbiddenChildren.find((e) => e.document_id === child.document_id) !== undefined
) {
return false;
}
// can't associate a document to itself
if (this.parent.document_id === child.document_id) {
return false;
}
// moderator can always add
if (this.$user.isModerator) {
return true;
}
/**********************************
/* rules for standard users. Do not forget that if he can open this window, he can edit parent
**********************************/
// only moderator can add waypoint/waypoint association
if (this.parent.type === 'w' && this.childType === 'waypoint') {
return false;
}
if (this.childType === 'article' && child.article_type === 'personal') {
return false; // TODO API : if user is article owner, he can => add author in response
}
if (this.childType === 'xreport') {
return false; // TODO API : if user is xreport owner, he can => add author in response
}
if (this.childType === 'image') {
return true; // TODO API : add image_type (collab), and author
}
return true;
},
canRemove(child) {
// technical limitation : an outing must have at least one route
if (this.parent.type === 'o' && this.childType === 'route' && this.current.length === 1) {
return false;
}
// otherwise, moderator can always remove
if (this.$user.isModerator) {
return true;
}
/**********************************
/ rules for standard user. Do not forget that if he can open this window, he can edit parent
**********************************/
// if it's an outing, user can do whatever he want's, except remove itself
if (this.parent.type === 'o') {
return child.document_id !== this.$user.id;
}
// if parent document is user own profile : can always remove
if (this.parent.document_id === this.$user.id) {
return true;
}
if (this.childType === 'article' && child.article_type === 'personal') {
return false; // TODO API : if user is article owner, he can => add author in response
}
if (this.childType === 'xreport') {
return false; // TODO API : if user is xreport owner, he can => add author in response
}
if (this.childType === 'image') {
return false; // TODO API : add image_type (collab), and author
}
if (this.childType === 'outing') {
return child.author.user_id === this.$user.id;
}
return false;
},
onclick(child) {
if (child.status === 'current') {
this.remove(child.document);
Expand Down
94 changes: 94 additions & 0 deletions src/js/associations-rights-mixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
export default {
props: {
forbiddenChildren: {
type: Array,
default: null,
},
},

methods: {
canAdd(parent, child, forbiddenChildren = []) {
// technical limitation : for waypoint/waypoint associations, link can't be both ways
if (forbiddenChildren?.some((forbidden) => forbidden.document_id === child.document_id)) {
return false;
}

// can't associate a document to itself
if (parent.document_id === child.document_id) {
return false;
}

// moderator can always add
if (this.$user.isModerator) {
return true;
}

/**********************************
/* rules for standard users. Do not forget that if he can open this page, he can edit parent
/**********************************/

// only moderator can add waypoint/waypoint association
if (parent.type === 'w' && child.type === 'w') {
return false;
}

if (child.type === 'a' && child.article_type === 'personal') {
return false; // TODO API : if user is article owner, he can => add author in response
}

if (child.type === 'x') {
return false; // TODO API : if user is xreport owner, he can => add author in response
}

if (child.type === 'i') {
return true; // TODO API : add image_type (collab), and author
}

return true;
},

canRemove(parent, child) {
// technical limitation : an outing must have at least one route
if (parent.type === 'o' && child.type === 'r' && parent.associations.routes?.length <= 1) {
return false;
}

// otherwise, moderator can always remove
if (this.$user.isModerator) {
return true;
}

/**********************************
/ rules for standard user. Do not forget that if he can open this page, he can edit parent
/**********************************/

// if it's an outing, user can do whatever he wants, except remove itself
if (parent.type === 'o') {
return child.document_id !== this.$user.id;
}

// if parent document is user own profile: can always remove
if (parent.document_id === this.$user.id) {
return true;
}

if (child.type === 'a' && child.article_type === 'personal') {
return false; // TODO API : if user is article owner, he can => add author in response
}

if (child.type === 'x') {
return false; // TODO API : if user is xreport owner, he can => add author in response
}

if (child.type === 'i') {
return false; // TODO API : add image_type (collab), and author
}

if (child.type === 'o') {
return child.author.user_id === this.$user.id;
}

return false;
},
},
};
11 changes: 9 additions & 2 deletions src/views/wiki/edition/utils/AssociationsInputRow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div v-for="child in document.associations[field.name]" :key="child.document_id" class="column is-4">
<document-card
:document="child"
show-delete-button
:show-delete-button="showDeleteButton(child)"
@delete="$documentUtils.removeAssociation(document, child)"
target="_blank"
/>
Expand All @@ -27,14 +27,15 @@
<script>
import FormInput from './FormInput';
import associationRights from '@/js/associations-rights-mixin';
import { requireDocumentProperty, requireFieldProperty } from '@/js/properties-mixins';
export default {
components: {
FormInput,
},
mixins: [requireFieldProperty, requireDocumentProperty],
mixins: [requireFieldProperty, requireDocumentProperty, associationRights],
props: {
label: {
Expand All @@ -56,5 +57,11 @@ export default {
return this.field.error !== null;
},
},
methods: {
showDeleteButton(document) {
return this.canRemove(this.document, document); // ! FIXME import mixin
},
},
};
</script>

0 comments on commit e8a7c97

Please sign in to comment.