user.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @swagger
  3. *
  4. * components:
  5. * schemas:
  6. * User:
  7. * description: User
  8. * type: object
  9. * properties:
  10. * __v:
  11. * type: number
  12. * description: record version
  13. * example: 0
  14. * _id:
  15. * type: string
  16. * description: user ID
  17. * example: 5ae5fccfc5577b0004dbd8ab
  18. * lang:
  19. * type: string
  20. * description: language
  21. * example: 'en_US'
  22. * status:
  23. * type: integer
  24. * description: status
  25. * example: 0
  26. * admin:
  27. * type: boolean
  28. * description: whether the admin
  29. * example: false
  30. * email:
  31. * type: string
  32. * description: E-Mail address
  33. * example: alice@aaa.aaa
  34. * username:
  35. * type: string
  36. * description: username
  37. * example: alice
  38. * name:
  39. * type: string
  40. * description: full name
  41. * example: Alice
  42. * createdAt:
  43. * type: string
  44. * description: date created at
  45. * example: 2010-01-01T00:00:00.000Z
  46. */
  47. module.exports = function(crowi, app) {
  48. const User = crowi.model('User');
  49. const ApiResponse = require('../util/apiResponse');
  50. const actions = {};
  51. const api = {};
  52. actions.api = api;
  53. api.checkUsername = async function(req, res) {
  54. const username = req.query.username;
  55. let valid = false;
  56. await User.findUserByUsername(username)
  57. .then((userData) => {
  58. if (userData) {
  59. valid = false;
  60. }
  61. else {
  62. valid = true;
  63. }
  64. })
  65. .catch((err) => {
  66. valid = false;
  67. });
  68. return res.json(ApiResponse.success({ valid }));
  69. };
  70. /**
  71. * @swagger
  72. *
  73. * /users.list:
  74. * get:
  75. * tags: [Users, CrowiCompatibles]
  76. * operationId: listUsersV1
  77. * summary: /users.list
  78. * description: Get list of users
  79. * parameters:
  80. * - in: query
  81. * name: user_ids
  82. * schema:
  83. * type: string
  84. * description: user IDs
  85. * example: 5e06fcc7516d64004dbf4da6,5e098d53baa2ac004e7d24ad
  86. * responses:
  87. * 200:
  88. * description: Succeeded to get list of users.
  89. * content:
  90. * application/json:
  91. * schema:
  92. * properties:
  93. * ok:
  94. * $ref: '#/components/schemas/V1Response/properties/ok'
  95. * users:
  96. * type: array
  97. * items:
  98. * $ref: '#/components/schemas/User'
  99. * description: user list
  100. * 403:
  101. * $ref: '#/components/responses/403'
  102. * 500:
  103. * $ref: '#/components/responses/500'
  104. */
  105. /**
  106. * @api {get} /users.list Get user list
  107. * @apiName GetUserList
  108. * @apiGroup User
  109. *
  110. * @apiParam {String} user_ids
  111. */
  112. api.list = async function(req, res) {
  113. const userIds = req.query.user_ids || null; // TODO: handling
  114. let userFetcher;
  115. if (!userIds || userIds.split(',').length <= 0) {
  116. userFetcher = User.findAllUsers();
  117. }
  118. else {
  119. userFetcher = User.findUsersByIds(userIds.split(','));
  120. }
  121. const data = {};
  122. try {
  123. const users = await userFetcher;
  124. data.users = users.map((user) => {
  125. // omit email
  126. if (user.isEmailPublished !== true) { // compare to 'true' because Crowi original data doesn't have 'isEmailPublished'
  127. user.email = undefined;
  128. }
  129. return user.toObject({ virtuals: true });
  130. });
  131. }
  132. catch (err) {
  133. return res.json(ApiResponse.error(err));
  134. }
  135. return res.json(ApiResponse.success(data));
  136. };
  137. return actions;
  138. };