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

Merge pull request #9 from crowi/register-notif-to-admin-on-restricted-mode

Notify to admin when user created on restricted registration mode
Sotaro KARASAWA 11 лет назад
Родитель
Сommit
0ae217e5ce
5 измененных файлов с 104 добавлено и 12 удалено
  1. 16 2
      lib/mailer.js
  2. 17 9
      models/user.js
  3. 36 1
      routes/login.js
  4. 14 0
      views/mail/admin/userInvitation.txt
  5. 21 0
      views/mail/admin/userWaitingActivation.txt

+ 16 - 2
lib/mailer.js

@@ -6,10 +6,12 @@ module.exports = function(app) {
   'use strict';
 
   var debug = require('debug')('crowi:lib:mailer')
-    , nodemailer = require("nodemailer")
+    , nodemailer = require('nodemailer')
+    , swig = require('swig')
     , config = app.set('config')
     , mailConfig = {}
     , mailer = {}
+    , MAIL_TEMPLATE_DIR = app.set('views') + '/mail/'
     ;
 
 
@@ -100,7 +102,19 @@ module.exports = function(app) {
 
   function send(config, callback) {
     if (mailer) {
-      return mailer.sendMail(setupMailConfig(config), callback);
+      var templateVars = config.vars || {};
+      return swig.renderFile(
+        MAIL_TEMPLATE_DIR + config.template,
+        templateVars,
+        function (err, output) {
+          if (err) {
+            throw err;
+          }
+
+          config.text = output;
+          return mailer.sendMail(setupMailConfig(config), callback);
+        }
+      );
     } else {
       debug('Mailer is not completed to set up. Please set up SMTP or AWS setting.');
       return callback(new Error('Mailer is not completed to set up. Please set up SMTP or AWS setting.'), null);

+ 17 - 9
models/user.js

@@ -236,6 +236,15 @@ module.exports = function(app, models) {
 
   };
 
+  userSchema.statics.findAdmins = function(callback) {
+    var User = this;
+    this.find({admin: true})
+      .exec(function(err, admins) {
+        debug('Admins: ', admins);
+        callback(err, admins);
+      });
+  };
+
   userSchema.statics.findUsersWithPagination = function(options, callback) {
     var sort = options.sort || {status: 1, username: 1, createdAt: 1};
 
@@ -405,20 +414,19 @@ module.exports = function(app, models) {
             createdUserList,
             function(user, next) {
               if (user.password === null) {
-                next();
+                return next();
               }
 
               mailer.send({
                   to: user.email,
                   subject: 'Invitation to ' + config.crowi['app:title'],
-                  text: 'Hi, ' + user.email + '\n\n'
-                    + 'You are invited to our Wiki, you can log in with following account:\n\n'
-                    + 'Email: ' + user.email + '\n'
-                    + 'Password: ' + user.password
-                    + '\n (This password was auto generated. Update required at the first time you logging in)\n'
-                    + '\n'
-                    + 'We are waiting for you!\n'
-                    + config.crowi['app:url']
+                  template: 'admin/userInvitation.txt',
+                  vars: {
+                    email: user.email,
+                    password: user.password,
+                    url: config.crowi['app:url'],
+                    appTitle: config.crowi['app:title'],
+                  }
                 },
                 function (err, s) {
                   debug('completed to send email: ', err, s);

+ 36 - 1
routes/login.js

@@ -3,10 +3,13 @@ module.exports = function(app) {
 
   var googleapis = require('googleapis')
     , debug = require('debug')('crowi:routes:login')
+    , async    = require('async')
     , models = app.set('models')
     , config = app.set('config')
+    , mailer = app.set('mailer')
     , Page = models.Page
     , User = models.User
+    , Config = models.Config
     , Revision = models.Revision
     , actions = {};
 
@@ -139,7 +142,7 @@ module.exports = function(app) {
     }
 
     // config で closed ならさよなら
-    if (config.crowi['security:registrationMode'] == 'Closed') {
+    if (config.crowi['security:registrationMode'] == Config.SECURITY_REGISTRATION_MODE_CLOSED) {
       return res.redirect('/');
     }
 
@@ -179,6 +182,38 @@ module.exports = function(app) {
             req.flash('registerWarningMessage', 'ユーザー登録に失敗しました。');
             return res.redirect('/login?register=1');
           } else {
+
+            // 作成後、承認が必要なモードなら、管理者に通知する
+            if (config.crowi['security:registrationMode'] === Config.SECURITY_REGISTRATION_MODE_RESTRICTED) {
+              // TODO send mail
+              User.findAdmins(function(err, admins) {
+                async.each(
+                  admins,
+                  function(adminUser, next) {
+                    mailer.send({
+                        to: adminUser.email,
+                        subject: '[' + config.crowi['app:title'] + ':admin] A New User Created and Waiting for Activation',
+                        template: 'admin/userWaitingActivation.txt',
+                        vars: {
+                          createdUser: userData,
+                          adminUser: adminUser,
+                          url: config.crowi['app:url'],
+                          appTitle: config.crowi['app:title'],
+                        }
+                      },
+                      function (err, s) {
+                        debug('completed to send email: ', err, s);
+                        next();
+                      }
+                    );
+                  },
+                  function(err) {
+                    debug('Sending invitation email completed.', err);
+                  }
+                );
+              });
+            }
+
             if (facebookId || googleId) {
               userData.updateGoogleIdAndFacebookId(googleId, facebookId, function(err, userData) {
                 if (err) { // TODO

+ 14 - 0
views/mail/admin/userInvitation.txt

@@ -0,0 +1,14 @@
+Hi, {{ email }}
+
+You are invited to our Wiki, you can log in with following account:
+
+Email: {{ email }}
+Password: {{ password }}
+(This password was auto generated. Update required at the first time you logging in)
+
+We are waiting for you!
+{{ url }}
+
+--
+{{ appTitle }}
+{{ url }}

+ 21 - 0
views/mail/admin/userWaitingActivation.txt

@@ -0,0 +1,21 @@
+Hi, {{ adminUser.name }}
+
+A user registered to {{ appTitle }}.
+
+
+====
+Created user:
+
+Name: {{ createdUser.name }}
+User Name: {{ createdUser.username }}
+Email: {{ createdUser.email }}
+====
+
+Please do some action with following URL:
+{{ url }}/admin/user
+
+
+--
+{{ appTitle }}
+{{ url }}
+