Просмотр исходного кода

Merge pull request #471 from weseek/fix/469-grant-group-modal

Fix/469 grant group modal
Yuki Takei 7 лет назад
Родитель
Сommit
4b54b3f8d0
4 измененных файлов с 96 добавлено и 41 удалено
  1. 27 13
      lib/models/page-group-relation.js
  2. 31 14
      lib/models/user-group-relation.js
  3. 18 5
      lib/models/user-group.js
  4. 20 9
      lib/routes/admin.js

+ 27 - 13
lib/models/page-group-relation.js

@@ -45,6 +45,27 @@ class PageGroupRelation {
     return this._crowi;
   }
 
+  static init() {
+    this.removeAllInvalidRelations();
+  }
+
+  /**
+   * remove all invalid relations that has reference to unlinked document
+   */
+  static removeAllInvalidRelations() {
+    return this.findAllRelation()
+      .then(relations => {
+        // filter invalid documents
+        return relations.filter(relation => {
+          return relation.targetPage == null || relation.relatedGroup == null;
+        });
+      })
+      .then(invalidRelations => {
+        const ids = invalidRelations.map(relation => relation._id);
+        return this.deleteMany({ _id: { $in: ids }});
+      });
+  }
+
   /**
    * find all page and group relation
    *
@@ -57,6 +78,7 @@ class PageGroupRelation {
     return this
       .find()
       .populate('targetPage')
+      .populate('relatedGroup')
       .exec();
   }
 
@@ -188,18 +210,7 @@ class PageGroupRelation {
    * @memberof PageGroupRelation
    */
   static removeAllByUserGroup(userGroup) {
-
-    return this.findAllRelationForUserGroup(userGroup)
-      .then((relations) => {
-        if (relations == null) {
-          return;
-        }
-        else {
-          relations.map((relation) => {
-            relation.remove();
-          });
-        }
-      });
+    return this.deleteMany({ relatedGroup: userGroup });
   }
 
   /**
@@ -240,10 +251,13 @@ class PageGroupRelation {
         }
       });
   }
+
 }
 
 module.exports = function(crowi) {
   PageGroupRelation.crowi = crowi;
   schema.loadClass(PageGroupRelation);
-  return mongoose.model('PageGroupRelation', schema);
+  const model = mongoose.model('PageGroupRelation', schema);
+  model.init();
+  return model;
 };

+ 31 - 14
lib/models/user-group-relation.js

@@ -40,6 +40,27 @@ class UserGroupRelation {
     return this._crowi;
   }
 
+  static init() {
+    this.removeAllInvalidRelations();
+  }
+
+  /**
+   * remove all invalid relations that has reference to unlinked document
+   */
+  static removeAllInvalidRelations() {
+    return this.findAllRelation()
+      .then(relations => {
+        // filter invalid documents
+        return relations.filter(relation => {
+          return relation.relatedUser == null || relation.relatedGroup == null;
+        });
+      })
+      .then(invalidRelations => {
+        const ids = invalidRelations.map(relation => relation._id);
+        return this.deleteMany({ _id: { $in: ids }});
+      });
+  }
+
   /**
    * find all user and group relation
    *
@@ -99,7 +120,12 @@ class UserGroupRelation {
     return this
       .find({ relatedUser: user.id })
       .populate('relatedGroup')
-      .exec();
+      // filter documents only relatedGroup is not null
+      .then(userGroupRelations => {
+        return userGroupRelations.filter(relation => {
+          return relation.relatedGroup != null;
+        });
+      });
   }
 
   /**
@@ -223,18 +249,7 @@ class UserGroupRelation {
    * @memberof UserGroupRelation
    */
   static removeAllByUserGroup(userGroup) {
-
-    return this.findAllRelationForUserGroup(userGroup)
-      .then((relations) => {
-        if (relations == null) {
-          return;
-        }
-        else {
-          relations.map((relation) => {
-            relation.remove();
-          });
-        }
-      });
+    return this.deleteMany({ relatedGroup: userGroup });
   }
 
   /**
@@ -263,5 +278,7 @@ class UserGroupRelation {
 module.exports = function(crowi) {
   UserGroupRelation.crowi = crowi;
   schema.loadClass(UserGroupRelation);
-  return mongoose.model('UserGroupRelation', schema);
+  const model = mongoose.model('UserGroupRelation', schema);
+  model.init();
+  return model;
 };

+ 18 - 5
lib/models/user-group.js

@@ -100,21 +100,34 @@ class UserGroup {
 
   // グループの完全削除
   static removeCompletelyById(id) {
+    const PageGroupRelation = mongoose.model('PageGroupRelation');
+    const UserGroupRelation = mongoose.model('UserGroupRelation');
 
+    let removed = undefined;
     return this.findById(id)
-      .then((userGroupData) => {
+      .then(userGroupData => {
         if (userGroupData == null) {
           throw new Exception('UserGroup data is not exists. id:', id);
         }
-        else {
-          return userGroupData.remove();
-        }
+        return userGroupData.remove();
+      })
+      .then(removedUserGroupData => {
+        removed = removedUserGroupData;
+      })
+      // remove relations
+      .then(() => {
+        return Promise.all([
+          UserGroupRelation.removeAllByUserGroup(removed),
+          PageGroupRelation.removeAllByUserGroup(removed),
+        ]);
+      })
+      .then(() => {
+        return removed;
       });
   }
 
   // グループ生成(名前が要る)
   static createGroupByName(name) {
-
     return this.create({name: name});
   }
 

+ 20 - 9
lib/routes/admin.js

@@ -771,16 +771,27 @@ module.exports = function(crowi, app) {
   actions.userGroup.removeCompletely = function(req, res) {
     const id = req.body.user_group_id;
 
+    const fileUploader = require('../util/fileUploader')(crowi, app);
+
     UserGroup.removeCompletelyById(id)
-    .then(() => {
-      req.flash('successMessage', '削除しました');
-      return res.redirect('/admin/user-groups');
-    })
-    .catch((err) => {
-      debug('Error while removing userGroup.', err, id);
-      req.flash('errorMessage', '完全な削除に失敗しました。');
-      return res.redirect('/admin/user-groups');
-    });
+      //// TODO remove attachments
+      // couldn't remove because filePath includes '/uploads/uploads'
+      // Error: ENOENT: no such file or directory, unlink 'C:\dev\growi\public\uploads\uploads\userGroup\5b1df18ab69611651cc71495.png
+      //
+      // .then(removed => {
+      //   if (removed.image != null) {
+      //     fileUploader.deleteFile(null, removed.image);
+      //   }
+      // })
+      .then(() => {
+        req.flash('successMessage', '削除しました');
+        return res.redirect('/admin/user-groups');
+      })
+      .catch((err) => {
+        debug('Error while removing userGroup.', err, id);
+        req.flash('errorMessage', '完全な削除に失敗しました。');
+        return res.redirect('/admin/user-groups');
+      });
   };
 
   actions.userGroupRelation = {};