prevent attribute inheritance cycle via template, closes #1077

This commit is contained in:
zadam 2020-06-04 12:27:41 +02:00
parent 7307ca385f
commit 1502b9ce66
3 changed files with 35 additions and 20 deletions

View File

@ -170,6 +170,16 @@ class NoteShort {
* @returns {Attribute[]} all note's attributes, including inherited ones * @returns {Attribute[]} all note's attributes, including inherited ones
*/ */
getAttributes(type, name) { getAttributes(type, name) {
return this.__filterAttrs(this.__getCachedAttributes([]), type, name);
}
__getCachedAttributes(path) {
// notes/clones cannot form tree cycles, it is possible to create attribute inheritance cycle via templates
// when template instance is a parent of template itself
if (path.includes(this.noteId)) {
return [];
}
if (!(this.noteId in noteAttributeCache)) { if (!(this.noteId in noteAttributeCache)) {
const ownedAttributes = this.getOwnedAttributes(); const ownedAttributes = this.getOwnedAttributes();
@ -177,11 +187,13 @@ class NoteShort {
ownedAttributes ownedAttributes
]; ];
const newPath = [...path, this.noteId];
for (const templateAttr of ownedAttributes.filter(oa => oa.type === 'relation' && oa.name === 'template')) { for (const templateAttr of ownedAttributes.filter(oa => oa.type === 'relation' && oa.name === 'template')) {
const templateNote = this.treeCache.notes[templateAttr.value]; const templateNote = this.treeCache.notes[templateAttr.value];
if (templateNote) { if (templateNote && templateNote.noteId !== this.noteId) {
attrArrs.push(templateNote.getAttributes()); attrArrs.push(templateNote.__getCachedAttributes(newPath));
} }
} }
@ -189,7 +201,7 @@ class NoteShort {
for (const parentNote of this.getParentNotes()) { for (const parentNote of this.getParentNotes()) {
// these virtual parent-child relationships are also loaded into frontend tree cache // these virtual parent-child relationships are also loaded into frontend tree cache
if (parentNote.type !== 'search') { if (parentNote.type !== 'search') {
attrArrs.push(parentNote.getInheritableAttributes()); attrArrs.push(parentNote.__getInheritableAttributes(newPath));
} }
} }
} }
@ -197,7 +209,7 @@ class NoteShort {
noteAttributeCache.attributes[this.noteId] = attrArrs.flat(); noteAttributeCache.attributes[this.noteId] = attrArrs.flat();
} }
return this.__filterAttrs(noteAttributeCache.attributes[this.noteId], type, name); return noteAttributeCache.attributes[this.noteId];
} }
__filterAttrs(attributes, type, name) { __filterAttrs(attributes, type, name) {
@ -212,8 +224,8 @@ class NoteShort {
} }
} }
getInheritableAttributes() { __getInheritableAttributes(path) {
const attrs = this.getAttributes(); const attrs = this.__getCachedAttributes(path);
return attrs.filter(attr => attr.isInheritable); return attrs.filter(attr => attr.isInheritable);
} }
@ -460,4 +472,4 @@ class NoteShort {
} }
} }
export default NoteShort; export default NoteShort;

View File

@ -4,7 +4,6 @@ const TPL = `
<table class="note-info-widget-table"> <table class="note-info-widget-table">
<style> <style>
.note-info-widget-table { .note-info-widget-table {
table-layout: fixed;
width: 100%; width: 100%;
} }
@ -22,22 +21,23 @@ const TPL = `
<tr> <tr>
<th>Note ID:</th> <th>Note ID:</th>
<td colspan="3" class="note-info-note-id"></td> <td class="note-info-note-id"></td>
</tr> </tr>
<tr> <tr>
<th>Created:</th> <th>Created:</th>
<td colspan="3" class="note-info-date-created"></td> <td class="note-info-date-created"></td>
</tr> </tr>
<tr> <tr>
<th>Modified:</th> <th>Modified:</th>
<td colspan="3" class="note-info-date-modified"></td> <td class="note-info-date-modified"></td>
</tr> </tr>
<tr> <tr>
<th>Type:</th> <th>Type:</th>
<td class="note-info-type"></td> <td>
<span class="note-info-type"></span>
<th>MIME:</th>
<td class="note-info-mime"></td> <span class="note-info-mime"></span>
</td>
</tr> </tr>
</table> </table>
`; `;
@ -69,9 +69,12 @@ export default class NoteInfoWidget extends CollapsibleWidget {
this.$type.text(note.type); this.$type.text(note.type);
this.$mime if (note.mime) {
.text(note.mime) this.$mime.text('(' + note.mime + ')');
.attr("title", note.mime); }
else {
this.$mime.empty();
}
} }
entitiesReloadedEvent({loadResults}) { entitiesReloadedEvent({loadResults}) {

View File

@ -96,9 +96,9 @@ async function anonymize() {
await db.run("UPDATE api_tokens SET token = 'API token value'"); await db.run("UPDATE api_tokens SET token = 'API token value'");
await db.run("UPDATE notes SET title = 'title'"); await db.run("UPDATE notes SET title = 'title'");
await db.run("UPDATE note_contents SET content = 'text'"); await db.run("UPDATE note_contents SET content = 'text' WHERE content IS NOT NULL");
await db.run("UPDATE note_revisions SET title = 'title'"); await db.run("UPDATE note_revisions SET title = 'title'");
await db.run("UPDATE note_revision_contents SET content = 'title'"); await db.run("UPDATE note_revision_contents SET content = 'title' WHERE content IS NOT NULL");
await db.run("UPDATE attributes SET name = 'name', value = 'value' WHERE type = 'label'"); await db.run("UPDATE attributes SET name = 'name', value = 'value' WHERE type = 'label'");
await db.run("UPDATE attributes SET name = 'name' WHERE type = 'relation' AND name != 'template'"); await db.run("UPDATE attributes SET name = 'name' WHERE type = 'relation' AND name != 'template'");
await db.run("UPDATE branches SET prefix = 'prefix' WHERE prefix IS NOT NULL"); await db.run("UPDATE branches SET prefix = 'prefix' WHERE prefix IS NOT NULL");