Browse Source

slicedSavable (permissions) save and delete. find[One] will be buggy

Ivan Asmer 5 years ago
parent
commit
92a2f36814
4 changed files with 73 additions and 50 deletions
  1. 31 28
      index.js
  2. 30 10
      mm.js
  3. 11 11
      package-lock.json
  4. 1 1
      package.json

+ 31 - 28
index.js

@@ -10,6 +10,7 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
     const Savable     = mm(db).Savable
     const SlicedSavable = mm(db).sliceSavable([ObjectID("5c9571219be797377361c65a"), 'user', 'admin'])
     //const SlicedSavable = mm(db).sliceSavable([])
+    //
 
     class Notebook extends SlicedSavable{
         static get relations(){
@@ -39,6 +40,35 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
     let rndItem  = arr => arr[Math.floor(Math.random()*arr.length)]
 
 
+    let person = new User({
+        name: 'Mykola',
+        surname: 'Silniy',
+        phones: ['105', '1'],
+        children: [
+            new User({
+                name: 'Marina',
+                surname: 'Silnaya',
+                phones: ['105', '1000503'],
+            }),
+            new User({
+                name: 'Andrey',
+                surname: 'Silniy',
+                phones: ['103', '1000502'],
+            }),
+            new User({
+                name: 'Fedor',
+                surname: 'Ivanova',
+                phones: ['102', '1000504'],
+                notebook: new Notebook({
+                    brand: 'dubovo'
+                })
+            })
+        ]
+    })
+
+    await person.save()
+
+
 
     let stamp = (new Date()).getTime()
     let prevI = 0;
@@ -119,7 +149,7 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
         return now - start
     }
 
-    await walker(8531)
+    //await walker(8531)
 
 
     //console.log(await Promise.all([walker(), walker()]))
@@ -142,33 +172,6 @@ const delay       = ms => new Promise(r => setTimeout(r.bind(ms), ms))
     
 
 
-    //let person = new User({
-        //name: 'Mykola',
-        //surname: 'Silniy',
-        //phones: ['105', '1'],
-        //children: [
-            //new User({
-                //name: 'Marina',
-                //surname: 'Silnaya',
-                //phones: ['105', '1000503'],
-            //}),
-            //new User({
-                //name: 'Andrey',
-                //surname: 'Silniy',
-                //phones: ['103', '1000502'],
-            //}),
-            //new User({
-                //name: 'Fedor',
-                //surname: 'Ivanova',
-                //phones: ['102', '1000504'],
-                //notebook: new Notebook({
-                    //brand: 'dubovo'
-                //})
-            //})
-        //]
-    //})
-
-    //await person.save()
 
 
 

+ 30 - 10
mm.js

@@ -99,7 +99,7 @@ module.exports = db => {
 
                 async function getValueByField(field, savable) {
                     let path = field.split('.');
-                    await savable.catch(e => console.log('GET VALUE BY FIELD ERROR'));
+                    await savable//.catch(e => console.log('GET VALUE BY FIELD ERROR'));
                     let result = savable;
                     let prev;
                     let lastKey = path.pop()
@@ -315,6 +315,7 @@ module.exports = db => {
 
     function sliceSavable(userACL){
         userACL = userACL.map(tag => tag.toString())
+        console.log(userACL)
         class SlicedSavable extends Savable {
             constructor(...params){
                 super  (...params)
@@ -322,9 +323,6 @@ module.exports = db => {
                 if (!this._empty){
                     this.___permissionsPrepare()
                 }
-                else {
-                    this.___owner = userACL[0] instanceof ObjectID ? userACL : ObjectID(userACL[0])
-                }
             }
 
             ___permissionsPrepare(){
@@ -338,9 +336,9 @@ module.exports = db => {
                 }
             }
 
-            ___permissionCan(permission){
-                const acl = (this.___permissions && 
-                                this.___permissions[permission] || 
+            ___permissionCan(permission, permissions=this.___permissions){
+                const acl = (permissions && 
+                                permissions[permission] || 
                                     this.__proto__.constructor.defaultPermissions[permission]).map(tag => tag.toString())
                 if (acl.includes('owner') && this.___owner && userACL.includes(this.___owner.toString())){
                     return true
@@ -353,13 +351,35 @@ module.exports = db => {
                 return false
             }
 
-            populate(...params){ //place to check read permission
-                if (!this.___permissionCan('read')){
+            populate(obj){ //place to check read permission
+                if (!this.___permissionCan('read', obj.___permissions)){
                     throw new ReferenceError(`No Access To Entity ${this._id} of class ${this._class}`)
                 }
-                super.populate(...params)
+                super.populate(obj)
+            }
+
+
+            async save(...params){
+                if (!this._id && !this.___permissionCan('create'))
+                    throw new ReferenceError(`Permissison denied Create Entity of class ${this._class}`)
+                if (!this.___permissionCan('write'))
+                    throw new ReferenceError(`Permissison denied Save Entity ${this._id} of class ${this._class}`)
+
+                if (!this._id){
+                    this.___owner = userACL[0] //TODO fix objectid troubles 
+                    console.log(typeof this.___owner, this.___owner)
+                }
+                return await super.save(...params)
             }
 
+
+            async delete(noRefs=false){
+                if (!this.___permissionCan('delete'))
+                    throw new ReferenceError(`Permissison denied Delete Entity ${this._id} of class ${this._class}`)
+                return await super.delete(noRefs)
+            }
+
+
             static get defaultPermissions(){
                 return {
                     //savable refs, objectid's, words like 'tags' or 'roles'

+ 11 - 11
package-lock.json

@@ -5,9 +5,9 @@
   "requires": true,
   "dependencies": {
     "bson": {
-      "version": "1.1.0",
-      "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.0.tgz",
-      "integrity": "sha512-9Aeai9TacfNtWXOYarkFJRW2CWo+dRon+fuLZYJmvLV3+MiUp0bEI6IAZfXEIg7/Pl/7IWlLaDnhzTsD81etQA=="
+      "version": "1.1.1",
+      "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.1.tgz",
+      "integrity": "sha512-jCGVYLoYMHDkOsbwJZBCqwMHyH4c+wzgI9hG7Z6SZJRXWr+x58pdIbm2i9a/jFGCkRJqRUr8eoI7lDWa0hTkxg=="
     },
     "memory-pager": {
       "version": "1.5.0",
@@ -16,20 +16,20 @@
       "optional": true
     },
     "mongodb": {
-      "version": "3.1.13",
-      "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.13.tgz",
-      "integrity": "sha512-sz2dhvBZQWf3LRNDhbd30KHVzdjZx9IKC0L+kSZ/gzYquCF5zPOgGqRz6sSCqYZtKP2ekB4nfLxhGtzGHnIKxA==",
+      "version": "3.2.2",
+      "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.2.2.tgz",
+      "integrity": "sha512-xQ6apOOV+w7VFApdaJpWhYhzartpjIDFQjG0AwgJkLh7dBs7PTsq4A3Bia2QWpDohmAzTBIdQVLMqqLy0mwt3Q==",
       "requires": {
-        "mongodb-core": "3.1.11",
+        "mongodb-core": "3.2.2",
         "safe-buffer": "^5.1.2"
       }
     },
     "mongodb-core": {
-      "version": "3.1.11",
-      "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.11.tgz",
-      "integrity": "sha512-rD2US2s5qk/ckbiiGFHeu+yKYDXdJ1G87F6CG3YdaZpzdOm5zpoAZd/EKbPmFO6cQZ+XVXBXBJ660sSI0gc6qg==",
+      "version": "3.2.2",
+      "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.2.2.tgz",
+      "integrity": "sha512-YRgC39MuzKL0uoGoRdTmV1e9m47NbMnYmuEx4IOkgWAGXPSEzRY7cwb3N0XMmrDMnD9vp7MysNyAriIIeGgIQg==",
       "requires": {
-        "bson": "^1.1.0",
+        "bson": "^1.1.1",
         "require_optional": "^1.0.1",
         "safe-buffer": "^5.1.2",
         "saslprep": "^1.0.0"

+ 1 - 1
package.json

@@ -19,6 +19,6 @@
   "author": "Ivan Grynkin",
   "license": "ISC",
   "dependencies": {
-    "mongodb": "^3.1.13"
+    "mongodb": "^3.2.2"
   }
 }