Explorar el Código

impl delete user

Yuki Takei hace 9 años
padre
commit
1cf5edd921
Se han modificado 3 ficheros con 39 adiciones y 4 borrados
  1. 1 1
      lib/models/page.js
  2. 7 1
      lib/models/user.js
  3. 31 2
      lib/routes/admin.js

+ 1 - 1
lib/models/page.js

@@ -944,7 +944,7 @@ module.exports = function(crowi) {
   pageSchema.statics.removePageByPath = function(pagePath) {
   pageSchema.statics.removePageByPath = function(pagePath) {
     var Page = this;
     var Page = this;
 
 
-    return Page.findPageByPath(redirectPath)
+    return Page.findPageByPath(pagePath)
       .then(function(pageData) {
       .then(function(pageData) {
         return Page.removePageById(pageData.id);
         return Page.removePageById(pageData.id);
       });
       });

+ 7 - 1
lib/models/user.js

@@ -245,10 +245,16 @@ module.exports = function(crowi) {
 
 
   userSchema.methods.statusDelete = function(callback) {
   userSchema.methods.statusDelete = function(callback) {
     debug('Delete User', this);
     debug('Delete User', this);
+
+    const now = new Date();
+
     this.status = STATUS_DELETED;
     this.status = STATUS_DELETED;
+    this.username = `deleted_at_${now.getTime()}`;
     this.password = '';
     this.password = '';
+    this.name = '';
     this.email = 'deleted@deleted';
     this.email = 'deleted@deleted';
     this.googleId = null;
     this.googleId = null;
+    this.isGravatarEnabled = false;
     this.image = null;
     this.image = null;
     this.save(function(err, userData) {
     this.save(function(err, userData) {
       return callback(err, userData);
       return callback(err, userData);
@@ -383,7 +389,7 @@ module.exports = function(crowi) {
   userSchema.statics.findUsersWithPagination = function(options, callback) {
   userSchema.statics.findUsersWithPagination = function(options, callback) {
     var sort = options.sort || {status: 1, username: 1, createdAt: 1};
     var sort = options.sort || {status: 1, username: 1, createdAt: 1};
 
 
-    this.paginate({}, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
+    this.paginate({status: { $ne: STATUS_DELETED }}, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
       if (err) {
       if (err) {
         debug('Error on pagination:', err);
         debug('Error on pagination:', err);
         return callback(err, null);
         return callback(err, null);

+ 31 - 2
lib/routes/admin.js

@@ -359,8 +359,37 @@ module.exports = function(crowi, app) {
   };
   };
 
 
   actions.user.remove = function(req, res) {
   actions.user.remove = function(req, res) {
-    // 未実装
-    return res.redirect('/admin/users');
+    var id = req.params.id;
+    let username = '';
+
+    return new Promise((resolve, reject) => {
+      User.findById(id, (err, userData) => {
+        username = userData.username;
+        return resolve(userData);
+      });
+    })
+    .then((userData) => {
+      return new Promise((resolve, reject) => {
+        userData.statusDelete((err, userData) => {
+          if (err) {
+            reject(err);
+          }
+          resolve(userData);
+        });
+      });
+    })
+    .then((userData) => {
+      return Page.removePageByPath(`/user/${username}`)
+        .then(() => userData);
+    })
+    .then((userData) => {
+      req.flash('successMessage', `${username} さんのアカウントを削除しました`);
+      return res.redirect('/admin/users');
+    })
+    .catch((err) => {
+      req.flash('errorMessage', '削除に失敗しました。');
+      return res.redirect('/admin/users');
+    });
   };
   };
 
 
   // これやったときの relation の挙動未確認
   // これやったときの relation の挙動未確認