user-group-relation.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. const debug = require('debug')('growi:models:userGroupRelation');
  2. const mongoose = require('mongoose');
  3. const mongoosePaginate = require('mongoose-paginate-v2');
  4. const uniqueValidator = require('mongoose-unique-validator');
  5. const ObjectId = mongoose.Schema.Types.ObjectId;
  6. /*
  7. * define schema
  8. */
  9. const schema = new mongoose.Schema({
  10. relatedGroup: { type: ObjectId, ref: 'UserGroup', required: true },
  11. relatedUser: { type: ObjectId, ref: 'User', required: true },
  12. createdAt: { type: Date, default: Date.now, required: true },
  13. });
  14. schema.plugin(mongoosePaginate);
  15. schema.plugin(uniqueValidator);
  16. /**
  17. * UserGroupRelation Class
  18. *
  19. * @class UserGroupRelation
  20. */
  21. class UserGroupRelation {
  22. /**
  23. * limit items num for pagination
  24. *
  25. * @readonly
  26. * @static
  27. * @memberof UserGroupRelation
  28. */
  29. static get PAGE_ITEMS() {
  30. return 50;
  31. }
  32. static set crowi(crowi) {
  33. this._crowi = crowi;
  34. }
  35. static get crowi() {
  36. return this._crowi;
  37. }
  38. /**
  39. * remove all invalid relations that has reference to unlinked document
  40. */
  41. static removeAllInvalidRelations() {
  42. return this.findAllRelation()
  43. .then((relations) => {
  44. // filter invalid documents
  45. return relations.filter((relation) => {
  46. return relation.relatedUser == null || relation.relatedGroup == null;
  47. });
  48. })
  49. .then((invalidRelations) => {
  50. const ids = invalidRelations.map((relation) => { return relation._id });
  51. return this.deleteMany({ _id: { $in: ids } });
  52. });
  53. }
  54. /**
  55. * find all user and group relation
  56. *
  57. * @static
  58. * @returns {Promise<UserGroupRelation[]>}
  59. * @memberof UserGroupRelation
  60. */
  61. static findAllRelation() {
  62. return this
  63. .find()
  64. .populate('relatedUser')
  65. .populate('relatedGroup')
  66. .exec();
  67. }
  68. /**
  69. * find all user and group relation of UserGroup
  70. *
  71. * @static
  72. * @param {UserGroup} userGroup
  73. * @returns {Promise<UserGroupRelation[]>}
  74. * @memberof UserGroupRelation
  75. */
  76. static findAllRelationForUserGroup(userGroup) {
  77. debug('findAllRelationForUserGroup is called', userGroup);
  78. return this
  79. .find({ relatedGroup: userGroup })
  80. .populate('relatedUser')
  81. .exec();
  82. }
  83. /**
  84. * find all user and group relation of UserGroups
  85. *
  86. * @static
  87. * @param {UserGroup[]} userGroups
  88. * @returns {Promise<UserGroupRelation[]>}
  89. * @memberof UserGroupRelation
  90. */
  91. static findAllRelationForUserGroups(userGroups) {
  92. return this
  93. .find({ relatedGroup: { $in: userGroups } })
  94. .populate('relatedUser')
  95. .exec();
  96. }
  97. /**
  98. * find all user and group relation of User
  99. *
  100. * @static
  101. * @param {User} user
  102. * @returns {Promise<UserGroupRelation[]>}
  103. * @memberof UserGroupRelation
  104. */
  105. static findAllRelationForUser(user) {
  106. return this
  107. .find({ relatedUser: user.id })
  108. .populate('relatedGroup')
  109. // filter documents only relatedGroup is not null
  110. .then((userGroupRelations) => {
  111. return userGroupRelations.filter((relation) => {
  112. return relation.relatedGroup != null;
  113. });
  114. });
  115. }
  116. /**
  117. * find all UserGroup IDs that related to specified User
  118. *
  119. * @static
  120. * @param {User} user
  121. * @returns {Promise<ObjectId[]>}
  122. */
  123. static async findAllUserGroupIdsRelatedToUser(user) {
  124. const relations = await this.find({ relatedUser: user.id })
  125. .select('relatedGroup')
  126. .exec();
  127. return relations.map((relation) => { return relation.relatedGroup });
  128. }
  129. /**
  130. * find all entities with pagination
  131. *
  132. * @see https://github.com/edwardhotchkiss/mongoose-paginate
  133. *
  134. * @static
  135. * @param {UserGroup} userGroup
  136. * @param {any} opts mongoose-paginate options object
  137. * @returns {Promise<any>} mongoose-paginate result object
  138. * @memberof UserGroupRelation
  139. */
  140. static findUserGroupRelationsWithPagination(userGroup, opts) {
  141. const query = { relatedGroup: userGroup };
  142. const options = Object.assign({}, opts);
  143. if (options.page == null) {
  144. options.page = 1;
  145. }
  146. if (options.limit == null) {
  147. options.limit = UserGroupRelation.PAGE_ITEMS;
  148. }
  149. return this.paginate(query, options)
  150. .catch((err) => {
  151. debug('Error on pagination:', err);
  152. });
  153. }
  154. /**
  155. * count by related group id and related user
  156. *
  157. * @static
  158. * @param {string} userGroupId find query param for relatedGroup
  159. * @param {User} userData find query param for relatedUser
  160. * @returns {Promise<number>}
  161. */
  162. static async countByGroupIdAndUser(userGroupId, userData) {
  163. const query = {
  164. relatedGroup: userGroupId,
  165. relatedUser: userData.id,
  166. };
  167. return this.count(query);
  168. }
  169. /**
  170. * find all "not" related user for UserGroup
  171. *
  172. * @static
  173. * @param {UserGroup} userGroup for find users not related
  174. * @returns {Promise<User>}
  175. * @memberof UserGroupRelation
  176. */
  177. static findUserByNotRelatedGroup(userGroup) {
  178. const User = UserGroupRelation.crowi.model('User');
  179. return this.findAllRelationForUserGroup(userGroup)
  180. .then((relations) => {
  181. const relatedUserIds = relations.map((relation) => {
  182. return relation.relatedUser.id;
  183. });
  184. const query = { _id: { $nin: relatedUserIds }, status: User.STATUS_ACTIVE };
  185. debug('findUserByNotRelatedGroup ', query);
  186. return User.find(query).exec();
  187. });
  188. }
  189. /**
  190. * get if the user has relation for group
  191. *
  192. * @static
  193. * @param {User} userData
  194. * @param {UserGroup} userGroup
  195. * @returns {Promise<boolean>} is user related for group(or not)
  196. * @memberof UserGroupRelation
  197. */
  198. static isRelatedUserForGroup(userData, userGroup) {
  199. const query = {
  200. relatedGroup: userGroup.id,
  201. relatedUser: userData.id,
  202. };
  203. return this
  204. .count(query)
  205. .exec()
  206. .then((count) => {
  207. // return true or false of the relation is exists(not count)
  208. return (count > 0);
  209. });
  210. }
  211. /**
  212. * create user and group relation
  213. *
  214. * @static
  215. * @param {UserGroup} userGroup
  216. * @param {User} user
  217. * @returns {Promise<UserGroupRelation>} created relation
  218. * @memberof UserGroupRelation
  219. */
  220. static createRelation(userGroup, user) {
  221. return this.create({
  222. relatedGroup: userGroup.id,
  223. relatedUser: user.id,
  224. });
  225. }
  226. /**
  227. * remove all relation for UserGroup
  228. *
  229. * @static
  230. * @param {UserGroup} userGroup related group for remove
  231. * @returns {Promise<any>}
  232. * @memberof UserGroupRelation
  233. */
  234. static removeAllByUserGroup(userGroup) {
  235. return this.deleteMany({ relatedGroup: userGroup });
  236. }
  237. /**
  238. * remove relation by id
  239. *
  240. * @static
  241. * @param {ObjectId} id
  242. * @returns {Promise<any>}
  243. * @memberof UserGroupRelation
  244. */
  245. static removeById(id) {
  246. return this.findById(id)
  247. .then((relationData) => {
  248. if (relationData == null) {
  249. throw new Error('UserGroupRelation data is not exists. id:', id);
  250. }
  251. else {
  252. relationData.remove();
  253. }
  254. });
  255. }
  256. }
  257. module.exports = function(crowi) {
  258. UserGroupRelation.crowi = crowi;
  259. schema.loadClass(UserGroupRelation);
  260. const model = mongoose.model('UserGroupRelation', schema);
  261. return model;
  262. };