user.js 9.3 KB

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