group.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. module.exports = function(crowi) {
  2. var debug = require('debug')('crowi:models:group')
  3. , mongoose = require('mongoose')
  4. , mongoosePaginate = require('mongoose-paginate')
  5. , uniqueValidator = require('mongoose-unique-validator')
  6. , crypto = require('crypto')
  7. , async = require('async')
  8. , ObjectId = mongoose.Schema.Types.ObjectId
  9. , PAGE_ITEMS = 50
  10. , groupEvent = crowi.event('group')
  11. , groupSchema;
  12. groupSchema = new mongoose.Schema({
  13. groupId: String,
  14. image: String,
  15. name: { type: String },
  16. groupname: { type: String, index: true },
  17. password: String,
  18. createdAt: { type: Date, default: Date.now },
  19. });
  20. groupSchema.plugin(mongoosePaginate);
  21. groupSchema.plugin(uniqueValidator);
  22. groupEvent.on('activated', groupEvent.onActivated);
  23. function generateRandomTempPassword () {
  24. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!=-_';
  25. var password = '';
  26. var len = 12;
  27. for (var i = 0; i < len; i++) {
  28. var randomPoz = Math.floor(Math.random() * chars.length);
  29. password += chars.substring(randomPoz, randomPoz+1);
  30. }
  31. return password;
  32. }
  33. function generatePassword (password) {
  34. var hasher = crypto.createHash('sha256');
  35. hasher.update(crowi.env.PASSWORD_SEED + password);
  36. return hasher.digest('hex');
  37. }
  38. groupSchema.methods.isPasswordSet = function() {
  39. if (this.password) {
  40. return true;
  41. }
  42. return false;
  43. };
  44. groupSchema.methods.isPasswordValid = function(password) {
  45. return this.password == generatePassword(password);
  46. };
  47. groupSchema.methods.setPassword = function(password) {
  48. this.password = generatePassword(password);
  49. return this;
  50. };
  51. groupSchema.methods.updatePassword = function(password, callback) {
  52. this.setPassword(password);
  53. this.save(function(err, groupData) {
  54. return callback(err, groupData);
  55. });
  56. };
  57. groupSchema.methods.updateImage = function(image, callback) {
  58. this.image = image;
  59. this.save(function(err, groupData) {
  60. return callback(err, groupData);
  61. });
  62. };
  63. groupSchema.methods.deleteImage = function(callback) {
  64. return this.updateImage(null, callback);
  65. };
  66. groupSchema.statics.filterToPublicFields = function(group) {
  67. debug('Group is', typeof group, group);
  68. if (typeof group !== 'object' || !group._id) {
  69. return group;
  70. }
  71. var filteredGroup = {};
  72. var fields = GROUP_PUBLIC_FIELDS.split(' ');
  73. for (var i = 0; i < fields.length; i++) {
  74. var key = fields[i];
  75. if (group[key]) {
  76. filteredGroup[key] = group[key];
  77. }
  78. }
  79. return filteredGroup;
  80. };
  81. groupSchema.statics.findGroups = function(options, callback) {
  82. var sort = options.sort || {status: 1, createdAt: 1};
  83. this.find()
  84. .sort(sort)
  85. .skip(options.skip || 0)
  86. .limit(options.limit || 21)
  87. .exec(function (err, groupData) {
  88. callback(err, groupData);
  89. });
  90. };
  91. groupSchema.statics.findAllGroups = function(option) {
  92. var Group = this;
  93. var option = option || {}
  94. , sort = option.sort || {createdAt: -1}
  95. , status = option.status || [STATUS_ACTIVE, STATUS_SUSPENDED]
  96. , fields = option.fields || GROUP_PUBLIC_FIELDS
  97. ;
  98. if (!Array.isArray(status)) {
  99. status = [status];
  100. }
  101. return new Promise(function(resolve, reject) {
  102. Group
  103. .find()
  104. .or(status.map(s => { return {status: s}; }))
  105. .select(fields)
  106. .sort(sort)
  107. .exec(function (err, groupData) {
  108. if (err) {
  109. return reject(err);
  110. }
  111. return resolve(groupData);
  112. });
  113. });
  114. };
  115. groupSchema.statics.findGroupsByIds = function(ids, option) {
  116. var Group = this;
  117. var option = option || {}
  118. , sort = option.sort || {createdAt: -1}
  119. , status = option.status || STATUS_ACTIVE
  120. , fields = option.fields || GROUP_PUBLIC_FIELDS
  121. ;
  122. return new Promise(function(resolve, reject) {
  123. Group
  124. .find({ _id: { $in: ids }, status: status })
  125. .select(fields)
  126. .sort(sort)
  127. .exec(function (err, groupData) {
  128. if (err) {
  129. return reject(err);
  130. }
  131. return resolve(groupData);
  132. });
  133. });
  134. };
  135. groupSchema.statics.findAdmins = function(callback) {
  136. var Group = this;
  137. this.find({admin: true})
  138. .exec(function(err, admins) {
  139. debug('Admins: ', admins);
  140. callback(err, admins);
  141. });
  142. };
  143. groupSchema.statics.findGroupsWithPagination = function(options, callback) {
  144. var sort = options.sort || {status: 1, groupname: 1, createdAt: 1};
  145. this.paginate({status: { $ne: STATUS_DELETED }}, { page: options.page || 1, limit: options.limit || PAGE_ITEMS }, function(err, result) {
  146. if (err) {
  147. debug('Error on pagination:', err);
  148. return callback(err, null);
  149. }
  150. return callback(err, result);
  151. }, { sortBy : sort });
  152. };
  153. groupSchema.statics.findGroupByGroupname = function(groupname) {
  154. var Group = this;
  155. return new Promise(function(resolve, reject) {
  156. Group.findOne({groupname: groupname}, function (err, groupData) {
  157. if (err) {
  158. return reject(err);
  159. }
  160. return resolve(groupData);
  161. });
  162. });
  163. };
  164. groupSchema.statics.isRegisterableGroupname = function(groupname, callback) {
  165. var Group = this;
  166. var groupnameUsable = true;
  167. this.findOne({groupname: groupname}, function (err, groupData) {
  168. if (groupData) {
  169. groupnameUsable = false;
  170. }
  171. return callback(groupnameUsable);
  172. });
  173. };
  174. groupSchema.statics.removeCompletelyById = function(id, callback) {
  175. var Group = this;
  176. Group.findById(id, function (err, groupData) {
  177. if (!groupData) {
  178. return callback(err, null);
  179. }
  180. debug('Removing group:', groupData);
  181. groupData.remove(function(err) {
  182. if (err) {
  183. return callback(err, null);
  184. }
  185. return callback(null, 1);
  186. });
  187. });
  188. };
  189. groupSchema.statics.resetPasswordByRandomString = function(id) {
  190. var Group = this;
  191. return new Promise(function(resolve, reject) {
  192. Group.findById(id, function (err, groupData) {
  193. if (!groupData) {
  194. return reject(new Error('Group not found'));
  195. }
  196. var newPassword = generateRandomTempPassword();
  197. groupData.setPassword(newPassword);
  198. groupData.save(function(err, groupData) {
  199. if (err) {
  200. return reject(err);
  201. }
  202. resolve({group: groupData, newPassword: newPassword});
  203. });
  204. });
  205. });
  206. };
  207. groupSchema.statics.createGroupByNameAndPassword = function(name, groupname, password, callback) {
  208. var Group = this
  209. , newGroup = new Group();
  210. newGroup.name = name;
  211. newGroup.groupname = groupname;
  212. newGroup.setPassword(password);
  213. newGroup.createdAt = Date.now();
  214. newGroup.save(function(err, groupData) {
  215. return callback(err, groupData);
  216. });
  217. };
  218. groupSchema.statics.createGroupPictureFilePath = function(group, name) {
  219. var ext = '.' + name.match(/(.*)(?:\.([^.]+$))/)[2];
  220. return 'group/' + group._id + ext;
  221. };
  222. groupSchema.statics.getGroupnameByPath = function(path) {
  223. var groupname = null;
  224. if (m = path.match(/^\/group\/([^\/]+)\/?/)) {
  225. groupname = m[1];
  226. }
  227. return groupname;
  228. };
  229. groupSchema.statics.GROUP_PUBLIC_FIELDS = GROUP_PUBLIC_FIELDS;
  230. groupSchema.statics.PAGE_ITEMS = PAGE_ITEMS;
  231. return mongoose.model('Group', groupSchema);
  232. };