user.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 = function(req, res) {
  54. const username = req.query.username;
  55. User.findUserByUsername(username)
  56. .then((userData) => {
  57. if (userData) {
  58. return res.json({ valid: false });
  59. }
  60. return res.json({ valid: true });
  61. })
  62. .catch((err) => {
  63. return res.json({ valid: true });
  64. });
  65. };
  66. /**
  67. * @swagger
  68. *
  69. * /users.list:
  70. * get:
  71. * tags: [Users, CrowiCompatibles]
  72. * operationId: listUsersV1
  73. * summary: /users.list
  74. * description: Get list of users
  75. * parameters:
  76. * - in: query
  77. * name: user_ids
  78. * schema:
  79. * type: string
  80. * description: user IDs
  81. * example: 5e06fcc7516d64004dbf4da6,5e098d53baa2ac004e7d24ad
  82. * responses:
  83. * 200:
  84. * description: Succeeded to get list of users.
  85. * content:
  86. * application/json:
  87. * schema:
  88. * properties:
  89. * ok:
  90. * $ref: '#/components/schemas/V1Response/properties/ok'
  91. * users:
  92. * type: array
  93. * items:
  94. * $ref: '#/components/schemas/User'
  95. * description: user list
  96. * 403:
  97. * $ref: '#/components/responses/403'
  98. * 500:
  99. * $ref: '#/components/responses/500'
  100. */
  101. /**
  102. * @api {get} /users.list Get user list
  103. * @apiName GetUserList
  104. * @apiGroup User
  105. *
  106. * @apiParam {String} user_ids
  107. */
  108. api.list = async function(req, res) {
  109. const userIds = req.query.user_ids || null; // TODO: handling
  110. let userFetcher;
  111. if (!userIds || userIds.split(',').length <= 0) {
  112. userFetcher = User.findAllUsers();
  113. }
  114. else {
  115. userFetcher = User.findUsersByIds(userIds.split(','));
  116. }
  117. const data = {};
  118. try {
  119. const users = await userFetcher;
  120. data.users = users.map((user) => {
  121. // omit email
  122. if (user.isEmailPublished !== true) { // compare to 'true' because Crowi original data doesn't have 'isEmailPublished'
  123. user.email = undefined;
  124. }
  125. return user.toObject({ virtuals: true });
  126. });
  127. }
  128. catch (err) {
  129. return res.json(ApiResponse.error(err));
  130. }
  131. return res.json(ApiResponse.success(data));
  132. };
  133. return actions;
  134. };