From fc3fe4e3a726b193b3fd416d2314e7d695b06562 Mon Sep 17 00:00:00 2001
From: Giannis Kopanas <jkopanas@gmail.com>
Date: Mon, 1 Apr 2019 13:19:00 +0300
Subject: [PATCH] fix(authsome): fix rules

---
 config/modules/authsome.js | 147 ++++++-------------------------------
 package.json               |   5 +-
 yarn.lock                  |   5 ++
 3 files changed, 29 insertions(+), 128 deletions(-)

diff --git a/config/modules/authsome.js b/config/modules/authsome.js
index 6f52996..fae78fb 100644
--- a/config/modules/authsome.js
+++ b/config/modules/authsome.js
@@ -44,7 +44,6 @@ class EditoriaMode {
   static difference(object, base) {
     const changes = (object, base) =>
       transform(object, (result, value, key) => {
-        // console.log(value, "!!!", base, key, "$$$$")
         if (!isEqual(value, base[key])) {
           result[key] =
             isObject(value) && isObject(base[key])
@@ -156,67 +155,24 @@ class EditoriaMode {
     }
     return undefined
   }
-  // async canRead() {
-  //   console.log(getFnName(this))
-  //   this.user = await this.context.models.User.find(this.userId)
-
-  //   const collection = await this.findBookByObject(this.object)
-
-  //   const permission =
-  //     (await this.isAuthor(collection)) ||
-  //     (await this.isAssignedCopyEditor(collection)) ||
-  //     (await this.isAssignedProductionEditor(collection))
-
-  //   return permission
-  // }
 
   async canReadBook() {
     await this.getUser()
     const Book = await this.context.models.Book.find(this.object.id)
-    console.log(Book, 'canReadBokk')
+
     const permission =
       (await this.isAuthor(Book)) ||
       (await this.isAssignedCopyEditor(Book)) ||
       (await this.isAssignedProductionEditor(Book))
 
-    console.log(
-      await this.isAssignedProductionEditor(Book),
-      await this.isAssignedCopyEditor(Book),
-      await this.isAuthor(Book),
-    )
-    console.log(permission, '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
-
     return permission
   }
 
   /* eslint-disable */
   async canReadBookCollection() {
-    console.log(true, 'canReadBookCollection')
     return true
   }
-  /* eslint-enable */
-
-  // async canListCollections() {
-  //   this.user = await this.context.models.User.find(this.userId)
-
-  //   return {
-  //     filter: async collections => {
-  //       const filteredCollections = await Promise.all(
-  //         collections.map(async collection => {
-  //           const condition =
-  //             (await this.isAuthor(collection)) ||
-  //             (await this.isAssignedCopyEditor(collection)) ||
-  //             (await this.isAssignedProductionEditor(collection))
-  //           return condition ? collection : undefined
-  //         }, this),
-  //       )
-
-  //       return filteredCollections.filter(collection => collection)
-  //     },
-  //   }
-  // }
 
-  /* eslint-disable */
   async canCreateUser() {
     return {
       filter: data => {
@@ -511,7 +467,6 @@ class EditoriaMode {
     const progressType = this.object.type
 
     const collection = { id: this.object.bookId }
-    console.log(collection, progressType)
     if (collection) {
       if (await this.isAssignedProductionEditor(collection)) {
         return true
@@ -549,7 +504,6 @@ class EditoriaMode {
         return condition
       } else if (await this.isAuthor(collection)) {
         let condition = false
-        console.log('Author')
         switch (progressType) {
           case 'review': {
             condition = true
@@ -693,6 +647,7 @@ class EditoriaMode {
     const bookComponent = this.object
 
     const isEditingSate = this.getStageType(bookComponent, 'edit').value === 0
+
     const isCleanUpSate =
       this.getStageType(bookComponent, 'clean_up').value === 0
 
@@ -720,48 +675,35 @@ class EditoriaMode {
     const collection = { id: bookComponent.bookId }
 
     if (
-      !this.canEditFull() &&
+      !(await this.canEditFull()) &&
       (await this.isAuthor(collection)) &&
       isReviewingSate
     ) {
       return true
     }
-
     return false
   }
 
   async canEditSelection() {
-    if (!this.canEditFull() && !this.canEditReview()) {
+    if (!(await this.canEditFull()) && !(await this.canEditReview())) {
       return true
     }
 
     return false
   }
 
-  // async canInteractWithEditor() {
-  //   await this.getUser()
-  //   const bookComponent = this.object
-  //   const isReviewingSate =
-  //     this.getStageType(bookComponent, 'review').value === 0
-  //   const isEditingSate = this.getStageType(bookComponent, 'edit').value === 0
-  //   const isCleanUpSate =
-  //     this.getStageType(bookComponent, 'clean_up').value === 0
-  //   const collection = { id: bookComponent.bookId }
-
-  //   if (collection) {
-  //     if (await this.isAssignedProductionEditor(collection)) {
-  //       return 'full'
-  //     } else if (
-  //       (await this.isAssignedCopyEditor(collection)) &&
-  //       (isEditingSate || isCleanUpSate)
-  //     ) {
-  //       return 'full'
-  //     } else if ((await this.isAuthor(collection)) && isReviewingSate) {
-  //       return 'review'
-  //     }
-  //   }
-  //   return 'selection'
-  // }
+  async canToggleTrackChanges() {
+    await this.getUser()
+    const bookComponent = this.object
+    const collection = { id: bookComponent.bookId }
+
+    const permissions = !(await this.checkTeamMembers(
+      ['isAuthor', 'isAssignedCopyEditor'],
+      collection,
+    ))
+
+    return permissions
+  }
 
   /* eslint-disable */
   async canRemoveTeamMember() {
@@ -867,7 +809,6 @@ module.exports = {
   read: (userId, operation, object, context) => {
     const mode = new EditoriaMode(userId, operation, object, context)
     mode.backend = true
-    console.log(object, '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@')
     if (object === 'Book' || object === 'Team' || object === 'BookCollection') {
       return true
     }
@@ -889,52 +830,6 @@ module.exports = {
 
     return false
   },
-  // PATCH: (userId, operation, object, context) => {
-  //   const mode = new EditoriaMode(userId, operation, object, context)
-  //   // PATCH /api/collections/:id
-  //   let data
-  //   if (object) {
-  //     if (object.current) {
-  //       data = object.current
-  //     } else {
-  //       data = object
-  //     }
-  //   } else {
-  //     return false
-  //   }
-
-  //   // if (data.type === 'collection') {
-  //   //   return mode.canInteractWithBooks()
-  //   // }
-  //   // PATCH /api/fragments/:id
-  //   if (data.type === 'fragment') {
-  //     return mode.canUpdateBook()
-  //   }
-  //   // PATCH /api/teams/:id
-  //   if (data.type === 'team') {
-  //     return mode.canUpdateTeam()
-  //   }
-
-  //   return false
-  // },
-  // DELETE: (userId, operation, object, context) => {
-  //   const mode = new EditoriaMode(userId, operation, object, context)
-  //   // DELETE /api/collections/:id
-  //   if (object && object.type === 'collection') {
-  //     return mode.canInteractWithBooks()
-  //   }
-  //   // DELETE /api/fragments/:id
-  //   if (object && object.type === 'fragment') {
-  //     return mode.canInteractWithBookComponents()
-  //   }
-
-  //   // DELETE /api/teams/:id
-  //   if (object && object.type === 'team') {
-  //     return mode.canUpdateTeam()
-  //   }
-
-  //   return false
-  // },
   'can view nav links': (userId, operation, object, context) => false,
   'can add books': (userId, operation, object, context) => {
     // DONE
@@ -1046,6 +941,10 @@ module.exports = {
     //   return false
     // }
     true,
+  'can toggle track changes': (userId, operation, object, context) => {
+    const mode = new EditoriaMode(userId, operation, object, context)
+    return mode.canToggleTrackChanges()
+  },
   'can edit full': (userId, operation, object, context) => {
     const mode = new EditoriaMode(userId, operation, object, context)
     return mode.canEditFull()
@@ -1054,13 +953,9 @@ module.exports = {
     const mode = new EditoriaMode(userId, operation, object, context)
     return mode.canEditSelection()
   },
-  'can edit review': (userId, operation, object, context) => {
+  'can edit preview': (userId, operation, object, context) => {
     const mode = new EditoriaMode(userId, operation, object, context)
     return mode.canEditReview()
   },
-  // 'can interact with editor': (userId, operation, object, context) => {
-  //   const mode = new EditoriaMode(userId, operation, object, context)
-  //   return mode.canInteractWithEditor()
-  // },
   // TODO: protect ink endpoint
 }
diff --git a/package.json b/package.json
index 307bd36..a575672 100644
--- a/package.json
+++ b/package.json
@@ -20,11 +20,11 @@
   "version": "1.1.0",
   "dependencies": {
     "@pubsweet/coko-theme": "^5.0.7",
+    "@pubsweet/db-manager": "^3.0.3",
     "@pubsweet/logger": "^0.2.2",
     "@pubsweet/model-fragment": "^3.0.4",
     "@pubsweet/model-team": "^2.0.4",
     "@pubsweet/model-user": "^4.0.4",
-    "@pubsweet/db-manager": "^3.0.3",
     "@pubsweet/ui": "^10.0.2",
     "@pubsweet/ui-toolkit": "^2.0.7",
     "authsome": "^0.1.0",
@@ -52,9 +52,10 @@
     "pubsweet-component-wax": "^0.7.0",
     "pubsweet-server": "^13.2.0",
     "pubsweet-theme-plugin": "^0.0.3",
-    "react-beautiful-dnd":"^10.0.4",
     "react": "^16.2.0",
+    "react-beautiful-dnd": "^10.0.4",
     "react-bootstrap": "0.32.1",
+    "react-codemirror2": "^5.1.0",
     "react-dom": "^16.2.0",
     "react-router-bootstrap": "0.24.4",
     "react-router-dom": "^4.3.1",
diff --git a/yarn.lock b/yarn.lock
index ddbc135..08d1ead 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -13561,6 +13561,11 @@ react-bootstrap@^0.32.0:
     uncontrollable "^5.0.0"
     warning "^3.0.0"
 
+react-codemirror2@^5.1.0:
+  version "5.1.0"
+  resolved "https://registry.yarnpkg.com/react-codemirror2/-/react-codemirror2-5.1.0.tgz#62de4460178adea40eb52eabf7491669bf3794b8"
+  integrity sha512-Cksbgbviuf2mJfMyrKmcu7ycK6zX/ukuQO8dvRZdFWqATf5joalhjFc6etnBdGCcPA2LbhIwz+OPnQxLN/j1Fw==
+
 react-codemirror@^1.0.0:
   version "1.0.0"
   resolved "https://registry.yarnpkg.com/react-codemirror/-/react-codemirror-1.0.0.tgz#91467b53b1f5d80d916a2fd0b4c7adb85a9001ba"
-- 
GitLab