Sfoglia il codice sorgente

model save wrapper permissions: owner (read slice) or write permission list

Ivan Asmer 5 anni fa
parent
commit
d69897759b
1 ha cambiato i file con 43 aggiunte e 6 eliminazioni
  1. 43 6
      index.js

+ 43 - 6
index.js

@@ -196,6 +196,35 @@
 
 						return (intersect.length || (await slice.allUserIds).includes(userId + '')) 					
 					}
+
+					let wrapSave = instance => {
+						let save = instance.save
+						instance.save = async function(...params){
+							let readSlice = await Slice.findOne({where: {
+																	model: model.getTableName(), 
+																	modelId: instance.id,
+																	permission: 'read'
+																}})
+							if (!readSlice) throw ReferenceError('No access to write')
+							if (readSlice.ownerId === userId){
+								console.log('SAVE because owner')
+								return save.apply(instance,params)
+							}
+
+							let writeSlice = await Slice.findOne({where: {
+																	model: model.getTableName(), 
+																	modelId: instance.id,
+																	permission: 'write'
+																}})
+							if (await checker(writeSlice)){
+								return save.apply(instance, params)
+							}
+							throw ReferenceError('No access to write')
+							return null;
+						}
+					}
+
+
 					let wrappers   = {
 						async read(...params){
 							console.log('read wrapper')
@@ -212,6 +241,7 @@
 									let instance  = result.filter(instance => instance.id === slice.modelId)[0]
 									//role check
 									if (await checker(slice)) {
+										wrapSave(instance)
 										filteredResult.push(instance)
 										continue;
 									}
@@ -224,6 +254,7 @@
 																		modelId: result.id,
 																		permission,
 																	}})
+								wrapSave(result)
 								return (await checker(slice)) ? result : null;
 							}
 						},
@@ -241,6 +272,7 @@
 										permission: 'read',
 										slice: createSlice.slice, //default read permissions from create
 									})
+									wrapSave(result)
 									return result
 								}
 							}
@@ -258,14 +290,19 @@
 		}
 	}
 
-	let SlicedContent = await sliced(Content)(3)
-	//console.log(await SlicedContent.findAll({}))
+	let SlicedContent = await sliced(Content)(2)
+	console.log(JSON.stringify(await SlicedContent.findAll({}),null, 4))
+
+	//let newContent    = await SlicedContent.create({title: "SLiced", data: "SLICED"})
+	//console.log(newContent)
+	//
+	let content = await SlicedContent.findByPk(7)
+	content.data = `SLICED by WRITE permission for groupzzz #1`
+	content.save()
 
-	let newContent    = await SlicedContent.create({title: "SLiced", data: "SLICED"})
-	console.log(newContent)
 
-	newContent.data = 'sliced2'
-	await newContent.save()
+	//newContent.data = `SLICED ${newContent.id}`
+	//await newContent.save()