user.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. module.exports = function(app, models) {
  2. var mongoose = require('mongoose')
  3. , mongoosePaginate = require('mongoose-paginate')
  4. , debug = require('debug')('crowi:models:user')
  5. , crypto = require('crypto')
  6. , config = app.set('config')
  7. , ObjectId = mongoose.Schema.Types.ObjectId
  8. , STATUS_REGISTERED = 1
  9. , STATUS_ACTIVE = 2
  10. , STATUS_SUSPENDED = 3
  11. , STATUS_DELETED = 4
  12. , STATUS_INVITED = 5
  13. , PAGE_ITEMS = 20
  14. , userSchema;
  15. userSchema = new mongoose.Schema({
  16. userId: String,
  17. fbId: String, // userId
  18. image: String,
  19. googleId: String,
  20. name: { type: String },
  21. username: { type: String },
  22. email: { type: String, required: true },
  23. password: String,
  24. status: { type: Number, required: true, default: STATUS_ACTIVE },
  25. createdAt: { type: Date, default: Date.now },
  26. admin: { type: Boolean, default: 0 }
  27. });
  28. userSchema.plugin(mongoosePaginate);
  29. function decideUserStatusOnRegistration () {
  30. var Config = models.Config;
  31. // status decided depends on registrationMode
  32. switch (config.crowi['security:registrationMode']) {
  33. case Config.SECURITY_REGISTRATION_MODE_OPEN:
  34. return STATUS_ACTIVE;
  35. case Config.SECURITY_REGISTRATION_MODE_RESTRICTED:
  36. case Config.SECURITY_REGISTRATION_MODE_CLOSED: // 一応
  37. return STATUS_REGISTERED;
  38. default:
  39. return STATUS_ACTIVE; // どっちにすんのがいいんだろうな
  40. }
  41. }
  42. function generatePassword (password) {
  43. var hasher = crypto.createHash('sha256');
  44. hasher.update(process.env.PASSWORD_SEED + password);
  45. return hasher.digest('hex');
  46. }
  47. userSchema.methods.isPasswordSet = function() {
  48. if (this.password) {
  49. return true;
  50. }
  51. return false;
  52. };
  53. userSchema.methods.isPasswordValid = function(password) {
  54. return this.password == generatePassword(password);
  55. };
  56. userSchema.methods.setPassword = function(password) {
  57. return this.password == generatePassword(password);
  58. };
  59. userSchema.methods.setPassword = function(password) {
  60. this.password = generatePassword(password);
  61. return this;
  62. };
  63. userSchema.methods.isEmailSet = function() {
  64. if (this.email) {
  65. return true;
  66. }
  67. return false;
  68. };
  69. userSchema.methods.update = function(name, email, callback) {
  70. this.name = name;
  71. this.email = email;
  72. this.save(function(err, userData) {
  73. return callback(err, userData);
  74. });
  75. };
  76. userSchema.methods.updatePassword = function(password, callback) {
  77. this.setPassword(password);
  78. this.save(function(err, userData) {
  79. return callback(err, userData);
  80. });
  81. };
  82. userSchema.methods.updateImage = function(image, callback) {
  83. this.image = image;
  84. this.save(function(err, userData) {
  85. return callback(err, userData);
  86. });
  87. };
  88. userSchema.methods.deleteImage = function(callback) {
  89. return this.updateImage(null, callback);
  90. };
  91. userSchema.methods.updateFacebookId = function(fbId, callback) {
  92. this.fbId = this.userId = fbId;
  93. this.save(function(err, userData) {
  94. return callback(err, userData);
  95. });
  96. };
  97. userSchema.methods.deleteFacebookId = function(callback) {
  98. return this.updateFacebookId(null, callback);
  99. };
  100. userSchema.methods.updateGoogleId = function(googleId, callback) {
  101. this.googleId = googleId;
  102. this.save(function(err, userData) {
  103. return callback(err, userData);
  104. });
  105. };
  106. userSchema.methods.deleteGoogleId = function(callback) {
  107. return this.updateGoogleId(null, callback);
  108. };
  109. userSchema.methods.removeFromAdmin = function(callback) {
  110. debug('Remove from admin', this);
  111. this.admin = 0;
  112. this.save(function(err, userData) {
  113. return callback(err, userData);
  114. });
  115. };
  116. userSchema.methods.makeAdmin = function(callback) {
  117. debug('Admin', this);
  118. this.admin = 1;
  119. this.save(function(err, userData) {
  120. return callback(err, userData);
  121. });
  122. };
  123. userSchema.methods.statusActivate = function(callback) {
  124. debug('Activate User', this);
  125. this.status = STATUS_ACTIVE;
  126. this.save(function(err, userData) {
  127. return callback(err, userData);
  128. });
  129. };
  130. userSchema.methods.statusSuspend = function(callback) {
  131. debug('Suspend User', this);
  132. this.status = STATUS_SUSPENDED;
  133. if (this.email === undefined || this.email === null) { // migrate old data
  134. this.email = '-';
  135. }
  136. if (this.name === undefined || this.name === null) { // migrate old data
  137. this.name = '-' + Date.now();
  138. }
  139. if (this.username === undefined || this.usename === null) { // migrate old data
  140. this.username = '-';
  141. }
  142. this.save(function(err, userData) {
  143. return callback(err, userData);
  144. });
  145. };
  146. userSchema.methods.statusDelete = function(callback) {
  147. debug('Delete User', this);
  148. this.status = STATUS_DELETED;
  149. this.password = '';
  150. this.email = 'deleted@deleted';
  151. this.googleId = null;
  152. this.fbId = null;
  153. this.image = null;
  154. this.save(function(err, userData) {
  155. return callback(err, userData);
  156. });
  157. };
  158. userSchema.methods.updateGoogleIdAndFacebookId = function(googleId, facebookId, callback) {
  159. this.googleId = googleId;
  160. this.fbId = this.userId = facebookId;
  161. this.save(function(err, userData) {
  162. return callback(err, userData);
  163. });
  164. };
  165. userSchema.statics.getUserStatusLabels = function() {
  166. var userStatus = {};
  167. userStatus[STATUS_REGISTERED] = '承認待ち';
  168. userStatus[STATUS_ACTIVE] = 'Active';
  169. userStatus[STATUS_SUSPENDED] = 'Suspended';
  170. userStatus[STATUS_DELETED] = 'Deleted';
  171. userStatus[STATUS_INVITED] = '招待済み';
  172. return userStatus;
  173. };
  174. userSchema.statics.isEmailValid = function(email, callback) {
  175. if (Array.isArray(config.crowi['security:registrationWhiteList'])) {
  176. return config.crowi['security:registrationWhiteList'].some(function(allowedEmail) {
  177. var re = new RegExp(allowedEmail + '$');
  178. return re.test(email);
  179. });
  180. }
  181. return true;
  182. };
  183. userSchema.statics.findUsers = function(options, callback) {
  184. var sort = options.sort || {status: 1, createdAt: 1};
  185. this.find()
  186. .sort(sort)
  187. .skip(options.skip || 0)
  188. .limit(options.limit || 21)
  189. .exec(function (err, userData) {
  190. callback(err, userData);
  191. });
  192. };
  193. userSchema.statics.findUsersWithPagination = function(options, callback) {
  194. var sort = options.sort || {status: 1, username: 1, createdAt: 1};
  195. this.paginate({}, options.page || 1, PAGE_ITEMS, function(err, pageCount, paginatedResults, itemCount) {
  196. if (err) {
  197. debug('Error on pagination:', err);
  198. return callback(err, null);
  199. }
  200. return callback(err, paginatedResults, pageCount, itemCount);
  201. }, { sortBy : sort });
  202. };
  203. userSchema.statics.findUserByUsername = function(username, callback) {
  204. this.findOne({username: username}, function (err, userData) {
  205. callback(err, userData);
  206. });
  207. };
  208. userSchema.statics.findUserByFacebookId = function(fbId, callback) {
  209. this.findOne({userId: fbId}, function (err, userData) {
  210. callback(err, userData);
  211. });
  212. };
  213. userSchema.statics.findUserByGoogleId = function(googleId, callback) {
  214. this.findOne({googleId: googleId}, function (err, userData) {
  215. callback(err, userData);
  216. });
  217. };
  218. userSchema.statics.findUserByEmailAndPassword = function(email, password, callback) {
  219. var hashedPassword = generatePassword(password);
  220. this.findOne({email: email, password: hashedPassword}, function (err, userData) {
  221. callback(err, userData);
  222. });
  223. };
  224. userSchema.statics.isRegisterable = function(email, username, callback) {
  225. var User = this;
  226. var emailUsable = true;
  227. var usernameUsable = true;
  228. // username check
  229. this.findOne({username: username}, function (err, userData) {
  230. if (userData) {
  231. usernameUsable = false;
  232. }
  233. // email check
  234. User.findOne({email: email}, function (err, userData) {
  235. if (userData) {
  236. emailUsable = false;
  237. }
  238. if (!emailUsable || !usernameUsable) {
  239. return callback(false, {email: emailUsable, username: usernameUsable});
  240. }
  241. return callback(true, {});
  242. });
  243. });
  244. };
  245. userSchema.statics.createUserByEmailAndPassword = function(name, username, email, password, callback) {
  246. var User = this
  247. , newUser = new User();
  248. newUser.name = name;
  249. newUser.username = username;
  250. newUser.email = email;
  251. newUser.setPassword(password);
  252. newUser.createdAt = Date.now();
  253. newUser.status = decideUserStatusOnRegistration();
  254. newUser.save(function(err, userData) {
  255. return callback(err, userData);
  256. });
  257. };
  258. userSchema.statics.createUserByFacebook = function(fbUserInfo, callback) {
  259. var User = this
  260. , newUser = new User();
  261. newUser.userId = fbUserInfo.id;
  262. newUser.image = '//graph.facebook.com/' + fbUserInfo.id + '/picture?size=square';
  263. newUser.name = fbUserInfo.name || '';
  264. newUser.username = fbUserInfo.username || '';
  265. newUser.email = fbUserInfo.email || '';
  266. newUser.createdAt = Date.now();
  267. newUser.status = decideUserStatusOnRegistration();
  268. newUser.save(function(err, userData) {
  269. return callback(err, userData);
  270. });
  271. };
  272. userSchema.statics.createUserPictureFilePath = function(user, name) {
  273. var ext = '.' + name.match(/(.*)(?:\.([^.]+$))/)[2];
  274. return 'user/' + user._id + ext;
  275. };
  276. userSchema.statics.STATUS_REGISTERED = STATUS_REGISTERED;
  277. userSchema.statics.STATUS_ACTIVE = STATUS_ACTIVE;
  278. userSchema.statics.STATUS_SUSPENDED = STATUS_SUSPENDED;
  279. userSchema.statics.STATUS_DELETED = STATUS_DELETED;
  280. userSchema.statics.STATUS_INVITED = STATUS_INVITED;
  281. models.User = mongoose.model('User', userSchema);
  282. return models.User;
  283. };