user-group-relation.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. }, {
  13. timestamps: { createdAt: true, updatedAt: false },
  14. });
  15. schema.plugin(mongoosePaginate);
  16. schema.plugin(uniqueValidator);
  17. /**
  18. * UserGroupRelation Class
  19. *
  20. * @class UserGroupRelation
  21. */
  22. class UserGroupRelation {
  23. /**
  24. * limit items num for pagination
  25. *
  26. * @readonly
  27. * @static
  28. * @memberof UserGroupRelation
  29. */
  30. static get PAGE_ITEMS() {
  31. return 50;
  32. }
  33. static set crowi(crowi) {
  34. this._crowi = crowi;
  35. }
  36. static get crowi() {
  37. return this._crowi;
  38. }
  39. /**
  40. * remove all invalid relations that has reference to unlinked document
  41. */
  42. static removeAllInvalidRelations() {
  43. return this.findAllRelation()
  44. .then((relations) => {
  45. // filter invalid documents
  46. return relations.filter((relation) => {
  47. return relation.relatedUser == null || relation.relatedGroup == null;
  48. });
  49. })
  50. .then((invalidRelations) => {
  51. const ids = invalidRelations.map((relation) => { return relation._id });
  52. return this.deleteMany({ _id: { $in: ids } });
  53. });
  54. }
  55. /**
  56. * find all user and group relation
  57. *
  58. * @static
  59. * @returns {Promise<UserGroupRelation[]>}
  60. * @memberof UserGroupRelation
  61. */
  62. static findAllRelation() {
  63. return this
  64. .find()
  65. .populate('relatedUser')
  66. .populate('relatedGroup')
  67. .exec();
  68. }
  69. /**
  70. * find all user and group relation of UserGroup
  71. *
  72. * @static
  73. * @param {UserGroup} userGroup
  74. * @returns {Promise<UserGroupRelation[]>}
  75. * @memberof UserGroupRelation
  76. */
  77. static findAllRelationForUserGroup(userGroup) {
  78. debug('findAllRelationForUserGroup is called', userGroup);
  79. return this
  80. .find({ relatedGroup: userGroup })
  81. .populate('relatedUser')
  82. .exec();
  83. }
  84. static async findAllUserIdsForUserGroup(userGroup) {
  85. const relations = await this
  86. .find({ relatedGroup: userGroup })
  87. .select('relatedUser')
  88. .exec();
  89. return relations.map(r => r.relatedUser);
  90. }
  91. /**
  92. * find all user and group relation of UserGroups
  93. *
  94. * @static
  95. * @param {UserGroup[]} userGroups
  96. * @returns {Promise<UserGroupRelation[]>}
  97. * @memberof UserGroupRelation
  98. */
  99. static findAllRelationForUserGroups(userGroups) {
  100. return this
  101. .find({ relatedGroup: { $in: userGroups } })
  102. .populate('relatedUser')
  103. .exec();
  104. }
  105. /**
  106. * find all user and group relation of User
  107. *
  108. * @static
  109. * @param {User} user
  110. * @returns {Promise<UserGroupRelation[]>}
  111. * @memberof UserGroupRelation
  112. */
  113. static findAllRelationForUser(user) {
  114. return this
  115. .find({ relatedUser: user.id })
  116. .populate('relatedGroup')
  117. // filter documents only relatedGroup is not null
  118. .then((userGroupRelations) => {
  119. return userGroupRelations.filter((relation) => {
  120. return relation.relatedGroup != null;
  121. });
  122. });
  123. }
  124. /**
  125. * find all UserGroup IDs that related to specified User
  126. *
  127. * @static
  128. * @param {User} user
  129. * @returns {Promise<ObjectId[]>}
  130. */
  131. static async findAllUserGroupIdsRelatedToUser(user) {
  132. const relations = await this.find({ relatedUser: user._id })
  133. .select('relatedGroup')
  134. .exec();
  135. return relations.map((relation) => { return relation.relatedGroup });
  136. }
  137. /**
  138. * count by related group id and related user
  139. *
  140. * @static
  141. * @param {string} userGroupId find query param for relatedGroup
  142. * @param {User} userData find query param for relatedUser
  143. * @returns {Promise<number>}
  144. */
  145. static async countByGroupIdAndUser(userGroupId, userData) {
  146. const query = {
  147. relatedGroup: userGroupId,
  148. relatedUser: userData.id,
  149. };
  150. return this.count(query);
  151. }
  152. /**
  153. * find all "not" related user for UserGroup
  154. *
  155. * @static
  156. * @param {UserGroup} userGroup for find users not related
  157. * @returns {Promise<User>}
  158. * @memberof UserGroupRelation
  159. */
  160. static findUserByNotRelatedGroup(userGroup, queryOptions) {
  161. const User = UserGroupRelation.crowi.model('User');
  162. let searchWord = new RegExp(`${queryOptions.searchWord}`);
  163. switch (queryOptions.searchType) {
  164. case 'forward':
  165. searchWord = new RegExp(`^${queryOptions.searchWord}`);
  166. break;
  167. case 'backword':
  168. searchWord = new RegExp(`${queryOptions.searchWord}$`);
  169. break;
  170. }
  171. const searthField = [
  172. { username: searchWord },
  173. ];
  174. if (queryOptions.isAlsoMailSearched === 'true') { searthField.push({ email: searchWord }) }
  175. if (queryOptions.isAlsoNameSearched === 'true') { searthField.push({ name: searchWord }) }
  176. return this.findAllRelationForUserGroup(userGroup)
  177. .then((relations) => {
  178. const relatedUserIds = relations.map((relation) => {
  179. return relation.relatedUser.id;
  180. });
  181. const query = {
  182. _id: { $nin: relatedUserIds },
  183. status: User.STATUS_ACTIVE,
  184. $or: searthField,
  185. };
  186. debug('findUserByNotRelatedGroup ', query);
  187. return User.find(query).exec();
  188. });
  189. }
  190. /**
  191. * get if the user has relation for group
  192. *
  193. * @static
  194. * @param {UserGroup} userGroup
  195. * @param {User} user
  196. * @returns {Promise<boolean>} is user related for group(or not)
  197. * @memberof UserGroupRelation
  198. */
  199. static isRelatedUserForGroup(userGroup, user) {
  200. const query = {
  201. relatedGroup: userGroup.id,
  202. relatedUser: user.id,
  203. };
  204. return this
  205. .count(query)
  206. .exec()
  207. .then((count) => {
  208. // return true or false of the relation is exists(not count)
  209. return (count > 0);
  210. });
  211. }
  212. /**
  213. * create user and group relation
  214. *
  215. * @static
  216. * @param {UserGroup} userGroup
  217. * @param {User} user
  218. * @returns {Promise<UserGroupRelation>} created relation
  219. * @memberof UserGroupRelation
  220. */
  221. static createRelation(userGroup, user) {
  222. return this.create({
  223. relatedGroup: userGroup.id,
  224. relatedUser: user.id,
  225. });
  226. }
  227. static async createRelations(userGroupIds, user) {
  228. const documentsToInsertMany = userGroupIds.map((groupId) => {
  229. return {
  230. relatedGroup: groupId,
  231. relatedUser: user._id,
  232. createdAt: new Date(),
  233. };
  234. });
  235. return this.insertMany(documentsToInsertMany);
  236. }
  237. /**
  238. * remove all relation for UserGroup
  239. *
  240. * @static
  241. * @param {UserGroup} userGroup related group for remove
  242. * @returns {Promise<any>}
  243. * @memberof UserGroupRelation
  244. */
  245. static removeAllByUserGroups(groupsToDelete) {
  246. if (!Array.isArray(groupsToDelete)) {
  247. throw Error('groupsToDelete must be an array.');
  248. }
  249. return this.deleteMany({ relatedGroup: { $in: groupsToDelete } });
  250. }
  251. /**
  252. * remove relation by id
  253. *
  254. * @static
  255. * @param {ObjectId} id
  256. * @returns {Promise<any>}
  257. * @memberof UserGroupRelation
  258. */
  259. static removeById(id) {
  260. return this.findById(id)
  261. .then((relationData) => {
  262. if (relationData == null) {
  263. throw new Error('UserGroupRelation data is not exists. id:', id);
  264. }
  265. else {
  266. relationData.remove();
  267. }
  268. });
  269. }
  270. static async findUserIdsByGroupId(groupId) {
  271. const relations = await this.find({ relatedGroup: groupId }, { _id: 0, relatedUser: 1 }).lean().exec(); // .lean() to get not ObjectId but string
  272. return relations.map(relation => relation.relatedUser);
  273. }
  274. static async createByGroupIdsAndUserIds(groupIds, userIds) {
  275. const insertOperations = [];
  276. groupIds.forEach((groupId) => {
  277. userIds.forEach((userId) => {
  278. insertOperations.push({
  279. insertOne: {
  280. document: {
  281. relatedGroup: groupId,
  282. relatedUser: userId,
  283. },
  284. },
  285. });
  286. });
  287. });
  288. await this.bulkWrite(insertOperations);
  289. }
  290. /**
  291. * Recursively finds descendant groups by populating relations.
  292. * @static
  293. * @param {UserGroupDocument[]} groups
  294. * @param {UserDocument} user
  295. * @returns UserGroupDocument[]
  296. */
  297. static async findGroupsWithDescendantsByGroupAndUser(group, user) {
  298. const descendantGroups = [group];
  299. const incrementGroupsRecursively = async(groups, user) => {
  300. const groupIds = groups.map(g => g._id);
  301. const populatedRelations = await this.aggregate([
  302. {
  303. $match: {
  304. relatedUser: user._id,
  305. },
  306. },
  307. {
  308. $lookup: {
  309. from: 'usergroups',
  310. localField: 'relatedGroup',
  311. foreignField: '_id',
  312. as: 'relatedGroup',
  313. },
  314. },
  315. {
  316. $unwind: {
  317. path: '$relatedGroup',
  318. },
  319. },
  320. {
  321. $match: {
  322. 'relatedGroup.parent': { $in: groupIds },
  323. },
  324. },
  325. ]);
  326. const nextGroups = populatedRelations.map(d => d.relatedGroup);
  327. // End
  328. const shouldEnd = nextGroups.length === 0;
  329. if (shouldEnd) {
  330. return;
  331. }
  332. // Increment
  333. descendantGroups.push(...nextGroups);
  334. return incrementGroupsRecursively(nextGroups, user);
  335. };
  336. await incrementGroupsRecursively([group], user);
  337. return descendantGroups;
  338. }
  339. }
  340. module.exports = function(crowi) {
  341. UserGroupRelation.crowi = crowi;
  342. schema.loadClass(UserGroupRelation);
  343. const model = mongoose.model('UserGroupRelation', schema);
  344. return model;
  345. };