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

refactor the User model so that it can be used without crowi instance

Yuki Takei 7 лет назад
Родитель
Сommit
c8cf701975
2 измененных файлов с 22 добавлено и 5 удалено
  1. 2 2
      src/server/crowi/index.js
  2. 20 3
      src/server/models/user.js

+ 2 - 2
src/server/crowi/index.js

@@ -124,8 +124,8 @@ Crowi.prototype.getEnv = function() {
 // getter/setter of model instance
 // getter/setter of model instance
 //
 //
 Crowi.prototype.model = function(name, model) {
 Crowi.prototype.model = function(name, model) {
-  if (model) {
-    return this.models[name] = model;
+  if (model != null) {
+    this.models[name] = model;
   }
   }
 
 
   return this.models[name];
   return this.models[name];

+ 20 - 3
src/server/models/user.js

@@ -21,9 +21,14 @@ module.exports = function(crowi) {
 
 
     , PAGE_ITEMS        = 50
     , PAGE_ITEMS        = 50
 
 
-    , userEvent = crowi.event('user')
-
   let userSchema;
   let userSchema;
+  let userEvent;
+
+  // init event
+  if (crowi != null) {
+    userEvent = crowi.event('user');
+    userEvent.on('activated', userEvent.onActivated);
+  }
 
 
   userSchema = new mongoose.Schema({
   userSchema = new mongoose.Schema({
     userId: String,
     userId: String,
@@ -55,9 +60,15 @@ module.exports = function(crowi) {
   userSchema.plugin(mongoosePaginate);
   userSchema.plugin(mongoosePaginate);
   userSchema.plugin(uniqueValidator);
   userSchema.plugin(uniqueValidator);
 
 
-  userEvent.on('activated', userEvent.onActivated);
+  function validateCrowi() {
+    if (crowi == null) {
+      throw new Error('"crowi" is null. Init User model with "crowi" argument first.');
+    }
+  }
 
 
   function decideUserStatusOnRegistration() {
   function decideUserStatusOnRegistration() {
+    validateCrowi();
+
     var Config = crowi.model('Config'),
     var Config = crowi.model('Config'),
       config = crowi.getConfig();
       config = crowi.getConfig();
 
 
@@ -96,6 +107,8 @@ module.exports = function(crowi) {
   }
   }
 
 
   function generatePassword(password) {
   function generatePassword(password) {
+    validateCrowi();
+
     var hasher = crypto.createHash('sha256');
     var hasher = crypto.createHash('sha256');
     hasher.update(crowi.env.PASSWORD_SEED + password);
     hasher.update(crowi.env.PASSWORD_SEED + password);
 
 
@@ -302,6 +315,8 @@ module.exports = function(crowi) {
   };
   };
 
 
   userSchema.statics.isEmailValid = function(email, callback) {
   userSchema.statics.isEmailValid = function(email, callback) {
+    validateCrowi();
+
     var config = crowi.getConfig()
     var config = crowi.getConfig()
       , whitelist = config.crowi['security:registrationWhiteList'];
       , whitelist = config.crowi['security:registrationWhiteList'];
 
 
@@ -573,6 +588,8 @@ module.exports = function(crowi) {
   };
   };
 
 
   userSchema.statics.createUsersByInvitation = function(emailList, toSendEmail, callback) {
   userSchema.statics.createUsersByInvitation = function(emailList, toSendEmail, callback) {
+    validateCrowi();
+
     var User = this
     var User = this
       , createdUserList = []
       , createdUserList = []
       , Config = crowi.model('Config')
       , Config = crowi.model('Config')