Began making generic tree re-arranging methods, still buggy

This commit is contained in:
Stefan Zermatten
2019-07-30 16:47:21 +02:00
parent 4b7ff2146f
commit d0304da4fd
18 changed files with 176 additions and 60 deletions

View File

@@ -1,4 +1,5 @@
import { _ } from 'meteor/underscore';
import fetchDocByRef from '/imports/api/parenting/fetchDocByRef.js';
function assertIdValid(userId){
if (!userId || typeof userId !== 'string'){
@@ -25,6 +26,12 @@ export function assertOwnership(doc, userId){
}
}
/**
* Assert that the user can edit the root document which manages its own sharing
* permissions.
*
* Warning: the doc and userId must be set by a trusted source
*/
export function assertEditPermission(doc, userId) {
assertIdValid(userId);
assertdocExists(doc);
@@ -36,6 +43,22 @@ export function assertEditPermission(doc, userId) {
}
}
function getRoot(doc){
assertdocExists(doc);
return fetchDocByRef(doc.ancestors && doc.ancestors.length && doc.ancestors[0] || doc);
}
/**
* Assert that the user can edit a descendant document whose root ancestor
* implements sharing permissions.
*
* Warning: the doc and userId must be set by a trusted source
*/
export function assertDocEditPermission(doc, userId){
let root = getRoot(doc);
assertEditPermission(root, userId);
}
export function assertViewPermission(doc, userId) {
assertIdValid(userId);
assertdocExists(doc);
@@ -51,3 +74,14 @@ export function assertViewPermission(doc, userId) {
`You do not have permission to view this character`);
}
}
/**
* Assert that the user can view a descendant document whose root ancestor
* implements sharing permissions.
*
* Warning: the doc and userId must be set by a trusted source
*/
export function assertDocViewPermission(doc, userId){
let root = getRoot(doc);
assertViewPermission(root, userId);
}